2013-08-16 155 views
18

我已經看過類似的問題,並嘗試了他們的解決方案,但它並沒有爲我工作。我嘗試採取imageView的寬度,但它返回0.這裏是代碼:查看的getWidth()和getHeight()返回0

public class MainActivity extends Activity { 
private ImageView image1; 
float centerX,centerY; 
private ImageView image2; 
private boolean isFirstImage = true; 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.rotate); 
    image1 = (ImageView) findViewById(R.id.ImageView01); 
    image2 = (ImageView) findViewById(R.id.ImageView02); 
    image2.setVisibility(View.GONE); 

    if (isFirstImage) { 
     applyRotation(0, 90); 
     isFirstImage = !isFirstImage; 
    } 
} 

private void applyRotation(float start, float end) { 


      centerX=image1.getWidth()/2.0f; 
    centerY=image1.getHeight()/2.0f; 
    final Flip3dAnimation rotation = 

    new Flip3dAnimation(start, end,centerX , centerY); 
    rotation.setDuration(500); 
    rotation.setFillAfter(true); 
    rotation.setInterpolator(new AccelerateInterpolator()); 
    rotation.setAnimationListener(new DisplayNextView(isFirstImage, image1, 
      image2)); 

    if (isFirstImage) 
    { 
     image1.startAnimation(rotation); 
    } else { 
     image2.startAnimation(rotation); 
    } 
} 
} 

任何人都可以幫助我嗎?謝謝

+5

關於此主題的SO有足夠多的問題/答案。簡而言之,您可以在尚未測量時訪問寬度和高度。 – Tobrun

回答

17

您需要使用onSizechanged()方法。

這是當這種觀點的尺寸已經改變

+0

但是他怎麼用'onSizechanged()'方法。這個方法是受保護的方法,只有當他創建了自己的佈局時,他纔可以使用這個方法。視圖 –

+0

這是真的。爲什麼我這樣回答我不記得了,但是如果從'Fragment'的'Activity'訪問'ImageView'的'width'和'height',那麼它應該在生命週期的某個地方完成,比如在'onStart )',儘管我找不到'height'和'width'變成非零的明確聲明。 – Kerry

9

在測量之前或在將其可見性設置爲GONE之後,您要麼訪問ImageViews的寬度和高度

image2.setVisibility(View.GONE); 

隨後的getWidth()或的getHeight()將返回0,

你也可以看看onWindowFocusChanged(),onSizeChanged()和onMeasure():

@Override 
public void onWindowFocusChanged(boolean focus) { 
    super.onWindowFocusChanged(focus); 
    // get the imageviews width and height here 
} 
4
public class GETImageView extends ImageView { 

public GETImageView (Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    Drawable d = getDrawable(); 

    if (d != null) { 
     int width = MeasureSpec.getSize(widthMeasureSpec); 
    } 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 
} 

ImageView還沒有繪製圖像,所以沒有寬度或高度返回。 您可以使用子類來獲取imageview寬度。

下面是一些鏈接爲您提供另一種簡單的方法:

How to get the width and height of an android.widget.ImageView?

How to get the width and height of an Image View in android?

Android ImageView return getWidth, getHeight zero

希望這有助於你。

+1

我不知道如何謝謝你。你上面的代碼的一部分幫助了我很多。 'int width = MeasureSpec.getSize(widthMeasureSpec);'。我花了差不多2天的時間,嘗試了所有的排列和組合來完成我的工作,但是這條線幫助我找出瞭解決方案。 :) – Srikanth

+0

我們只是在幫助對方節省時間〜沒關係〜 –

3

可以使用OnGlobalLayoutListener爲ImageView的圖像1,因此您可以請使用得到確切的寬度和高度由視圖佈局...佔領作爲

image1 = (ImageView) findViewById(R.id.ImageView01); 
     ViewTreeObserver viewTreeObserver = image1.getViewTreeObserver(); 
     viewTreeObserver 
       .addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
        @Override 
        public void onGlobalLayout() { 
         if (isFirstImage) { 
          applyRotation(0, 90); 
          isFirstImage = !isFirstImage; 
         } 
         if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) 
          image1.getViewTreeObserver() 
            .removeOnGlobalLayoutListener(this); 
         else 
          image1.getViewTreeObserver() 
            .removeGlobalOnLayoutListener(this); 
        } 
       }); 
+0

當能見度消失時情況並非如此。我的意思是,在這種情況下,你無法得到寬度或高度。 – Sotti

-7

處理程序並獲得高度和寬度上與該延遲時間> = 10

這可能是使用有用:

@Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     getHeight(); 

    } 

    private void getHeight() { 
     // TODO Auto-generated method stub 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       Toast.makeText(getApplicationContext(), "Height:" + mAnimationView.getMeasuredHeight(), 
         Toast.LENGTH_SHORT).show(); 
      } 
     }, 10L); 

    } 
+5

這樣做並不是一個好主意。你不能保證'10'的任意延遲將足夠佈局發生。 – jwir3

8

你應該使用:image1.getLayoutParams().width;

+5

使用layout_width =「match_parent」時失敗...返回-1 ... –

0

也許你應該先調用View.measure(INT,INT)。
由於在繪製該視圖之前未定義寬度和高度。你可以手動調用度量來強制這個視圖來計算它的大小;

相關問題