应用程序首先显示Image然后显示动画

时间:2016-07-01 20:23:07

标签: android android-animation fadein

我的代码的问题在于它首先显示图像然后显示动画(淡入)。首先,图像在屏幕上显示大约一秒钟,然后淡出同一图像。请帮助。 这是代码:

public class LoginActivity extends Activity {

    private Animation translate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_login);



        final ImageView image = (ImageView) findViewById(R.id.image);
        Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
        image.startAnimation(translate);

        final int[] imageArray = {R.drawable.agra, R.drawable.bang,
                R.drawable.mumbai
        };

        final Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            int i = 0;

            public void run() {
                image.setImageResource(imageArray[i]);
                i++;
                if (i > imageArray.length - 1) {
                    i = 0;
                }
                handler.postDelayed(this, 4000);
            }
        };
        handler.postDelayed(runnable, 4000);

    }




}

这是动画文件translate.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="4000"
    android:fillAfter="true"
    android:fromAlpha="0"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:repeatCount="-1"
    android:toAlpha="1">
</alpha>

1 个答案:

答案 0 :(得分:0)

在XML中,将图像视图设置为INVISIBLE:

<ImageView
     ....
     android:visibility:"invisible"/>

然后,尝试更新您的代码,如下所示:

final ImageView image = (ImageView) findViewById(R.id.image);
Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
translate.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {
        findViewById(R.id.image).setVisibility(View.VISIBLE);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

image.startAnimation(translate);