我需要做一个闪屏

时间:2011-08-06 14:46:27

标签: android sdk

在iOS中我会这样做:

    splashView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 768, 1024)];
splashView.image = [UIImage imageNamed:@"Default.png"];
[window addSubview:splashView];
[window bringSubviewToFront:splashView];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:3.5];
[UIView setAnimationTransition:UIViewAnimationTransitionNone forView:window cache:YES];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(startupAnimationDone:finished:context:)];
splashView.alpha = 0.0;
splashView.frame = CGRectMake(-60, -60, 900, 1200);
[UIView commitAnimations];

然后“推”视图控制器继续。

我查看了各种示例和教程,无法处理需要的代码:

  1. 显示闪屏
  2. 让它淡出
  3. 是否显示了另一个视图(操作?)。
  4. 有些人发布了我改编的内容,但评论的代码会产生错误。我一定错过了什么。

1 个答案:

答案 0 :(得分:1)

public class SplashScreen extends Activity {
protected boolean _active = true;
protected int _splashTime = 3000;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.splash);

    // thread for displaying the SplashScreen
    Thread splashTread = new Thread() {
        @Override
        public void run() {
            try {
                int waited = 0;
                while (_active && (waited < _splashTime)) {
                    sleep(100);
                    if (_active) {
                        waited += 100;
                    }
                }
            } catch (InterruptedException e) {
                // do nothing
            } finally {
                finish();
                startActivity(new Intent(
                        "com.android.NextActivity"));
            }
        }
    };
    splashTread.start();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        _active = false;
    }
    return true;
}

}

和splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width="fill_parent"
android:layout_height="fill_parent"  >
<ImageView android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:src="@drawable/your_image_here" />
</LinearLayout>