Java Question How to share/expose a Canvas?

Jim Brown

Active Member
Licensed User
Longtime User
In the same way that the Canvas1.Initialise() can obtain a Views background I want to implement this into my library too but I don't know how to share or allow access. See code comment.

Libray code example
B4X:
public class MyViewWrapper extends ViewWrapper<MyiewWrapper.MyPanel> {
...
public class MyPanel extends View {
        public MyPanel(Context context) {
            super(context);
        }
        @Override
        protected void onDraw(Canvas c) {
         ...
        }
}

B4A code example
B4X:
Dim v as MyView
Dim canvas1 as Canvas
...
v.Initialise("blah")
canvas1.Initialise(MyView) ' ** How to implement this part?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
public void Initialize(View Target) {
     paint = new Paint();
     LayoutParams lp = Target.getLayoutParams();
     Bitmap bitmap = Bitmap.createBitmap(lp.width , lp.height , Config.ARGB_8888);
     BitmapDrawable bd = new BitmapDrawable(bitmap);
     canvas = new Canvas(bitmap);
     if (Target.getBackground() != null) {
       Target.getBackground().setBounds(0, 0, lp.width, lp.height);
       Target.getBackground().draw(canvas);
     }
     Target.setBackgroundDrawable(bd);
     bw = new BitmapWrapper();
     bw.setObject(bitmap);

   }
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…