Drawing shapes on changing image in android
I need to draw shapes on an image in my layout. This image needs to be able 开发者_StackOverflowto change to another image programically, and I also need to draw shapes (rectangles and circles) over top of this image programically. The shapes will also change. I have an existing xml layout and would like to use this layout with the programmed image view in it. What's the easiest way to do this? Would it be possible to see a short example?
I figured out how to do this:
Here's how:
ImageView image = (ImageView) findViewById(R.id.mainImageView);
Bitmap bMap = BitmapFactory.decodeFile(imageFileString);
bMap = bMap.copy(Bitmap.Config.ARGB_8888 , true);
Canvas canvas = new Canvas(bMap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.BLUE);
canvas.drawCircle(x, y, radius, paint);
image.setImageBitmap(bMap);
Explanation: The first line gets the ImageView
from the layout. I then grab the new image I want displayed using BitmapFactory.decodeFile
, where imageFileString
is the file location of the new image I want to display. After that I create a new canvas using the bitmap and draw on it. I then display the bitmap to the ImageView
using image.setImageBitmap(bMap);
.
精彩评论