This is a topic for which I found on the Internet a lot of ideas but they were more workarounds for special situations than general solutions.
One of my apps contained an ImageView widget into which I had to load a series of pictures one after the other at runtime. The pictures were of different size, some smaller and some larger than the widget’s size, so the images would either be centered or automatically scaled down by the widget. The challenge was to mark certain areas in the picture with a coloured rectangle dynamically created at run time and adjusted using the coordinates of the original, unscaled image.
The general recommendation on the Internet was to subclass ImageView and overwrite OnDraw with a method that first calls the original OnDraw method of ImageView with the canvas that is provided as parameter to OnDraw and then draws into this canvas on top of the already drawn image.
I am meanwhile quite sure that this works only under certain circumstances, for example when you are sure, your image will not be scaled. The problem lies in the provided canvas. The original onDraw method applies a certain transformation matrix to the canvas before it draws the image into the canvas and restores the canvas in its previous state afterwards. This matrix is stored in a private member variable of ImageView and for whatever reason it is not always the same matrix as the one returned from getImageMatrix() calls. Using the debugger I have seen cases where getImageMatrix returned a non-null matrix where onDraw used a null Matrix and therefore did no do any transformation to the canvas at all. So there is no general reliable way to calculate the coordinates for anything you want to draw on top of what ImageView has drawn.
Therefore I used a different approach:
- Create a new image bitmap and attach a brand new canvas to it so that the bitmap and the canvas use the same coordinate system.
- Draw the image bitmap into the canvas.
- Draw everything else you want into the canvas (the rectangles in my case).
- Attach the canvas to the ImageView.
Here is the code snippet:
import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.drawable.BitmapDrawable; ImageView myImageView = ... Bitmap myBitmap = ... Paint myRectPaint = ... int x1 = ... int y1 = ... int x2 = ... int y2 = ... //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); //Draw everything else you want into the canvas, in this example a rectangle with rounded edges tempCanvas.drawRoundRect(new RectF(x1,y1,x2,y2), 2, 2, myPaint); //Attach the canvas to the ImageView myImageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));