Override onDraw to change how the drawing occurs (Android)
- by Casebash
I want to change how my UI elements display, so I am overriding onDraw. The following code allows me to change a View to be drawn using PorterDuff.Mode.DARKEN. Unfortunately, this method involves creating a bitmap the size of the entire screen, then drawing to it then drawing this large bitmap the main screen again, for each UI element. This isn't at all efficient. Is it possible to achieve this in a more effecient way?
@Override
protected void onDraw(Canvas canvas) {
//TODO: Reduce the burden from multiple drawing
Bitmap bitmap=Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888);
Log.e("tmp",canvas.getClipBounds().toString());
Canvas offscreen=new Canvas(bitmap);
super.onDraw(offscreen);
//Then draw onscreen
Paint p=new Paint();
p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
canvas.drawBitmap(bitmap, 0, 0, p);
}