问题描述
我正在研究Android应用程序的框架多重布局选择功能。
我的要求是缩放并捏住图像后获取Bitmap的顶部和左侧坐标。
我将TouchImageView用于缩放和移动功能。
我尝试了很多SO解决方案,但没有得到我要求的解决方案。
第一次尝试-
public static int[] getBitmapOffset(ImageView img, boolean includeLayout) {
        int[] offset = new int[2];
        float[] values = new float[9];
        Matrix m = img.getImageMatrix();
        m.getValues(values);
        offset[0] = (int) values[5];
        offset[1] = (int) values[2];
        if (includeLayout) {
            ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams) img.getLayoutParams();
            int paddingTop = (int) (img.getPaddingTop() );
            int paddingLeft = (int) (img.getPaddingLeft() );
            offset[0] += paddingTop + lp.topMargin;
            offset[1] += paddingLeft + lp.leftMargin;
        }
        return offset;
    }
第二次尝试-
            Rect r = img.getDrawable().getBounds();
            int drawLeft = r.left;
            int drawTop = r.top;
            int drawRight = r.right;
            int drawBottom = r.bottom;
            Logger.logsInfo(TAG, "Focus drawLeft : " + i + " : " +drawLeft);
            Logger.logsInfo(TAG, "Focus drawTop : " + i + " : " +drawTop);
            Logger.logsInfo(TAG, "Focus drawRight : " + i + " : " +drawRight);
            Logger.logsInfo(TAG, "Focus drawBottom : " + i + " : " +drawBottom);
请让我知道我做错了什么,因为从最近三天开始我一直在为此工作,但仍未完成。
提前致谢。 建议真的很感激。
1楼
 
     查看getZoomedRect的TouchImageView 
 
     添加函数并返回PointF topLeft = transformCoordTouchToBitmap(0, 0, true); 
方法签名
private PointF transformCoordTouchToBitmap(float x, float y, boolean clipToBitmap)
 
     通过clipToBitmap true表示可见矩形 
/**
     * Return a Rect representing the zoomed image.
     * @return rect representing zoomed image
     */
    public RectF getZoomedRect() {
        if (mScaleType == ScaleType.FIT_XY) {
            throw new UnsupportedOperationException("getZoomedRect() not supported with FIT_XY");
        }
        PointF topLeft = transformCoordTouchToBitmap(0, 0, true);
        PointF bottomRight = transformCoordTouchToBitmap(viewWidth, viewHeight, true);
        float w = getDrawable().getIntrinsicWidth();
        float h = getDrawable().getIntrinsicHeight();
        return new RectF(topLeft.x / w, topLeft.y / h, bottomRight.x / w, bottomRight.y / h);
    }