22

我查看了类似的问题并尝试了他们的解决方案,但它对我不起作用。我正在尝试阅读widtha 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);
    }
}
}

谁能帮我这个?谢谢

4

8 回答 8

22

您需要使用onSizechanged()方法。

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

于 2013-08-16T11:23:56.560 回答
18

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

于 2014-09-30T00:22:32.650 回答
11

您要么在测量之前访问 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
 }
于 2013-08-16T08:30:10.943 回答
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 还没有绘制图像,所以没有宽度或高度返回。您可以使用子类来获取图像视图的宽度。

这是一些链接,为您提供了另一种简单的方法:

如何获取 android.widget.ImageView 的宽度和高度?

如何在android中获取图像视图的宽度和高度?

Android ImageView 返回getWidth,getHeight 为零

希望这对您有所帮助。

于 2013-08-16T08:32:07.363 回答
2

您可以将 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);
                    }
                });
于 2014-04-07T05:22:36.223 回答
1

顺便说一句,在采取测量步骤之前,视图不会被调整大小。请参阅此链接:

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

于 2014-03-14T00:22:43.640 回答
0

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

于 2014-11-18T08:47:11.520 回答
-9

请使用处理程序并在延迟时间> = 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);

    }
于 2014-05-09T09:53:45.343 回答