Android 启动页倒计时

前言

 实现的逻辑很简单,谷歌给我们提供了倒计时很方便,配合基本活动之间的跳转完成,注意声明该有的权限

布局

  • activity_main

    
    
    
    
    
    
  • activity_main2

    
    
    
    
    
      
    
          
    
          
      
    
      
    
          
    
          
          
        
     
    

代码

  • MainActivity

    import android.content.Intent;
    import android.os.CountDownTimer;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.ImageView;
    import com.bumptech.glide.Glide;
    
    public class MainActivity extends AppCompatActivity {
    
    @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      getSupportActionBar().hide();// 隐藏ActionBar
      setContentView(R.layout.activity_main);
      Window window = getWindow();
      window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
      init();
      timer.start();
    }
    
    public void init() {
      ImageView iv = findViewById(R.id.main_image_001);
      Glide
              .with(this)
              .load("http://p1.so.qhimgs1.com/bdr/575__/t017dd6c89c1d818a2d.jpg")
              .into(iv);
    }
    
    CountDownTimer timer = new CountDownTimer(2000, 1000){
        @Override
        public void onTick(long sin) {
    
      }
    
      @Override
      public void onFinish() {
          Intent intent=new Intent(MainActivity.this, Main2Activity.class);
          startActivity(intent);
            }
        };
    }
    
  • Main2Activity

    import android.annotation.SuppressLint;
    import android.os.CountDownTimer;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.bumptech.glide.Glide;
    
    public class Main2Activity extends AppCompatActivity {
    
        TextView lote,time;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getSupportActionBar().hide();// 隐藏ActionBar
            setContentView(R.layout.activity_main2);
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
            init();
            timer.start();
        }
    
        public void init() {
            time = findViewById(R.id.main2_text_001);
            lote = findViewById(R.id.main2_text_002);
            ImageView logo = findViewById(R.id.main2_image_001);
            ImageView test = findViewById(R.id.main2_image_002);
            Glide
                  .with(this)
                  .load("http://p1.so.qhimgs1.com/bdr/575__/t017dd6c89c1d818a2d.jpg")
                  .into(logo);
            Glide
                  .with(this)
                  .load("http://p1.so.qhimgs1.com/bdr/575__/t017dd6c89c1d818a2d.jpg")
                  .into(test);
      }
    
        CountDownTimer timer = new CountDownTimer(5000, 1000){
            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long sin) {
                time.setText("跳过(" + sin/1000 + ")");
            }
    
            @Override
            public void onFinish() {
                Toast.makeText(Main2Activity.this, "倒计时完成,下一步跳转主页" ,          Toast.LENGTH_SHORT).show();
    
          }
      };
    
      public void A(View v){
          timer.cancel();
          Toast.makeText(Main2Activity.this, "倒计时终止,下一步跳转主页" ,       Toast.LENGTH_SHORT).show();
        }
    }
    
  • 备注

    拥有完全的网络访问权限:
    
    
    
    注意Github上的Glide图片框架,添加依赖:
    dependencies {
      ...
      implementation 'com.github.bumptech.glide:glide:3.7.0'
      ...
    }
    

你可能感兴趣的:(Android 启动页倒计时)