I have looked at the similar questions and tried their solutions but it did not work for me. I try to take width of an imageView, but it returns 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
Maybe you should call View.measure(int,int) first.
Since the width and height is not defined until this view is draw. You can call measure by hand to enforce this view to calculate it's size;
By the way, views don't get sized until the measurement steps have been taken. See this link:
getWidth() and getHeight() of View returns 0
The ImageView have not yet draw the image, so no width or height return. You may use a subclass to get the imageview width.
Here is some link provide you another easy methods:
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
Hope this help you.
you can use OnGlobalLayoutListener for the ImageView image1 so you can get exact width and height occupied by the view in the layout...as
You are either accessing the ImageViews width and height before it has been measured, or after you set its visibility to GONE.
Then getWidth() or getHeight() will return 0.,
You can also have a look at onWindowFocusChanged(), onSizeChanged() and onMeasure():
You should use:
image1.getLayoutParams().width;