我有多個在for循環中動態創建的圖像。獲取動態創建的Imageview的高度和寬度
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()& ivFront.getHeight(),然後它顯示我0(高度)-0(寬)。我已經嘗試在onWindowFocusChanged()方法中獲取ImageView高度&寬度,但輸出是相同的0(高度)-0(寬度)。
所以,當我打開評論就說明我下面的錯誤:
11-05 19:16:28.522: E/AndroidRuntime(9153): Caused by: java.lang.IllegalArgumentException: width and height must be > 0
可以顯示放置你的imageview?如果他們可以顯示意味着他們應該有寬度和高度> 0 – 2014-11-05 14:04:49