How to create and display a bitmap on a view widget in Android?

Question:

Hello. I decided to write a program for Android in Java.
There was a problem, it is required on a layer (layuot) with widgets (for example, with two buttons), to draw an image that, when clicked on it, moved to the clicked place, I just can't figure out how to organize this. I dig in the direction of the view object, I have already sketched the following code:

 public void onResume(Bundle savedInstanceState){
        Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        c.drawBitmap(b, 10, 10, null);
        view1 = (View)findViewById(R.id.view1);
        view1.draw(c);
    }

The code works, but the image does not appear, although there is essentially nothing to appear.
Thanks in advance!

Answer:

You can override the onDraw method of your view and draw to the Canvas in it. Like that:

public class SomeView extends View{
    // TODO: Дополнительный код: конструкторы и тд.

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Bitmap bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888);
        Canvas over = new Canvas(bitmap);

        // TODO: рисовать на over

        canvas.drawBitmap(bitmap, 0, 0, null);
    }
}
Scroll to Top
AllEscort