I've implemented drawing for component like in picture following way:
@Override
protected void onDraw(Canvas canvas) {
int w = canvas.getWidth();
int h = canvas.getHeight();
if (mFinalBitmap == null) {
mFinalBitmap = Bitmap.createBitmap(w, h,
Bitmap.Config.ARGB_8888);
}
if (mTempCanvas == null) {
mTempCanvas = new Canvas(mFinalBitmap);
}
if (mBackgroundBitmap == null) {
mBackgroundBitmap = createBitmap(R.drawable.rounded_background,
w, h);
}
if (mBackgroundImage == null) {
mBackgroundImage = createBitmap(R.drawable.image_for_background, w, h);
}
mTempCanvas.drawBitmap(mBackgroundBitmap, 0, 0, null);
mTempCanvas.drawBitmap(mBackgroundImage, 0, 0, mPaint);
canvas.drawBitmap(mFinalBitmap, 0, 0, null);
}
private Bitmap createBitmap(int id, int width, int height) {
Bitmap bitmap = BitmapFactory.decodeResource(getContext()
.getResources(), id);
return Bitmap.createScaledBitmap(bitmap, width, height, false);
}
Where mPaint has
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
I'm wondering whether the code is good, or can be optimized for same result, it uses lot's of memory and is an potential trigger for OutOfMemoryError
.
Thanks.