ANDROID启动画面有两个图像淡出

时间:2017-01-09 10:28:19

标签: android xml android-animation splash-screen

我是新手。

我怀疑的是如何创建一个具有两个不同背景的闪屏,其效果是逐渐淡化,然后我想进入下一个活动。

我用谷歌搜索了它。但我无法找到解决方案。我的代码如下。

SplashActivity.java

public class SplashActivity extends Activity {
    LinearLayout splashLayout;
    protected Animation fadeIn,fadeOut;
    protected ImageView img1;
    protected ImageView img2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);
        img1 = (ImageView) findViewById(R.id.imageView);
        img2 = (ImageView) findViewById(R.id.imageView2);
        splashLayout = (LinearLayout) findViewById(R.id.splashDrawer);
        splashLayout.setBackgroundResource(R.drawable.slidetwo);
        fadeIn = AnimationUtils.loadAnimation(SplashActivity.this, R.anim.fade_in);
        fadeOut = AnimationUtils.loadAnimation(SplashActivity.this, R.anim.fade_out);
        //img2.setVisibility(View.VISIBLE);
        splashLayout.startAnimation(fadeIn);


        //splashLayout.setBackgroundResource(R.drawable.slidetwo);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(SplashActivity.this,
                        SigninActivity.class);
                startActivity(intent);
                splashLayout.setBackgroundResource(R.drawable.slideone);
                //img2.setVisibility(View.GONE);
                //img2.startAnimation(fadeOut);
                img1.setVisibility(View.VISIBLE);
                img1.startAnimation(fadeIn);
                SplashActivity.this.finish();

                overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

            }
        }, Constants.SPLASH_TIME_OUT);
    }
}

splash_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:id="@+id/splashDrawer" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/imageView"
            android:src="@drawable/slideone"
            android:visibility="gone" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView2"
            android:src="@drawable/slidetwo"
            android:visibility="gone" />
</LinearLayout>

fade_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="5000"/> <!--//Time in milliseconds-->
</set>

Fade_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <alpha android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:duration="5000"/><!-- //Time in milliseconds-->
</set>

push_left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="100%p"
        android:toXDelta="0" />

</set>

push_left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:toXDelta="-100%p" />
</set>

这适用于第一个图像背景,但图像并不完全适合屏幕。但在将背景更改为第一个之前,它会导航到signinActivity

我的客户要求是以2秒钟显示每个背景。

2 个答案:

答案 0 :(得分:0)

它如此简单。在你的onStart中。使用它会在启动活动时显示淡出动画。无需使用动画。 overridePendingTransition也可以这样做。

@Override
protected void onStart() {
    super.onStart();
    overridePendingTransition(0, R.anim.splashfadeout);
}

并在您的处理程序中使用此 -

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
           fadeOutAndHideImage(img);
        }
    }, Constants.IMAGE_TIME_OUT);

//handler for switching to other activity
new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
           Intent intent = new Intent(SplashActivity.this,
                    SigninActivity.class);
            startActivity(intent);
        }
    }, Constants.SPLASH_TIME_OUT);


  private void fadeOutAndHideImage(final ImageView img)
  {
    Animation fadeOut = new AlphaAnimation(1, 0);
    fadeOut.setInterpolator(new AccelerateInterpolator());
    fadeOut.setDuration(1000);

    fadeOut.setAnimationListener(new AnimationListener()
    {
        public void onAnimationEnd(Animation animation) 
        {
              img.setVisibility(View.GONE);
        }
        public void onAnimationRepeat(Animation animation) {}
        public void onAnimationStart(Animation animation) {}
});

img.startAnimation(fadeOut);
}

设置两个处理程序之间的时差。

<强> splashfadeout.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="750"
android:fromAlpha="1.0"
android:interpolator="@android:interpolator/accelerate_cubic"
android:startOffset="250"
android:toAlpha="0.0"
android:zAdjustment="top" />

答案 1 :(得分:0)

不确定,但你可以试试这个

new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

              img1.setAnimation( inFromRightAnimation() );
              img1.setBackground(R.Drawable.YourSecondImageId);

           //splashLayout.setBackgroundResource(R.drawable.slidetwo);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    Intent intent = new Intent(SplashActivity.this,
                            SigninActivity.class);
                    startActivity(intent);
                    splashLayout.setBackgroundResource(R.drawable.slideone);
                    //img2.setVisibility(View.GONE);
                    //img2.startAnimation(fadeOut);
                    img1.setVisibility(View.VISIBLE);
                    img1.startAnimation(fadeIn);
                    SplashActivity.this.finish();

                    overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

                   }
                 }, Constants.SPLASH_TIME_OUT);


                }
            }, Constants.TIME_TO_CHANGE_FIRST_IMAGE);

并且

 public Animation inFromRightAnimation()
    {
        Animation inFromRight = new TranslateAnimation(
                Animation.RELATIVE_TO_PARENT, +1.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f,
                Animation.RELATIVE_TO_PARENT, 0.0f);
        inFromRight.setDuration(240);
        inFromRight.setInterpolator(new AccelerateInterpolator());
        return inFromRight;
    }

您需要管理两个图像和Splash之间的时间差 如果不行,请告诉我