图像不会被隐藏

时间:2015-07-19 11:15:43

标签: android

我希望图像从屏幕的左侧移动到右侧,然后消失 翻译工作。但是

img.setVisibility(View.GONE);

一切都消失了,不仅仅是图像。

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView img = (ImageView) findViewById(R.id.img_animation);

        wander(img);

        img.setVisibility(View.GONE);
    }

    public void wander(ImageView img)
    {
        TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,0.0f, 0.0f);
        animation.setDuration(500);
        animation.setRepeatCount(1);
        animation.setRepeatMode(1);
        animation.setFillAfter(true);
        img.startAnimation(animation);
    }
}

1 个答案:

答案 0 :(得分:2)

您当前的代码启动动画,然后隐藏您的图像,而无需等待动画完成。使用这样的监听器来确定动画结束。

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ImageView img = (ImageView) findViewById(R.id.img_animation);

        wander(img);
    }

    public void wander(ImageView img)
    {
        TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,0.0f, 0.0f);
        animation.setDuration(500);
        animation.setRepeatCount(1);
        animation.setRepeatMode(1);
        animation.setFillAfter(false);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

           @Override
           public void onAnimationEnd(Animation animation) {
               final ImageView img = (ImageView) findViewById(R.id.img_animation);
               img.setVisibiltiy(View.GONE);
           }

           @Override
           public void onAnimationRepeat(Animation animation) {
           }
        });
        img.startAnimation(animation);
    }
}

了解更多信息: Android Animation Listener