Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

android - View's getWidth() and getHeight() returning 0

I have looked at the similar questions and tried their solutions, but it didn't work for me. I'm trying to read width of an imageView, but it is returning 0. Here is the code:

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

Can anyone help me with this? Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use the onSizechanged() method.

This is called during layout when the size of this view has changed


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...