渐变背景淡出并转到另一页

时间:2014-08-12 21:16:58

标签: java android xml

我有渐变背景的页面,我也有一个动画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  
}

}

2 个答案:

答案 0 :(得分:0)

你需要创建一个 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>

使用onAnimationEnd完成 fade_out 动画后使用此功能。

<强>样品:

 @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);
     }

答案 1 :(得分:0)

我找到答案如何解决这个问题 它可以通过添加

轻松解决
animFadeout.setFillAfter(true);

现在效果很好。 ;)