如何淡入1张图像淡出第2张图像?

时间:2013-01-21 11:06:04

标签: android

我发现这个教程http://android-er.blogspot.com/2012/02/animate-fade-infade-out-by-changing.html淡出淡出相同的图像如何更改此代码使用两个图像淡入1个图像淡出第二个自动???

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

 public class AndroidAnimTranslateActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Button buttonFadeOut = (Button)findViewById(R.id.fadeout);
    final ImageView image = (ImageView)findViewById(R.id.image);

     Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);

    image.startAnimation(animationFadeIn);
    animationFadeIn.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub

            image.startAnimation(animationFadeOut);

        }
    });
    final Animation animationFadeOut = AnimationUtils.loadAnimation(this,  
 R.anim.fadeout);
    buttonFadeOut.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            image.startAnimation(animationFadeOut);
        }});
 }
}


        <?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:orientation="vertical" >

 <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
     android:text="@string/hello" />
 <Button
    android:id="@+id/fadein"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fade In"/>
 <Button
    android:id="@+id/fadeout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Fade Out"/>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" 
    android:gravity="center">

    <ImageView
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:src="@drawable/ic_launcher" />

</LinearLayout>    

     </LinearLayout>



         <!-------fadein.xml-->>


        <?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/linear_interpolator">
 <alpha 
    android:fromAlpha="0.1" 
    android:toAlpha="1.0" 
    android:duration="2000" 
    />
     </set>



 <!--------fadeout.xml----->


               <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<alpha 
    android:fromAlpha="1.0" 
    android:toAlpha="0.1" 
    android:duration="2000" 
    />
</set>

1 个答案:

答案 0 :(得分:0)

我认为你应该使用Animationlistener:

image.startAnimation(animationFadeIn);
animationFadeIn.setAnimationListener(new AnimationListener() {

                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub

                    image.startAnimation(animationFadeOut);

                }
            });

所以第二个动画将在第一个结束时开始