3

Good Day Everyone

I was hoping if you could help me understand the concepts of understanding how to add an image into a canvas on a OnTouchEvent implemented on a View. So far, this is what i've come up with.
parent is the Activity where in this customized view is instantiated and is added into.

@Override
protected void onDraw(Canvas canvas)
{
    // TODO Auto-generated method stub
    super.onDraw(canvas);
}

public void insertImage()
{
    if (parent.selected_icon.contentEquals("image1"))
    {
        image = getResources().getDrawable(R.drawable.image1);
    }
    else if (parent.selected_icon.contentEquals("image1"))
    {
        image = getResources().getDrawable(R.drawable.image2);
    }
    else if (parent.selected_icon.contentEquals("iamge3"))
    {
        image = getResources().getDrawable(R.drawable.image3);
    }

    Rect srcRect = new Rect(0, 0, image.getIntrinsicWidth(), 
            image.getIntrinsicHeight());
    Rect dstRect = new Rect(srcRect);

    Bitmap bitmap = Bitmap.createBitmap(image.getIntrinsicWidth(), 
            image.getIntrinsicHeight(), Bitmap.Config.ALPHA_8);

    Canvas canvas = new Canvas();
    canvas.drawBitmap(bitmap, srcRect, dstRect, null);
    invalidate();
}
rekaszeru
  • 19,130
  • 7
  • 59
  • 73

1 Answers1

1

When you want to draw over a view, you have to do that in onDraw(), using the Canvas passed there. That Canvas is already bound to the Bitmap that is the actual drawing of your view.

I had to do something similar and my approach was like this:

  • I had a list of "things to be drawn over the view" as a member of the class.
  • whenever I added something to that list, I called invalidate(), so that onDraw() would get called.
  • My onDraw() looked like this:

...

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas); // the default drawing

    for(ThingToBeDrawn thing : mListOfThingsToBeDrawn) {
         thing.drawThing(canvas); // draw each thing over the view
    }
}

A Canvas is just a tool used to draw a Bitmap, and it works quite differently than SurfaceView.

bigstones
  • 15,087
  • 7
  • 65
  • 82
  • I agree totally, use the canvas.drawBitmap() inside of your onDraw() method. – Amplify91 Apr 28 '11 at 19:44
  • bigstones, Can you tell, please, how does the method invalidate() look like, and where to call it? Thanks in advance. – Yarik Feb 23 '13 at 16:59
  • I have a bitmap in MainActivity. have a custom view that extends View and yes it has an onDraw() method. But how am I supposed to get my bitmap variable in MainActivity to the custom view? – Daniel Viglione Jul 20 '17 at 18:25
  • @Donato just like you would do with any java class: get a reference to your custom view in the activity and use a setter like `customView.setMyBitmap(myBitmap)`, or pass it in the constructor if you're adding the view programmatically. Why the downvote? – bigstones Jul 22 '17 at 15:49
  • @bigstones you have to edit your answer so I can remove the downvote. I was just frustrated that I see questions that I have and answers that don't answer the question I thought was being asked. – Daniel Viglione Jul 22 '17 at 21:21