闪屏页(二)

来源:互联网 发布:livin on a prayer知乎 编辑:程序博客网 时间:2024/06/08 14:04

场景

闪屏页(SplashActivity)即应用启动后起始页。

实现

闪屏页定时跳转

主代码

package com.example.administrator.individualresume.view;import android.os.CountDownTimer;import android.view.View;import android.view.WindowManager;import android.widget.LinearLayout;import android.widget.TextView;import com.example.administrator.individualresume.R;import com.example.administrator.individualresume.base.BaseActivity;public class SplashActivity extends BaseActivity implements View.OnClickListener {    /*声明控件*/    private TextView tv_time_hint;    private LinearLayout linearLayout;    // CountDownTimer存一定延迟,有时不可见,故设倒计总长3200    private CountDownTimer countDownTimer = new CountDownTimer(3200, 1000) {        @Override        public void onTick(long l) {            tv_time_hint.setText(l / 1000 + " s");        }        @Override        public void onFinish() {            tv_time_hint.setText(0 + " s");            JumpNoBundleAndFinish(LoginActivity.class);            overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);        }    };    @Override    public void onClick(View view) {        switch (view.getId()) {            case R.id.linearLayout:// 点击跳转                JumpNoBundleAndFinish(LoginActivity.class);                overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);                break;            default:                break;        }    }    @Override    protected void InitView() {        // 设全屏        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);        // 加载视图        setContentView(R.layout.activity_splash);        // 初始化控件        tv_time_hint = (TextView) findViewById(R.id.tv_time_hint);        linearLayout = (LinearLayout) findViewById(R.id.linearLayout);    }    @Override    protected void SetListener() {        linearLayout.setOnClickListener(this);    }    @Override    protected void InitVariable() {    }    @Override    protected void InitData() {    }    @Override    protected void CarryOut() {    }    @Override    protected void onResume() {        super.onResume();        countDownTimer.start();    }    @Override    protected void onDestroy() {        super.onDestroy();        if (countDownTimer != null) {            countDownTimer = null;        }    }}

布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout 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="com.example.administrator.individualresume.view.SplashActivity">    <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:scaleType="centerCrop"        android:src="@drawable/splash_page" />    <LinearLayout        android:id="@+id/linearLayout"        android:layout_width="@dimen/d46"        android:layout_height="@dimen/d46"        android:layout_alignParentRight="true"        android:layout_marginRight="@dimen/d12"        android:layout_marginTop="@dimen/d23"        android:background="@drawable/title_page_time_hint"        android:gravity="center"        android:orientation="vertical"        android:padding="@dimen/d6">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/jump"            android:textColor="@color/colorPrimary"            android:textSize="@dimen/s11" />        <TextView            android:id="@+id/tv_time_hint"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textColor="@color/colorPrimary"            android:textSize="@dimen/s10"            tools:text="3s" />    </LinearLayout></RelativeLayout>

drawable

title_page_time_hint

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="oval"    android:useLevel="false">    <stroke        android:width="@dimen/d2"        android:color="@color/colorPrimary"></stroke>    <solid android:color="@color/transparent"></solid></shape>

原理

android.os中类CountDownTimer优点为不需理会线程交互,该类自动处理好。开发者只需在回调中处理时间,设跳转逻辑即可。

注意

不足之处为第一秒倒计时有时不可见,故设倒计总长3200ms。

原创粉丝点击