android - how to get the image edge x/y position inside imageview

前端 未结 4 839
不知归路
不知归路 2020-12-23 15:01

If i have an ImageView that fills the screen.
The ImageView background is set to green color.
I place a bitmap in the ImageView, keeping bitmap proportions.

4条回答
  •  孤独总比滥情好
    2020-12-23 15:48

    Here is a method I wanted to share, it will return the offset of the bitmap inside an imageView using getImageMatrix

    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[Matrix.MTRANS_Y];
            offset[1] = (int) values[Matrix.MTRANS_X];
    
            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;
        }
    

提交回复
热议问题