A few days later, trying to solve this problem, thanks to those answers above and other sources, I was able to write a full working code.
To save time for all of you guys, here it is. Copy-paste and edit it a little to fit your goals.
First of all, you need to edit the main action - we will use a custom ImageView
<your.package.name.ZoomView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
Then we will edit MainActivity.java. Use ZoomView as ImageView
ImageView takenPhoto; takenPhoto = (ZoomView) findViewById(R.id.imageView1);
After that create the ZoomView class and paste this:
public class ZoomView extends ImageView { PointF zoomPos; boolean zooming; Matrix matrix; BitmapShader mShader; Paint mPaint; public ZoomView(Context context) { super(context); } public ZoomView(Context context, AttributeSet attrs) { super(context, attrs); } public ZoomView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onTouchEvent(@NonNull MotionEvent event) { zoomPos = new PointF(); zoomPos.x = event.getX(); zoomPos.y = event.getY(); matrix = new Matrix(); mShader = new BitmapShader(MainActivity.mutableBitmap, TileMode.CLAMP, TileMode.CLAMP); mPaint = new Paint(); mPaint.setShader(mShader); int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: zooming = true; this.invalidate(); break; case MotionEvent.ACTION_UP: zooming = false; this.invalidate(); break; case MotionEvent.ACTION_CANCEL: zooming = false; this.invalidate(); break; default: break; } return true; } @Override protected void onDraw(@NonNull Canvas canvas) { super.onDraw(canvas); if (zooming) { matrix.reset(); matrix.postScale(2f, 2f, zoomPos.x, zoomPos.y); mPaint.getShader().setLocalMatrix(matrix); canvas.drawCircle(zoomPos.x, zoomPos.y, 100, mPaint); } } }
You should now have a working magnifying effect.
source share