I used options.inPurgeable
to store bitmap in Ashmem
when decoding bitmap. So that I don't wanna create more bitmap on Java heap
(causes much GC) when drawing circle bitmap in onDraw()
method. My code is below, but it doesn't work
private void init() {
mRoundPaint = new Paint();
mRoundPaint.setColor(Color.RED);
mRoundPaint.setStyle(Paint.Style.FILL);
mRoundPaint.setAntiAlias(true);
// mRoundPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mBmPaint = new Paint();
mBmPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = Math.min(w, h);
rectDes = new Rect(0, 0, mWidth, mWidth);
}
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
if(getDrawable() == null)
return;
// Option 1
Drawable drawable = getDrawable();
canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mRoundPaint);
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
canvas.drawBitmap(bitmap, null, rectDes, mBmPaint);
// Option 2
// Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
// canvas.drawBitmap(bitmap, null, rectDes, mBmPaint);
// canvas.drawCircle(mWidth / 2, mWidth / 2, mWidth / 2, mRoundPaint);
}
Here is the result:
What I expect
I also used BitmapShader, It worked. But I don't know why BitmapShader keeps my bitmaps in Ashmem aren't removed by GC.
BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(mWidth / 2, mWidth/ 2, mWidth /2, paint);
I appreciate all your help.
EDIT Sorry, I forget to show my expected bitmap.
Uhm, If I don't misunderstand, you want to get rounded bitmap from normal image? Here's my function to do so, you can try to change it whatever you like: