褪色动画

时间:2016-08-12 06:26:13

标签: android android-animation

我想在android中做淡化动画。通过单击图像转换发生,我可以看到下一个图像。

但是如何通过点击第二张图片回到第一张图片?

public void fade (View view) {
    ImageView naruto = (ImageView) findViewById(R.id.naruto);
    ImageView sasuke = (ImageView) findViewById(R.id.sasuke);
    naruto.animate().alpha(0f).setDuration(2000);
    sasuke.animate().alpha(1f).setDuration(2000);
}

public void reappear (View view) {
    ImageView sasuke1 = (ImageView) findViewById(R.id.sasuke);
    ImageView naruto1 = (ImageView) findViewById(R.id.naruto);
    sasuke1.animate().alpha(0f).setDuration(2000);
    naruto1.animate().alpha(1f).setDuration(2000);
}

1 个答案:

答案 0 :(得分:1)

淡出:

public void fade (View view) {
    ImageView naruto = (ImageView) findViewById(R.id.naruto);
    ImageView sasuke = (ImageView) findViewById(R.id.sasuke);
    naruto.animate().alpha(0f).setDuration(2000).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            naruto.setVisibility(View.GONE);
        }
    });
    sasuke.setAlpha(0f);
    sasuke.setVisibility(View.VISIBLE);
    sasuke.animate().alpha(1f).setDuration(2000).setListener(null);
}

重新显示信息:

public void reappear (View view) {
    ImageView sasuke = (ImageView) findViewById(R.id.sasuke);
    ImageView naruto = (ImageView) findViewById(R.id.naruto);
    sasuke.animate().alpha(0f).setDuration(2000).setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            naruto.setVisibility(View.GONE);
        }
    });
    naruto.setAlpha(0f);
    naruto.setVisibility(View.VISIBLE);
    naruto.animate().alpha(1f).setDuration(2000);
}