0

Frescoには円形の画像と丸みのあるコーナーが組み込まれていますが、ダイヤモンドや平行四辺形などの他の形状はどうですか?Android Fresco:さまざまな種類の画像を描画する

BitmapShaderを使用するカスタムdrawableを使用して標準のImageViewを使用するのは簡単です。

enter image description here

public class MaskDrawable extends Drawable { 
    private Paint mPaint; 
    private Path mPath; 
    private int mSlopeHeight; 

    public MaskDrawable(Bitmap bitmap, int slopeHeight) { 
     BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
     mPaint = new Paint(); 
     mPaint.setAntiAlias(true); 
     mPaint.setShader(shader); 
     mSlopeHeight = slopeHeight; 

     mPath = new Path(); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     Rect bounds = getBounds(); 

     mPath.moveTo(0, 0); 
     mPath.lineTo(0, bounds.bottom); 
     mPath.lineTo(bounds.right, bounds.bottom - mSlopeHeight); 
     mPath.lineTo(bounds.right, 0); 
     canvas.drawPath(mPath, mPaint); 
    } 

をフレスコで、私は画像のビットマップが必要であることを行うには:たとえば、次のカスタムDrawableのはImageViewのは、この写真のように見えるように画像のビットマップと斜面の高さを受け取りますしかし、私はそれをどうやって行うのか分からない。私は、ImagePipelineからBitmapを直接取得することができますが、それはそれに付随する多くの問題点です。あるケースでは、返されたBitmapは短期間であり、画面に描画するために使用すべきではありません。他のケースでは、私には明らかではないいくつかの点で解放する必要があるCloseableReferenceを取得します。私はまだ試していませんし、誰かが代わりにビットとバイトIの実用的なソリューションを提供できるかどうか迷った

ImagePipeline imagePipeline = Fresco.getImagePipeline(); 

     ImageRequest imageRequest = ImageRequestBuilder 
       .newBuilderWithSource(uri) 
       .setRequestPriority(Priority.HIGH) 
       .setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH) 
       .build(); 

     DataSource<CloseableReference<CloseableBitmap>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, getContext()); 

     DataSubscriber<CloseableReference<CloseableBitmap>> dataSubscriber = 
       new BaseDataSubscriber<CloseableReference<CloseableBitmap>>() { 
        @Override 
        protected void onNewResultImpl(DataSource<CloseableReference<CloseableBitmap>> dataSource) { 
         mBitmapRef = dataSource.getResult(); 
         // Get the bitmap here and use it in my custom drawable? 
        } 

        @Override 
        protected void onFailureImpl(DataSource<CloseableReference<CloseableBitmap>> dataSource) { 
        } 
       }; 

     dataSource.subscribe(dataSubscriber, UiThreadImmediateExecutorService.getInstance()); 

:私はネット上で見てきたことは、これまでのビットマップを取得するため、このようなコードですこれまでにさまざまな場所から集まってきました。それは正しく行われなければならない、そうでなければ、私は簡単にメモリをリークすることができます。

答えて

-1

あなたはビューを扱っているときにイメージパイプラインを使用する必要はありません。

ポストプロセッサでビットマップを管理する方法もあります。プロセスメソッドをオーバーライドし、同じBitmapShader、ペイント、キャンバスの実装を使用し、PlatformBitmapFactory createBitmapを使用してビットマップCloseableReferenceを作成し、最後にビットマップの終了時に参照を閉じる必要があります。

はより http://frescolib.org/docs/modifying-image.html

EDIT

に以下は、私は傑王からの助けを取得した後、思い付いた最終的な実装がある参照してください。次のコードスニペットは、質問で提示した図形に画像を配置します。

mSimpleDraweeView = (SimpleDraweeView) findViewById(R.id.shaped_picture); 
final int slopeHeight = 100; 

Postprocessor maskProcessor = new BasePostprocessor() { 
    @Override 
    public CloseableReference<Bitmap> process(Bitmap sourceBitmap, PlatformBitmapFactory bitmapFactory) { 
     // Get the size of the downloaded bitmap 
     final int width = sourceBitmap.getWidth(); 
     final int height = sourceBitmap.getHeight(); 

     // Create a new bitmap and use it to draw the shape that we want. 
     CloseableReference<Bitmap> bitmapRef = bitmapFactory.createBitmap(width, height); 
     try { 
      Bitmap destBitmap = bitmapRef.get(); 

      // Create canvas using the new bitmap we created earlier 
      Canvas canvas = new Canvas(destBitmap); 

      // Set up the Paint we will use for filling in the shape 
      // BitmapShader will fill the shape with the downloaded bitmap 
      BitmapShader shader = new BitmapShader(sourceBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); 
      Paint paint = new Paint(); 
      paint.setAntiAlias(true); 
      paint.setShader(shader); 

      // Set up the actual shape. Modify this part with any shape you want to have. 
      Path path = new Path(); 
      path.moveTo(0, 0); 
      path.lineTo(0, height); 
      path.lineTo(width, height - slopeHeight); 
      path.lineTo(width, 0); 

      // Draw the shape and fill it with the paint 
      canvas.drawPath(path, paint); 

      return CloseableReference.cloneOrNull(bitmapRef); 
     } 
     finally { 
      CloseableReference.closeSafely(bitmapRef); 
     } 
    } 
}; 

ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri) 
     .setPostprocessor(maskProcessor) 
     .build(); 

DraweeController controller = Fresco.newDraweeControllerBuilder() 
     .setImageRequest(request) 
     .setOldController(mSimpleDraweeView.getController()) 
     .build(); 

mSimpleDraweeView.setController(controller); 
+0

他の誰かがこの問題にぶつかる場合に備えて、具体的なソリューションの実装を行うように答えを更新しました。 –

関連する問題