View的getWidth()和getHeight()返回0

时间:2013-08-16 08:22:33

标签: android view size measure

我看过类似的问题并尝试了他们的解决方案,但它对我不起作用。我尝试获取imageView的宽度,但它返回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);
    }
}
}

任何人都可以帮我吗?感谢

8 个答案:

答案 0 :(得分:20)

您需要使用onSizechanged()方法。

  

当此视图的大小发生变化时,在布局期间调用此方法

答案 1 :(得分:10)

您要么在测量之前访问ImageViews的宽度和高度,要么在将其可见性设置为GONE之后访问它。

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
 }

答案 2 :(得分:9)

您应该使用:image1.getLayoutParams().width;

答案 3 :(得分:5)

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

希望这对你有所帮助。

答案 4 :(得分:3)

您可以将OnGlobalLayoutListener用于ImageView image1,这样您就可以获得布局中视图所占据的精确宽度和高度...

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

答案 5 :(得分:1)

顺便说一句,在采取测量步骤之前,视图不会变大。看到这个链接:

getWidth() and getHeight() of View returns 0

答案 6 :(得分:0)

也许你应该首先调用View.measure(int,int) 由于在此视图绘制之前未定义宽度和高度。您可以手动调用度量来强制执行此视图以计算其大小;

答案 7 :(得分:-8)

请使用处理程序并在此处获取高度和宽度,延迟时间> = 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);

    }