简体   繁体   中英

How to stop a Splash Screen with a click before the end?

I created an activity splashScreen in my application.

This works very well with animation is my code :

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

Now I would like to add the ability to click on the screen to stop the SplashScreen.

I tried this way, it works but I think it is not an optimal solution (slow) :

@Override
public boolean onTouchEvent(MotionEvent evt) {
    if(evt.getAction() == MotionEvent.ACTION_DOWN) {
        flagBack = true;
        StartMainActivity();
    }
    return true;
}

Thank you in advance!

使用与onTouchEvent中相同的代码将OnClickListener设置为活动

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM