╔══════════════════════════════════════════════╗ ^
║ ImageView ╔══════════════╗ ║ |
║ ║ ║ ║ |
║ ║ Actual image ║ ║ |
║ ║ ║ ║ |60px height of ImageView
║ ║ ║ ║ |
║ ║ ║ ║ |
║ ╚══════════════╝ ║ |
╚══════════════════════════════════════════════╝
<------------------------------------------------>
90px width of ImageView
我有一个图像视图,一些默认的高度和宽度,图像存储在db和我想缩放图像根据Imageview的高度和宽度。因为我不想让它给出默认值,因为当我改变它的高度和宽度时,我也必须在代码中改变它。
我试图获得ImageView的高度和宽度,但在这两种情况下都返回0。
int height = ((ImageView) v.findViewById(R.id.img_ItemView)).getHeight();
这将返回0,即使它有默认的高度和宽度
如果你动态创建了多个图像,试试这个:
//初始化图像数组
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);
}
//就是这样。快乐的编码。
如果你动态创建了多个图像,试试这个:
//初始化图像数组
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);
}
//就是这样。快乐的编码。