我有多个图像是动态创建的for循环。
for (int i = 0; i < 15; i++) {
ivFront = new TouchImageView(this);
ivFront.setId(i + 1);
ivFront.setBackgroundResource(frontImages[i]);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
frontWidth[i], frontHeight[i]);
((MarginLayoutParams) params).setMargins(frontX_axis[i],
frontY_axis[i], 0, 0);
ivFront.setAdjustViewBounds(true);
ivFront.setLayoutParams(params);
ivFront.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
Bitmap bmp = loadBitmapFromView(v);
return true;
}
});
//Bitmap bmp_new = loadBitmapFromView(ivFront);
rlMain.addView(ivFront, params);
}
public static Bitmap loadBitmapFromView(View v) {
Canvas bitmapCanvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(v.getWidth() * 2,
v.getHeight() * 2, Bitmap.Config.ARGB_8888);
bitmapCanvas.setBitmap(bitmap);
bitmapCanvas.scale(2.0f, 2.0f);
v.draw(bitmapCanvas);
return bitmap;
}
当我写ivFront.getWidth()&amp; ivFront.getHeight()然后它显示0(高度)-0(宽度)。我已经尝试过获得ImageView高度&amp; onWindowFocusChanged()方法中的宽度,但输出相同0(高度)-0(宽度)。
因此,当我打开评论时,它会显示以下错误:
11-05 19:16:28.522: E/AndroidRuntime(9153): Caused by: java.lang.IllegalArgumentException: width and height must be > 0
答案 0 :(得分:0)
使用PreDrawListener在测量视图后获取尺寸。 OnWindowFocusChanged不保证所有视图都已被测量。
view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
view.getViewTreeObserver().removeOnPreDrawListener(this);
int width = view.getWidth();
}
});
答案 1 :(得分:0)
为每个动态imageview
:
ivFront.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
iv.getViewTreeObserver().removeOnPreDrawListener(this);
height = iv.getMeasuredHeight();
width = iv.getMeasuredWidth();
return true;
}
});
ivFront.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent arg1) {
Bitmap bmp = loadBitmapFromView(v);
return true;
}
});
如果您要保存所有动态imageview's
width
和height
,可以使用List
整数来保存每个imageview
的宽度和高度{{ 1}}。
答案 2 :(得分: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);
}
//运行代码