当View高度为零时,为什么不调用onAnimationEnd [英] Why does onAnimationEnd not called when View height is zero

查看:106
本文介绍了当View高度为零时,为什么不调用onAnimationEnd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设ImageView的宽度和高度分别在布局文件中设置为match_parent和wrap_content。这样的事情:



< ImageView 
android:id = @ + id / image
android:layout_width = match_parent
android:layout_height = wrap_content />





然后我们在java文件中访问它:



 ImageView image =(ImageView)findViewById(R.id.image); 





然后我们创建一个动画:



< pre lang =java>动画动画= AlphaAnimation( 0 1 );
anim.setAnimationListener( new AnimationListener(){
@ Override
public void onAnimationStart(动画动画){
// 什么都不做
}

@ Override
public void onAnimationEnd(动画动画){
// 在此处开始下一个活动
}

@覆盖
public void onAnimationRepeat(动画动画){
// 什么都不做
}
}





然后我们将动画添加到ImageView:



 image.startAnimation(anim); 





现在这不行,对吧?因为我相信ImageView是空的(没有高度,所以动画不起作用,onAnimationEnd永远不会被调用)



但是当我添加以下内容时line,onAnimationEnd有效!



 setImageBitmap(null)





检查视图的尺寸后,高度设置为1px。



现在,这是否意味着动画仅适用于该视图至少有1px的尺寸(宽度和高度)?



我试图追踪Android源代码,但我在某个地方迷路而且无法理解View,ImageView和Animation类中发生的事情。



有人可以对此有所了解吗?在此先感谢。

解决方案

Let's say an ImageView width and height is set to match_parent and wrap_content respectively in the layout file. Something like this:

<ImageView
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />



Then we access it in the java file:

ImageView image = (ImageView) findViewById(R.id.image);



Then we create an animation:

Animation anim = new AlphaAnimation(0, 1);
anim.setAnimationListener(new AnimationListener() {
    @Override
        public void onAnimationStart(Animation animation) {
            // do nothing
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // start next activity here
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // do nothing
        }
}



Then we attach the animation in the ImageView:

image.startAnimation(anim);



Now this will not work, right? Since I believe the ImageView is empty (does not have a height, so the animation will not work and the onAnimationEnd will never be called)

But when I added the following line, the onAnimationEnd works!

setImageBitmap(null)



Upon checking the dimension of the view, the height is set to 1px.

Now, does this mean Animation only works when the the view has at least 1px of dimension (for both width and height)?

I tried to trace Android source code but I got lost somewhere and wasn't able to comprehend what is going on in the View, ImageView and Animation classes.

Can anyone shed some light regarding this? Thanks in advance.

解决方案

这篇关于当View高度为零时,为什么不调用onAnimationEnd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆