I've created a custom view by extending View.
public class CustomView extends View {
private Canvas canvas2;
private Bitmap backingBitmap;
In CustomView's constructor, I have the following code:
backingBitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
canvas2 = new Canvas(backingBitmap);
In onDraw, I have:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(100, 100, 50, paint);
canvas2.drawCircle(0, 0, 10, paint);
}
I can see the circle that gets drawn using "canvas" object, but not the one that's drawn using "canvas2" object. My understanding is that one needs only create a mutable Bitmap and make it a Canvas' backing bitmap. Can someone point out what's wrong here?
Bonus question: Where does the custom canvas object get created on the screen, and how would one set its position?
Reason for creating multiple canvases: I have a SurfaceView which encompasses the entire screen. So, basically its canvas covers the entire screen. I need a make a window-like rectangle within the screen and stuff needs to animate within the bounds of that window. Basically, if you translate an image within this window, the image shouldn't get drawn outside the window's bounds.
Cheers!
Canvas
object valid or not, definitely yourcanvas2
won't render on your surface ever. When you create view it attached to window. At this point it has surface start drawing it gives youcanvas
ref provided byonDraw()
method. So how can your surface know you have created another canvas object....think about this. – Gru Jul 26 '13 at 9:00