![](/img/trans.png)
[英]Android android.widget.ImageView setCropToPadding() get around
[英]How to get the width and height of an android.widget.ImageView?
╔══════════════════════════════════════════════╗ ^
║ ImageView ╔══════════════╗ ║ |
║ ║ ║ ║ |
║ ║ Actual image ║ ║ |
║ ║ ║ ║ |60px height of ImageView
║ ║ ║ ║ |
║ ║ ║ ║ |
║ ╚══════════════╝ ║ |
╚══════════════════════════════════════════════╝
<------------------------------------------------>
90px width of ImageView
我有一个具有默认高度和宽度的图像视图,图像存储在db中,并且我想根据Imageview的高度宽度缩放Image。 因为我不希望它提供默认值,因为每当我更改它的高度和宽度时,我也必须在代码中对其进行更改。
我正在尝试获取ImageView的高度和宽度,但是在两种情况下都返回0。
int height = ((ImageView) v.findViewById(R.id.img_ItemView)).getHeight();
这将返回0,即使它具有默认的高度和宽度
我对这个问题的回答可能会帮助您:
int finalHeight, finalWidth;
final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
final TextView tv = (TextView)findViewById(R.id.size_label);
ViewTreeObserver vto = iv.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
iv.getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = iv.getMeasuredHeight();
finalWidth = iv.getMeasuredWidth();
tv.setText("Height: " + finalHeight + " Width: " + finalWidth);
return true;
}
});
然后,您可以从onPreDraw()方法中添加图像缩放工作。
我只是设置了此属性,现在Android OS正在处理所有事情。
android:adjustViewBounds =“ true”
在您已放置ImageView的layout.xml中使用它:D
我可以通过可绘制图像获得图像的宽度和高度;
int width = imgView.getDrawable().getIntrinsicWidth();
int height = imgView.getDrawable().getIntrinsicHeight();
ImageView的尺寸为0的原因是因为在查询它们时,视图仍未执行布局和测量步骤。 您仅告诉视图在布局中的“行为”,但仍未计算放置每个视图的位置。
您如何确定要赋予图像视图的尺寸? 您不能简单地使用本机实现的缩放选项之一吗?
发布到UI线程对我有用。
final ImageView iv = (ImageView)findViewById(R.id.scaled_image);
iv.post(new Runnable() {
@Override
public void run() {
int width = iv.getMeasuredWidth();
int height = iv.getMeasuredHeight();
}
});
您的xml文件:
<ImageView android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image"
android:scaleType="fitXY"
android:adjustViewBounds="true"/>
您的Java文件:
ImageView imageView = (ImageView)findViewById(R.id.imageView);
int width = imageView.getDrawable().getIntrinsicWidth();
int height = imageView.getDrawable().getIntrinsicHeight();
我认为您可以让Android操作系统为您解决此问题。 将ImageView上的比例类型设置为fitXY
,它将显示的图像的大小调整为适合视图的当前大小。
<ImageView
android:layout_width="90px"
android:layout_height="60px"
android:scaleType="fitXY" />
最简单的方法是在活动的onWindowFocusChanged方法中获取ImageView的宽度和高度
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
height = mImageView.getHeight();
width = mImageView.getWidth();
}
如果您动态创建了多个图像,则可以尝试以下操作:
// 初始化图片数组
private ImageView myImages[] = new ImageView[your_array_length];
// 以编程方式创建并添加到父视图
for (int i = 0; i < your_array_length; i++) {
myImages[i] = new ImageView(this);
myImages[i].setId(i + 1);
myImages[i].setBackgroundResource(your_array[i]);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
frontWidth[i], frontHeight[i]);
((MarginLayoutParams) params).setMargins(frontX_axis[i],
frontY_axis[i], 0, 0);
myImages[i].setAdjustViewBounds(true);
myImages[i].setLayoutParams(params);
if (getIntent() != null && i != your_array,length) {
final int j = i;
myImages[j].getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
myImages[j].getViewTreeObserver().removeOnPreDrawListener(this);
finalHeight = myImages[j].getMeasuredHeight();
finalWidth = myImages[j].getMeasuredWidth();
your_textview.setText("Height: " + finalHeight + " Width: " + finalWidth);
return true;
}
});
}
your_parent_layout.addView(myImages[i], params);
}
// 而已。 编码愉快。
我的朋友因此你没有得到存储在db中的图像的高度,但是你得到了视图的高度。要获得图像的高度,你必须从db,s的图像中创建位图。然后,你可以获取imageview的高度和宽度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.