Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to magnify touch point of bitmap in android?

I'm trying to implement magnifier in my android application. On touching a bitmap it should be zoomed to some ratio and showed aside.I'm not getting the exact bitmap region where i touched on bitmap.Can anyone give me a solution.This is my code

Bitmap bitmap = new BitmapShader(modelBitmap, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
matrix1.postScale(2f, 2f, mouseDownX, mouseDownY);
bitmap.setLocalMatrix(matrix1);
paint.setShader(bitmap);
canvas.drawCircle(60,230, (int) (width * 0.10), paint);

Here mouseDownX and mouseDownY are touch co-ordinates. I'm not getting the exact area of bitmap where i touched.

like image 576
user3305353 Avatar asked Dec 06 '25 03:12

user3305353


1 Answers

Create your own Class which will extend ImageView class and Override the onTouchEvent and onTouchEvent methods. Get the tapped location and draw the touched section on canvas in any shape you want.

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Shader;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;


public class CodesforMagnifierImageView extends AppCompatImageView
{

    private PointF zoomPosition;
    private boolean zooming = false;
    private Matrix matrix;
    private Paint paint;
    private Bitmap bitmap;
    private BitmapShader shader;
    private int sizeOfMagnifier = 200;

    public CodesforMagnifierImageView(Context context)
    {
        super(context);
        init();
    }

    public CodesforMagnifierImageView(Context context, AttributeSet attrs, int defStyleAttr)
    {
        super(context, attrs, defStyleAttr);
        init();
    }

    public CodesforMagnifierImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    private void init()
    {
        zoomPosition = new PointF(0, 0);
        matrix = new Matrix();
        paint = new Paint();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event)
    {
        int action = event.getAction();

        zoomPosition.x = event.getX();
        zoomPosition.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, zoomPosition.x, zoomPosition.y);
            paint.getShader().setLocalMatrix(matrix);
            canvas.drawCircle(zoomPosition.x, zoomPosition.y, sizeOfMagnifier, paint);
        }
    }


}

Use it in following way:

<com.utilities.CodesforMagnifierImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image" />
like image 150
Akshay Avatar answered Dec 08 '25 16:12

Akshay