2

我想显示一个闪屏动画,其中图像淡入然后淡出。我希望在图像淡出后加载第二个活动。

  1. 淡入时间(1000 毫秒)
  2. 等待(1000 毫秒)
  3. 淡出时间(1000 毫秒)
  4. 等待(1000 毫秒)
  5. 加载第二个活动

我该怎么做?我目前使用的代码是:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
public class Splash extends Activity
{
    ImageView img;
    Thread timer;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);
        img = (ImageView) findViewById (R.id.imgSplash);
        img.startAnimation(FadeIn(1000));
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e1)
        {
            e1.printStackTrace();
        }

        img.startAnimation(FadeOut(1000));

        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e1)
        {
            e1.printStackTrace();
        }
        timer.start();
        Intent intent = new Intent();
        intent.setClass(Splash.this,MainScreen.class);
        startActivity(intent);
    }
    public void onPause()
    {
        super.onPause();
        finish();
    }
    private Animation FadeIn(int t)
    {
        Animation fade;
        fade = new AlphaAnimation(0.0f,1.0f);
        fade.setDuration(t);
        fade.setInterpolator(new AccelerateInterpolator());
        return fade;
    }
    private Animation FadeOut(int t)
    {
        Animation fade;
        fade = new AlphaAnimation(1.0f,0.0f);
        fade.setDuration(t);
        fade.setInterpolator(new AccelerateInterpolator());
        return fade;
    }
}

请帮忙。

4

1 回答 1

3

您可以使用 AnimationSet 来完成。该Animation.setStartOffset()方法允许说明动画应该何时开始(0 表示淡入,2000 表示淡出)。下一个 Activity 在 3 秒后使用Handler.postDelayed().

private final Handler handler = new Handler();

private final Runnable startActivityRunnable = new Runnable() {

    @Override
    public void run() {
        Intent intent = new Intent();
            intent.setClass(Splash.this,MainScreen.class);
        startActivity(intent);
    }
}; 

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    img = (ImageView) findViewById (R.id.imgSplash);

    setContentView(img);
}

@Override
protected void onResume() {
    super.onResume();

    AnimationSet set = new AnimationSet(true);

    Animation fadeIn = FadeIn(1000);
    fadeIn.setStartOffset(0);
    set.addAnimation(fadeIn);

    Animation fadeOut = FadeOut(1000);
    fadeOut.setStartOffset(2000);
    set.addAnimation(fadeOut);

    img.startAnimation(set);

    handler.postDelayed(startActivityRunnable, 3000);
}

public void onPause()
{
    super.onPause();
    handler.removeCallbacks(startActivityRunnable);
}
于 2012-07-12T16:14:00.890 回答