2017-02-16 93 views
0

我正在研究Android应用程序的Frame multiple layout selection特性。如何获取ImageView中的可见位图的顶部和左侧坐标android

我的要求是在缩放和缩小图像后获取位图的顶部和左侧坐标。

我正在使用TouchImageView进行缩放和移动功能。

我已经尝试了很多SO解决方案,但没有得到我的要求的解决方案。

1日试 -

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); 

请让我知道我做错了,因为我对这个从过去三天的工作,但仍不能完成。

在此先感谢。 建议非常感谢。退房getZoomedRect

+0

得到'img.getDrawingCache() '你会继续可见。在此之前添加'img.setDrawingCacheEnabled(true);' – Qamar

+0

在你的第一次尝试中,你是否尝试过使用'Matrix.postScale(yourScaleFactor,yourScaleFactor)'? –

+0

您正在按矩阵调整图像大小。在'postScale'图像获得新的坐标之后,我认为matrix.mapRect会帮助你。获取视图矩形和映射矩阵,然后离开将指向您预期的x。 – Qamar

回答

0

TouchImageView

附加功能和返回PointF topLeft = transformCoordTouchToBitmap(0, 0, true);

方法签名

private PointF transformCoordTouchToBitmap(float x, float y, boolean clipToBitmap) 

传递clipToBitmap真实均值可见矩形

/** 
    * 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); 
    } 
+0

感谢这一点,但我越来越 - RectF(0.90758044,0.0,0.99993077,0.333333334) –

+0

而且我怎样才能通过RectF重新绘制图像? –

+0

查看此方法文档。 Canvas.drawBitmap(Bitmap,Rect,RectF,Paint) – Qamar

相关问题