渐变背景淡出并进入另一个页面



我有一个渐变背景的页面,我也有一个动画XML页面,将alpha从1更改为0,我试图淡出背景,然后转到另一个页面,但问题是,当持续时间完成(背景淡出),它想去另一个页面背景一秒钟出现在屏幕上。我该如何解决这个问题?

动画/fade_out.xml

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="4000" >
</alpha>

可拉的/linegradient.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
    android:centerX="20%"
    android:endColor="#aed36c"
    android:startColor="#44c8f5" />
</shape>

MainActivity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/firstpage"
android:background="@drawable/linergradiant"
android:orientation="vertical" >
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/texture" >
    <LinearLayout
        android:layout_width="200dip"
        android:layout_height="200dip"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="116dp"
        android:background="@drawable/logo_big2" >
    </LinearLayout>
</RelativeLayout>
</LinearLayout>

MainActivity.java

public class MainActivity extends ActionBarActivity implements AnimationListener {
 LinearLayout screen;
// Handler handler = new Handler();
 int i;
 Intent intent;
 Animation animFadeout;
 Animation animFadein;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    screen=(LinearLayout) findViewById(R.id.firstpage);
    animFadeout=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_out );
    animFadeout.setAnimationListener(this);
    screen.post(new Runnable() {
        @Override
        public void run() {
            screen.startAnimation(animFadeout); 
        }
    }); 
}
@Override
public void onAnimationEnd(Animation arg0) {
    animFadein=AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in );
    animFadein.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation arg0) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onAnimationRepeat(Animation arg0) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onAnimationEnd(Animation arg0) {
            startActivity(new Intent(getApplicationContext(),BMIcalculator.class)); 
        }
    });
     screen.startAnimation(animFadein);
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationStart(Animation arg0) {
    // TODO Auto-generated method stub  
}
}

您需要创建一个fade_in动画来逆转fade_out动画

的效果

创建一个fade_id动画在你的动画文件夹

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="1000" >
</alpha>

fade_out的动画完成后使用它的onAnimationEnd

示例:

 @Override
    public void onAnimationEnd(Animation arg0) {
        Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in );
        animFadeIn.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {}
            @Override
            public void onAnimationRepeat(Animation animation) { }
            @Override
            public void onAnimationEnd(Animation animation) { 
                startActivity(new Intent(getApplicationContext(),BMIcalculator.class)); 
            }
        });
        screen.startAnimation(animFadeIn);
     }

我找到了解决这个问题的答案只需添加

即可轻松解决。
animFadeout.setFillAfter(true);

它现在工作得很好。div;)

最新更新