闪屏奥飞动漫在Android中 [英] Splash Screen Alpha Animation in Android

查看:209
本文介绍了闪屏奥飞动漫在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示一个闪屏动画,其中在图像变淡,然后淡出。我想要的图像已经淡出后的第二个活动来加载。

I want to display a splash screen animation where an image fades in and then fades out. I want the second activity to load after the image has faded out.


  1. 淡入时间(1000毫秒)

  2. 等待(1000毫秒)

  3. 淡出的时间(1000毫秒)

  4. 等待(1000毫秒)

  5. 加载第二个活动

我怎么去呢?目前我使用的code是:

How do I go about this? The code I'm currently using is:

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

请帮忙。

推荐答案

您可以使用AnimationSet做到这一点。在 Animation.setStartOffset()方法可以说当动画应开始(0为淡入和2000年的淡出)。下一个活动是用在3秒后启动了 Handler.postDelayed()

You can do it using an AnimationSet. The Animation.setStartOffset() method allows to say when the animation should start (0 for the fadeIn and 2000 for the fadeOut). The next Activity is launched after 3 seconds using a 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);
}

这篇关于闪屏奥飞动漫在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆