我在我的应用程序中创建了一个活动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;
}
提前谢谢!
答案 0 :(得分:0)
使用与onTouchEvent
中相同的代码将OnClickListener设置为活动