...">
美文网首页
Android 启动页倒计时

Android 启动页倒计时

作者: 死鬼土匪 | 来源:发表于2019-04-06 16:55 被阅读0次

    前言

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

    布局

    • activity_main

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout     
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".MainActivity">
      
      <ImageView
          android:id="@+id/main_image_001"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:scaleType="fitXY" />
      </android.support.constraint.ConstraintLayout>
      
    • activity_main2

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout       
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".Main2Activity">
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          tools:ignore="MissingConstraints">
      
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
      
            <ImageView
                android:id="@+id/main2_image_001"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                tools:srcCompat="@tools:sample/backgrounds/scenic[0]" />
      
            <TextView
                android:id="@+id/main2_text_001"
                android:onClick="A"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:layout_marginStart="300dp"
                android:layout_marginTop="20dp"
                android:gravity="center"
                android:text="TextView"
                android:textColor="#000"
                android:textSize="20sp"
                tools:ignore="OnClick" />
        </RelativeLayout>
      
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:gravity="center"
            android:layout_weight="1" >
      
            <ImageView
                android:id="@+id/main2_image_002"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:scaleType="fitXY" />
      
            <TextView
                android:id="@+id/main2_text_002"
                android:textSize="20sp"
                android:textColor="#000"
                android:gravity="center"
                android:layout_marginStart="20dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="图片框架" />
            </LinearLayout>
          </LinearLayout>
       </android.support.constraint.ConstraintLayout>
      

    代码

    • 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();
          }
      }
      
    • 备注

      拥有完全的网络访问权限:
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      
      注意Github上的Glide图片框架,添加依赖:
      dependencies {
        ...
        implementation 'com.github.bumptech.glide:glide:3.7.0'
        ...
      }
      

    相关文章

      网友评论

          本文标题:Android 启动页倒计时

          本文链接:https://www.haomeiwen.com/subject/rfwriqtx.html