如何在安卓系统中将图像缩放到指定坐标?

12
我想将图片放大到图像的特定部分(指定坐标)。我有一张图片,我在Android图片视图中全屏显示它。当我点击一个按钮时,我想将图像缩放到指定的图像坐标。比如,我有left:500,top:50,width:60和height:20的坐标。我希望整张图片缩放到指定坐标并将此子图像适配到图像视图的中心。
目前我是通过裁剪图像到指定的坐标来实现的。我会得到一个小的图像。然后我会在ImageView中展示它。但我认为这不是一个很好的解决方案。
有人能帮我找到实现缩放功能的方法吗?

1
可能会有帮助:https://dev59.com/blbTa4cB1Zd3GeqP7AzF - Niko
谢谢Niko,你似乎建议裁剪图像。但我不需要裁剪图像。我只是将图像缩放到给定的坐标。 - sathish
你找到解决方案了吗?请帮忙。 - MinnuKaAnae
你能找到任何解决方案吗? - cgr
2个回答

0
也许这就是你正在寻找的内容:
/**
 * Set zoom to the specified scale. Image will be centered around the point
 * (focusX, focusY). These floats range from 0 to 1 and denote the focus point
 * as a fraction from the left and top of the view. For example, the top left 
 * corner of the image would be (0, 0). And the bottom right corner would be (1, 1).
 * @param scale
 * @param focusX
 * @param focusY
 * @param scaleType
 */
public void setZoom(float scale, float focusX, float focusY, ScaleType scaleType) {
    //
    // setZoom can be called before the image is on the screen, but at this point, 
    // image and view sizes have not yet been calculated in onMeasure. Thus, we should
    // delay calling setZoom until the view has been measured.
    //
    if (!onDrawReady) {
        delayedZoomVariables = new ZoomVariables(scale, focusX, focusY, scaleType);
        return;
    }

    if (scaleType != mScaleType) {
        setScaleType(scaleType);
    }
    resetZoom();
    scaleImage(scale, viewWidth / 2, viewHeight / 2, true);
    matrix.getValues(m);
    m[Matrix.MTRANS_X] = -((focusX * getImageWidth()) - (viewWidth * 0.5f));
    m[Matrix.MTRANS_Y] = -((focusY * getImageHeight()) - (viewHeight * 0.5f));
    matrix.setValues(m);
    fixTrans();
    setImageMatrix(matrix);
}

更多信息请参考:

这个 Github 链接可以帮助你更多

在此输入链接描述

编辑:

请参考以下内容,你会得到答案:

你问题的答案


是的,我看过这段代码。如果我没记错的话,“scale”参数决定了缩放级别。但是我需要找到那个缩放(比例尺)。你觉得呢? - cgr
根据你的问题,上面的答案是正确的...现在在评论中,你又问了一个不同的问题,嘿嘿。 - Raut Darpan
那不对。请理解我的评论——这个方法以比例尺(缩放)作为参数,这正是我想用坐标来计算的。如果您认为我还没有理解,请再解释一下。非常感谢。 - cgr
我会尝试明天解释,现在我没有笔记本电脑。 - Raut Darpan

0
使用此自定义 ImageView 类来设置您的 ImageView。如果您想保留 Pinch Zoom,则也可以使用它,否则请从中删除所有手势。您可以在下面提供的类中看到“双击方法”。只需在单击按钮时调用此方法代码即可。
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.ImageView;

public class TouchImageView extends ImageView {

Matrix matrix = new Matrix();

// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;

// Remember some things for zooming
PointF last = new PointF();
PointF start = new PointF();
float minScale = 1f;
float maxScale = 3f;
float[] m;

float redundantXSpace, redundantYSpace, origRedundantXSpace, origRedundantYSpace;;

float width, height;
static final int CLICK = 3;
static final float SAVE_SCALE = 1f;
float saveScale = SAVE_SCALE;

float right, bottom, origWidth, origHeight, bmWidth, bmHeight, origScale, origBottom,origRight;

ScaleGestureDetector mScaleDetector;
GestureDetector mGestureDetector;

Context context;

public TouchImageView(Context context) {
    super(context);
    super.setClickable(true);
    this.context = context;
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());

    matrix.setTranslate(1f, 1f);
    m = new float[9];
    setImageMatrix(matrix);
    setScaleType(ScaleType.MATRIX);

    setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            boolean onDoubleTapEvent = mGestureDetector.onTouchEvent(event);
            if (onDoubleTapEvent) {
                // Reset Image to original scale values
                mode = NONE;
                bottom = origBottom;
                right = origRight;
                last = new PointF();
                start = new PointF();
                m = new float[9];
                saveScale = SAVE_SCALE;
                matrix = new Matrix();
                matrix.setScale(origScale, origScale);
                matrix.postTranslate(origRedundantXSpace, origRedundantYSpace);
                setImageMatrix(matrix);
                invalidate();
                return true;
            } 


            mScaleDetector.onTouchEvent(event);

            matrix.getValues(m);
            float x = m[Matrix.MTRANS_X];
            float y = m[Matrix.MTRANS_Y];
            PointF curr = new PointF(event.getX(), event.getY());

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                last.set(event.getX(), event.getY());
                start.set(last);
                mode = DRAG;
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    float deltaX = curr.x - last.x;
                    float deltaY = curr.y - last.y;
                    float scaleWidth = Math.round(origWidth * saveScale);
                    float scaleHeight = Math.round(origHeight * saveScale);
                    if (scaleWidth < width) {
                        deltaX = 0;
                        if (y + deltaY > 0)
                            deltaY = -y;
                        else if (y + deltaY < -bottom)
                            deltaY = -(y + bottom);
                    } else if (scaleHeight < height) {
                        deltaY = 0;
                        if (x + deltaX > 0)
                            deltaX = -x;
                        else if (x + deltaX < -right)
                            deltaX = -(x + right);
                    } else {
                        if (x + deltaX > 0)
                            deltaX = -x;
                        else if (x + deltaX < -right)
                            deltaX = -(x + right);

                        if (y + deltaY > 0)
                            deltaY = -y;
                        else if (y + deltaY < -bottom)
                            deltaY = -(y + bottom);
                    }
                    matrix.postTranslate(deltaX, deltaY);
                    last.set(curr.x, curr.y);
                }
                break;

            case MotionEvent.ACTION_UP:
                mode = NONE;
                int xDiff = (int) Math.abs(curr.x - start.x);
                int yDiff = (int) Math.abs(curr.y - start.y);
                if (xDiff < CLICK && yDiff < CLICK)
                    performClick();
                break;

            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                break;
            }

            setImageMatrix(matrix);
            invalidate();

            return true; // indicate event was handled
        }

    });

    mGestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return true;
        }
    });
}

@Override
public void setImageBitmap(Bitmap bm) {
    super.setImageBitmap(bm);
    bmWidth = bm.getWidth();
    bmHeight = bm.getHeight();
}

public void setMaxZoom(float x) {
    maxScale = x;
}

private class ScaleListener extends
        ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
        mode = ZOOM;
        return true;
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        float mScaleFactor = (float) Math.min(
                Math.max(.95f, detector.getScaleFactor()), 1.05);
        float origScale = saveScale;
        saveScale *= mScaleFactor;
        if (saveScale > maxScale) {
            saveScale = maxScale;
            mScaleFactor = maxScale / origScale;
        } else if (saveScale < minScale) {
            saveScale = minScale;
            mScaleFactor = minScale / origScale;
        }
        right = width * saveScale - width
                - (2 * redundantXSpace * saveScale);
        bottom = height * saveScale - height
                - (2 * redundantYSpace * saveScale);
        if (origWidth * saveScale <= width
                || origHeight * saveScale <= height) {
            matrix.postScale(mScaleFactor, mScaleFactor, width / 2,
                    height / 2);
            if (mScaleFactor < 1) {
                matrix.getValues(m);
                float x = m[Matrix.MTRANS_X];
                float y = m[Matrix.MTRANS_Y];
                if (mScaleFactor < 1) {
                    if (Math.round(origWidth * saveScale) < width) {
                        if (y < -bottom)
                            matrix.postTranslate(0, -(y + bottom));
                        else if (y > 0)
                            matrix.postTranslate(0, -y);
                    } else {
                        if (x < -right)
                            matrix.postTranslate(-(x + right), 0);
                        else if (x > 0)
                            matrix.postTranslate(-x, 0);
                    }
                }
            }
        } else {
            matrix.postScale(mScaleFactor, mScaleFactor,
                    detector.getFocusX(), detector.getFocusY());
            matrix.getValues(m);
            float x = m[Matrix.MTRANS_X];
            float y = m[Matrix.MTRANS_Y];
            if (mScaleFactor < 1) {
                if (x < -right)
                    matrix.postTranslate(-(x + right), 0);
                else if (x > 0)
                    matrix.postTranslate(-x, 0);
                if (y < -bottom)
                    matrix.postTranslate(0, -(y + bottom));
                else if (y > 0)
                    matrix.postTranslate(0, -y);
            }
        }
        return true;

    }
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    width = MeasureSpec.getSize(widthMeasureSpec);
    height = MeasureSpec.getSize(heightMeasureSpec);
    // Fit to screen.
    float scale;
    float scaleX = (float) width / (float) bmWidth;
    float scaleY = (float) height / (float) bmHeight;
    scale = Math.min(scaleX, scaleY);
    matrix.setScale(scale, scale);
    setImageMatrix(matrix);
    saveScale = SAVE_SCALE;
    origScale = scale;

    // Center the image
    redundantYSpace = (float) height - (scale * (float) bmHeight);
    redundantXSpace = (float) width - (scale * (float) bmWidth);
    redundantYSpace /= (float) 2;
    redundantXSpace /= (float) 2;

    origRedundantXSpace = redundantXSpace;
    origRedundantYSpace = redundantYSpace;

    matrix.postTranslate(redundantXSpace, redundantYSpace);

    origWidth = width - 2 * redundantXSpace;
    origHeight = height - 2 * redundantYSpace;
    right = width * saveScale - width - (2 * redundantXSpace * saveScale);
    bottom = height * saveScale - height
            - (2 * redundantYSpace * saveScale);
    origRight = right;
    origBottom = bottom;
    setImageMatrix(matrix);
}

}

希望这能有所帮助!


你是指 if (onDoubleTapEvent) 下的代码吗?如果是的话,我认为它没有通过获取焦点点(TRANS_X、TRANS_Y)和/或图像的宽度和高度(以像素为单位)来计算比例/缩放因子。 - cgr
你需要在 "setScale()" 方法中为 sx 和 sy 指定特定的值,以便缩放图像到你想要的大小。matrix.setScale(x 坐标, y 坐标); - Zohaib Hassan
谢谢回复。您的意思是保持设置比例直到我缩放到想要缩放的坐标?我也在考虑这个问题,但是我们如何确定坐标现在是否在视图内,从而停止缩放呢? 此外,我想缩放的坐标可能在视图内,但我想缩小视图,使其适合视图并正确可见。请查看问题下面添加的赏金评论。 - cgr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接