简体   繁体   English

如何在Android中放大位图的接触点?

[英]How to magnify touch point of bitmap in android?

I'm trying to implement magnifier in my android application. 我正在尝试在Android应用程序中实现放大镜。 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. 这里的mouseDownX和mouseDownY是触摸坐标。 I'm not getting the exact area of bitmap where i touched. 我没有得到我触摸过的位图的确切区域。

BitmapShader 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(mouseDownX, mouseDownY, (int) (width * 0.10), paint);

please see How to magnify/zoom part of image 请参阅如何放大/缩放图像的一部分

Create your own Class which will extend ImageView class and Override the onTouchEvent and onTouchEvent methods. 创建您自己的类,该类将扩展ImageView类并覆盖onTouchEventonTouchEvent方法。 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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM