如何在两张图片之间实现淡入淡出效果?

5

好的,我来帮忙翻译一下。这里有两张图片在我的启动画面中加载。第一张图片打开(开始启动画面),然后第二张图片打开,当第二张图片关闭时,主活动开始。现在我的问题是如何让我的第一张图片淡出,然后淡入我的第二张图片?

-哦,对了,不要交叉淡入淡出 -只需要完整的淡出和淡入过渡效果 -提前感谢

-启动画面.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:id="@+id/lin_lay"
android:gravity="center" >

<ImageView
android:contentDescription="@string/desc"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinning_wheel_image"
android:background="@drawable/splashscreen1" />
</LinearLayout>

主要的anim.xml文件。
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
<item android:drawable="@drawable/splashscreen1" android:duration="2500" />
<item android:drawable="@drawable/splashscreen2" android:duration="4000" />
</animation-list>

Splash.java文件

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound);
    ourSong.start();
    Thread timer = new Thread(){
        public void run(){
            try{
                sleep(10500);
            } catch (InterruptedException e){
                e.printStackTrace();
            }finally{
                Intent openStartingPoint = new Intent("com.theapplication.app.STARTINGPOINT");
                startActivity(openStartingPoint);

            }
        }
    };
    timer.start();
}

@Override
public void setRequestedOrientation(int requestedOrientation) {
    // TODO Auto-generated method stub
    super.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    // TODO Auto-generated method stub
    super.onWindowFocusChanged(hasFocus);
    ImageView mainimage = (ImageView)findViewById(R.id.spinning_wheel_image);
    mainimage.setBackgroundResource(R.anim.mainamin);
    mainanimation = (AnimationDrawable) mainimage.getBackground();
    mainanimation.start();

尝试使用playSequentially了吗? - MysticMagicϡ
你能用代码展示给我看吗? - Warren0618
2个回答

11

使用ImageSwitcher代替ImageView,它本身支持动画效果。可以参考以下示例:

http://www.java2s.com/Code/Android/UI/UsingImageSwitcher.htm

您可以像下面这样添加动画效果:

imageSwitcher.setInAnimation(fadeInAnimation);
imageSwitcher.setOutAnimation(fadeOutAnimation);

//我的测试:

  public class IntroActivity extends Activity implements ViewFactory {

        private static final String TAG = "IntroActivity";

        private final int[] images = { R.drawable.img3, R.drawable.img2,
            R.drawable.img1, R.drawable.img4, R.drawable.img5, R.drawable.img6,
            R.drawable.img7, R.drawable.img8 };
    private int index = 0;
    private final int interval = 10000;
    private boolean isRunning = true;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_intro);

        startAnimatedBackground();  

    }

    private void startAnimatedBackground() {
        Animation aniIn = AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in);
        aniIn.setDuration(3000);
        Animation aniOut = AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out);
        aniOut.setDuration(3000);

        final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
        imageSwitcher.setInAnimation(aniIn);
        imageSwitcher.setOutAnimation(aniOut);
        imageSwitcher.setFactory(this);
        imageSwitcher.setImageResource(images[index]);

        final Handler handler = new Handler();
        Runnable runnable = new Runnable() {

            @Override
            public void run() {
                if (isRunning) {
                    index++;
                    index = index % images.length;
                    Log.d("Intro Screen", "Change Image " + index);
                    imageSwitcher.setImageResource(images[index]);
                    handler.postDelayed(this, interval);
                }
            }
        };
        handler.postDelayed(runnable, interval);

    }

    @Override
    public View makeView() {
        ImageView imageView = new ImageView(this);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        return imageView;
    }

    @Override
    public void finish() {
        isRunning = false;
        super.finish();
    }
}

为了启动下一个活动,在

@Override public void run() { if (isRunning) {

中检查索引值,如果索引值等于1,则启动下一个活动并结束当前活动;


你能用我上面给的代码示例演示一下吗?抱歉,我还在努力掌握这个。 - Warren0618
或者你如何从 ImageView 更改为 ImageSwitcher?使用以上代码。 - Warren0618
请参见链接中的示例。 - Mohammad Ersan
我该如何在ImageView中添加淡入淡出的过渡效果? - Warren0618
嘿,MoshErsan!我有一个快速的问题,如何在不重叠或交叉淡化图像的情况下实现渐变效果(我想先显示一张图像,然后“淡出”到黑色,再“淡入”另一张图像)?这可能吗? - Warren0618

2

您可以简单地将图像淡出,更改它,最后再将其淡入。

imageView.animate()
       .alpha(0f)
       .setDuration(100)
       .setListener(new Animator.AnimatorListener() {
           @Override
           public void onAnimationStart(Animator animator) { }
           @Override
           public void onAnimationEnd(Animator animator) {
               imageView.setImageResource(R.drawable.newimg);
               imageView.animate().alpha(1).setDuration(200);
           }
           @Override
           public void onAnimationCancel(Animator animator) { }
           @Override
           public void onAnimationRepeat(Animator animator) { }
       });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接