Adding BitMap Resource to Paint Object in Android -
i have pain object want overlay bitmap android resources.
public static paint createpaint(int color, int strokewidth, style style) { paint paint = androidgraphicfactory.instance.createpaint(); paint.setcolor(color); paint.setstrokewidth(strokewidth); paint.setstyle(style); return paint; } it simple thing do. not understanding android paint, canvas, , bitmap relationships.
paint objects used style canvas operations in 'ondraw' method of view. canvas object lets draw bitmaps directly it. if wanted bitmap background draw bitmap first, use other canvas operations draw on top using configured paint objects style drawing operations.
see view#ondraw() , linked canvas object in api docs: http://developer.android.com/reference/android/view/view.html#ondraw(android.graphics.canvas)
other tips, avoid object allocation in ondraw method. create paint object(s) , load bitmaps elsewhere , keep reference them use in ondraw method.
extremely simplistic example:
public class customview extends view { private bitmap background; private paint paint; private float size; public customview(context context) { super(context); init(); } public customview(context context, attributeset attrs) { super(context, attrs); init(); } public customview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); init(); } public void init() { // prepare paint objects paint = new paint(); paint.setstrokewidth(2); paint.setcolor(color.red); // load bitmap background = bitmapfactory.decoderesource(getresources(), r.drawable.waracle_square); // calculate density dependant variables size = getresources().getdisplaymetrics().density * 100; } @override protected void ondraw(canvas canvas) { // paint optional when drawing image canvas.drawbitmap(background, 0, 0, null); // red circle of size 100dp in top left corner canvas.drawoval(0, 0, size, size, paint); } }
Comments
Post a Comment