2014-11-05 145 views
1

我有多個在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 
+0

可以顯示放置你的imageview?如果他們可以顯示意味着他們應該有寬度和高度> 0 – 2014-11-05 14:04:49

回答

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

//運行代碼

0

一旦你的觀點已經被測量使用PreDrawListener得到的尺寸。 OnWindowFocusChanged不保證所有視圖都被測量過。

view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
    @Override 
    public boolean onPreDraw() { 
     view.getViewTreeObserver().removeOnPreDrawListener(this); 
     int width = view.getWidth(); 

    } 
}); 
+0

您是否嘗試過超過1個ImageView。我籠罩它會給最後的imageview的寬度和高度。謝謝 – 2014-11-05 13:59:24

+0

@ user406090是的,它發生了同樣的情況。有沒有解決方案? – 2014-11-05 14:01:20

0

添加此代碼爲動態imageview每個人:如果你想保存所有動態imageview'swidthheight的,你可以使用整數List保存寬度

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