Trying to get the display size of an image in an I

2019-01-03 23:15发布

I'm trying to get the real size of an image displayed in an image view. Actually my image is larger than the screen and the imageview is resizing the image to diplay it. I'm looking for this new size.

I've tried to override the onDraw method of the ImageView in a custom view but I'm not getting the correct height and width...

public class LandImageView extends ImageView
{
    public LandImageView( Context context )
    {
        super( context );
    }

    public LandImageView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
    }

    public LandImageView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw( Canvas canvas )
    {
        super.onDraw( canvas );

        int test = this.getWidth();
        int test2 = this.getHeight();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
    }
}

Do you have any clues ?

13条回答
你好瞎i
2楼-- · 2019-01-03 23:42

None of the answers here actually answer the question:

From a Bitmap of any size displayed by an ImageView, find the actual dimensions of the displayed image as opposed to the dimensions of the supplied Bitmap.

Namely:

  • Using ImageView.getDrawable().getInstrinsicWidth() and getIntrinsicHeight() will both return the original dimensions.
  • Getting the Drawable through ImageView.getDrawable() and casting it to a BitmapDrawable, then using BitmapDrawable.getBitmap().getWidth() and getHeight() also returns the original image and its dimensions.

The only way to get the actual dimensions of the displayed image is by extracting and using the transformation Matrix used to display the image as it is shown. This must be done after the measuring stage and the example here shows it called in an Override of onMeasure() for a custom ImageView:

public class SizeAwareImageView extends ImageView {

    public SizeAwareImageView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // Get image matrix values and place them in an array
        float[] f = new float[9];
        getImageMatrix().getValues(f);

        // Extract the scale values using the constants (if aspect ratio maintained, scaleX == scaleY)
        final float scaleX = f[Matrix.MSCALE_X];
        final float scaleY = f[Matrix.MSCALE_Y];

        // Get the drawable (could also get the bitmap behind the drawable and getWidth/getHeight)
        final Drawable d = getDrawable();
        final int origW = d.getIntrinsicWidth();
        final int origH = d.getIntrinsicHeight();

        // Calculate the actual dimensions
        final int actW = Math.round(origW * scaleX);
        final int actH = Math.round(origH * scaleY);

        Log.e("DBG", "["+origW+","+origH+"] -> ["+actW+","+actH+"] & scales: x="+scaleX+" y="+scaleY);
    }

}  

Note: To get the image transformation Matrix from code in general (like in an Activity), the function is ImageView.getImageMatrix() - e.g. myImageView.getImageMatrix()

查看更多
爷的心禁止访问
3楼-- · 2019-01-03 23:44

If I get you correctly you need your ImageView dimension, in order to scale your image accordingly. I did this with a custom class, where I override the onMeasure() call to get width and height.

class LandImageView extends ImageView{ 
public LandImageView (Context context, AttributeSet attrs) {
  super (context, attrs);
 } 
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
 {
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);

  final int width = MeasureSpec.getSize(widthMeasureSpec);
  final int height = MeasureSpec.getSize(heightMeasureSpec);

  Log.v("", String.format("w %d h %d", width, height));
                // width and height are the dimensions for your image
                // you should remember the values to scale your image later on

  this.setMeasuredDimension(width, height );
 }}

In onMeasure you get the width and height for your image so that it fits your view.

You can use the LandImageView in your Layout like this:

<my.package.LandImageView ... >
查看更多
Summer. ? 凉城
4楼-- · 2019-01-03 23:44

I was looking for a solution to set the dimension of the image view to the scaled image to prevent empty space on top/bottom or left/right (cause the dimension of the view doesn't changed to fit the scaled image).

What I found to do the trick was using the method mImageView.setAdjustViewBounds(true); which results in the correct layout dimension. I don't have the scaled image dimension but I got the result I was looking for... just if someone needs it...

查看更多
在下西门庆
5楼-- · 2019-01-03 23:45
public class images extends Activity {
ImageView i1;
LinearLayout l1;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     i1=(ImageView)findViewById(R.id.iv);
     l1 = new LinearLayout(this);
    ImageView i = new ImageView(this);
    ImageView i1 = new ImageView(this);
    i.setImageResource(R.drawable.  imagename.........);

    l1.addView(i);
    l1.addView(i1);
    setContentView(l1);
    }
}

first add the images in ur resource folder....... and in xml file create a image view....

查看更多
太酷不给撩
6楼-- · 2019-01-03 23:48

try overriding onMeasure(int widthMeasureSpec, int heightMeasureSpec) instead of onSizeChanged.

查看更多
时光不老,我们不散
7楼-- · 2019-01-03 23:48

How about ImageView.getBackground() or ImageView.getDrawable(), then Drawable.getBounds()?

查看更多
登录 后发表回答