尝试在ImageView中获取图像的显示大小

我试图获得图像视图中显示的图像的实际大小。 其实我的图像比屏幕更大,imageview正在调整图像来显示它。 我正在寻找这个新的尺寸。

我试图覆盖自定义视图中ImageView的onDraw方法,但我没有得到正确的高度和宽度…

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

你有什么线索吗?

这里的答案都没有回答这个问题:

ImageView显示的任何尺寸的Bitmap中,find显示图像实际尺寸,而不是提供的Bitmap的尺寸。

即:

  • 使用ImageView.getDrawable().getInstrinsicWidth()getIntrinsicHeight()将返回原始尺寸。
  • 通过ImageView.getDrawable()获取Drawable并将其转换为BitmapDrawable ,然后使用BitmapDrawable.getBitmap().getWidth()getHeight()也返回原始图像及其尺寸。

获得显示图像的实际尺寸的唯一方法是提取并使用用于显示图像的变换Matrix ,如图所示。 这必须在测量阶段后完成,这里的示例显示了在自定义ImageViewonMeasure()Override中调用:

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

注意:为了从一般的代码中获得图像转换Matrix (就像在一个Activity ),函数是ImageView.getImageMatrix() – 例如myImageView.getImageMatrix()

我扩展了BT的答案,从中产生一个静态方法,并将图像左侧和顶部位置包含到ImageView中:

 /** * Returns the bitmap position inside an imageView. * @param imageView source ImageView * @return 0: left, 1: top, 2: width, 3: height */ public static int[] getBitmapPositionInsideImageView(ImageView imageView) { int[] ret = new int[4]; if (imageView == null || imageView.getDrawable() == null) return ret; // Get image dimensions // Get image matrix values and place them in an array float[] f = new float[9]; imageView.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 = imageView.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); ret[2] = actW; ret[3] = actH; // Get image position // We assume that the image is centered into ImageView int imgViewW = imageView.getWidth(); int imgViewH = imageView.getHeight(); int top = (int) (imgViewH - actH)/2; int left = (int) (imgViewW - actW)/2; ret[0] = left; ret[1] = top; return ret; } 

我发现WarrenFaith的build议使用setAdjustViewBounds工作,但我不得不将ImageView的layout_width / layout_height更改为'wrap_content'(与'match_parent',setAdjustViewBounds什么也没有)。 为了获得我想要的高度/宽度/重力行为,我必须将ImageView包装在FrameLayout中:

 <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:scaleType="fitCenter" android:adjustViewBounds="true" /> </FrameLayout> 

这样做后,ImageView的尺寸(由getWidth()和getHeight()返回)与图像的显示尺寸相匹配。

尝试

 ImageView.getDrawable().getIntrinsicHeight() ImageView.getDrawable().getIntrinsicWidth() 

我只是路过,但希望它仍然有助于我假设你谈论你的imageView位图

你想了解的是之间的区别

bmpBack.getWidth() – >这给你的位图和bmpBack.getScaledWidth(canvas)的大小; – >这会给你在屏幕上显示的位图的大小。

我从来没有使用过ImageView,因为相对的显示让我很生气,所以最后我只是重写了onDraw,而且和opengl非常类似。

我认为这是你的问题

干杯

贾森

尝试重写onMeasure(int widthMeasureSpec, int heightMeasureSpec)而不是onSizeChanged。

如果我正确地得到你,你需要你的ImageView维度,以相应地缩放你的图像。 我做了一个自定义的类,在那里我重写onMeasure()调用来获取宽度和高度。

 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 %dh %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 ); }} 

在onMeasure中,您可以获得图像的宽度和高度,以适合您的视图。

你可以像这样在你的布局中使用LandImageView:

 <my.package.LandImageView ... > 
 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); } } 

首先添加图像在你的资源文件夹…….和XML文件中创build一个图像视图….

我正在寻找解决scheme,将图像视图的尺寸设置为缩放的图像,以防止上/下或左/右的空白空间(导致视图的尺寸不会改变以适合缩放的图像)。

我发现要做的伎俩是使用方法mImageView.setAdjustViewBounds(true); 这导致正确的布局维度。 我没有缩放的图像尺寸,但我得到了我正在寻找的结果…只是如果有人需要它…

尝试使用BitmapFactory.Options()类使用BitmapFactory.decodeResource(Resources res,int id,BitmapFactory.Options opts)加载资源。 BitmapFactory.Options()有一个叫“inJustDecodeBounds”的标志,只给出资源维度。

希望有所帮助。

如何ImageView.getBackground()或ImageView.getDrawable(),然后Drawable.getBounds()?