我在我的应用程序中创建了一个活动 splashScreen。
这与动画效果很好是我的代码:
public class SpalshScreenActivity extends Activity {
private static final int STOPSPLASH = 0;
private static final long SPLASHTIME = 3000;
private boolean flagBack = false;
private final transient Handler splashHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == STOPSPLASH && !flagBack) {
StartMainActivity();
}
super.handleMessage(msg);
}
};
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(R.style.HideActionBar);
setContentView(R.layout.splash);
StartAnimations();
final Message msg = new Message();
msg.what = STOPSPLASH;
splashHandler.sendMessageDelayed(msg, SPLASHTIME);
}
private void StartAnimations() {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
anim.reset();
LinearLayout l=(LinearLayout) findViewById(R.id.lin_lay);
l.clearAnimation();
l.startAnimation(anim);
anim = AnimationUtils.loadAnimation(this, R.anim.translate);
anim.reset();
ImageView iv = (ImageView) findViewById(R.id.logo);
iv.clearAnimation();
iv.startAnimation(anim);
}
private void StartMainActivity() {
final Intent intent = new Intent(SpalshScreenActivity.this, MainFragmentActivity.class);
startActivity(intent);
finish();
}
public boolean onKeyDown(int keyCode, KeyEvent evt) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
flagBack = true;
finish();
return true;
}
return false;
}}
现在我想添加单击屏幕以停止 SplashScreen 的功能。
我尝试过这种方式,它可以工作,但我认为这不是最佳解决方案(慢):
@Override
public boolean onTouchEvent(MotionEvent evt) {
if(evt.getAction() == MotionEvent.ACTION_DOWN) {
flagBack = true;
StartMainActivity();
}
return true;
}
先感谢您!