There is an ImageMagnifier, which is a magnifying glass for ImageView:

public class ImageMagnifier extends android.support.v7.widget.AppCompatImageView { private PointF zoomPos; private boolean zooming = false; private Matrix matrix; private Paint paint; private Bitmap bitmap; private BitmapShader shader; private int sizeOfMagnifier = 200; public ImageMagnifier(Context context) { super(context); init(); } public ImageMagnifier(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public ImageMagnifier(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { zoomPos = new PointF(0, 0); matrix = new Matrix(); paint = new Paint(); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); zoomPos.x = event.getX(); zoomPos.y = event.getY(); switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: zooming = true; this.invalidate(); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: zooming = false; this.invalidate(); break; default: break; } return true; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (!zooming) { buildDrawingCache(); } else { bitmap = getDrawingCache(); shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); paint = new Paint(); paint.setShader(shader); matrix.reset(); matrix.postScale(2f, 2f, zoomPos.x, zoomPos.y); paint.getShader().setLocalMatrix(matrix); canvas.drawCircle(zoomPos.x, zoomPos.y - 150, sizeOfMagnifier, paint); } } } 

Everything works correctly, but I don’t know how to add a frame for a magnifying glass. Tried to do something like:

 paint.setStyle(Paint.Style.STROKE); paint.setColor(Color.BLACK); 

But in this case, an inversion is obtained: the enlarged image is displayed in a frame (which should be black), and the magnifying glass itself is empty.

    1 answer 1

    Your thoughts were correct, only for the frame, you need to create a separate Pain object and draw it after drawing the magnifying glass itself:

     ... canvas.drawCircle(zoomPos.x, zoomPos.y-150, sizeOfMagnifier, paint); Paint border = new Paint(); border.setStyle(Paint.Style.STROKE); border.setColor(Color.BLACK); canvas.drawCircle(zoomPos.x, zoomPos.y - 150, sizeOfMagnifier, border); 

    Only you should avoid creating new objects in the onDraw method, as Android Studio has probably already suggested to you. That is, create Paint objects when I initialize the view, and in onDraw , just reuse them.