Reputation: 53
I want to draw a rectangle on a button click. All the research I've done says to me that this should work, but it doesn't. I simply can't understand why.
In my MainActivity class I have:
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
public Canvas canvas = new Canvas(b);
In my constructor I have:
button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Paint paint = new Paint();
paint.setColor(Color.GREEN);
//paint.setStrokeWidth(5);
canvas.drawRect(0, 0, 50, 50, paint);
Log.e("Blah Blah Blah", "Blah Blah, Blah");
}
});
It does go into the function, which I see because when I click it the "Blah Blah Blah" is logged, but it doesn't draw the rectangle.
Any ideas?
Upvotes: 0
Views: 1162
Reputation: 39577
You need to set your canvas to an ImageView
//Create a new image bitmap and attach a brand new canvas to it
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565);
Canvas tempCanvas = new Canvas(tempBitmap);
//Draw the image bitmap into the cavas
tempCanvas.drawBitmap(myBitmap, 0, 0, null);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
//paint.setStrokeWidth(5);
tempCanvas.drawRoundRect(new RectF(x1,y1,x2,y2), 2, 2, paint);
//Attach the canvas to the ImageView
myImageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
Upvotes: 1