【问题标题】:How to get the top and left coordinate of Visible Bitmap inside ImageView in android如何在android中的ImageView中获取Visible Bitmap的顶部和左侧坐标
【发布时间】:2017-02-16 07:37:06
【问题描述】:

我正在开发 Android 应用的 Frame 多布局选择功能。

我的要求是缩放和捏合图像后获取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);

请让我知道我做错了什么,因为我从过去 3 天开始一​​直在努力,但仍未完成。

提前致谢。 非常感谢您的建议。

【问题讨论】:

  • get img.getDrawingCache() 你只会看到。在此之前添加img.setDrawingCacheEnabled(true);
  • 第一次尝试使用Matrix.postScale(yourScaleFactor, yourScaleFactor)
  • 您正在按矩阵调整图像大小。在postScale 图像获得新坐标后,我认为 matrix.mapRect 会帮助你。使用图像矩阵获取视图 rect 和映射,然后左侧将指向您预期的 x。
  • @HarisQureshi 感谢您的快速评论,您能告诉我如何获得“yourScaleFactor”吗?
  • @Qamar 我已经尝试过 img.getDrawingCache( ) 但这样我只得到位图(编辑后的最终位图,但 不是我原始位图的坐标)也请分享一些与 matrix.mapRect 相关的代码?

标签: android bitmap imageview android-imageview android-bitmap


【解决方案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);
    }

【讨论】:

  • 谢谢你,但我得到了这个 - RectF(0.90758044, 0.0, 0.9993077, 0.33333334)
  • 还有如何通过 RectF 重绘该图像?
  • 查看此方法文档。 Canvas.drawBitmap(Bitmap, Rect, RectF, Paint)
猜你喜欢
  • 2012-02-11
  • 2022-11-22
  • 2014-02-02
  • 1970-01-01
  • 1970-01-01
  • 2012-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多