【发布时间】: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