addroid实现广告倒计时跳Activity之CountDownTimer

来源:互联网 发布:win7 64位优化版下载 编辑:程序博客网 时间:2024/06/14 11:01

现在的APP大部分首页打开时的欢迎界面都有广告,倒计时结束进入主界面,以及点击跳过广告直接进入,以前小小实现了一下,趁今天有时间,传上来给大家分享,欢迎大家一起学习,主要用到有:
Intent:意图,实现页面跳转
CountDownTimer:倒计时器,
首先咱看图:
111

倒计时完自动跳指定Activity,也可以跳过广告,下面直接上完整代码
首先布局文件

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <ImageView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:src="@drawable/aa"        android:id="@+id/image_ss"        android:scaleType="fitXY"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:id="@+id/text_time"        android:textSize="20sp"        /></FrameLayout>

下面SplashActivity:

package com.zking.suzhen;import android.content.Intent;import android.os.Bundle;import android.os.CountDownTimer;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.ImageView;import android.widget.TextView;public class SplashActivity extends AppCompatActivity {    private TextView timer;    private MycountDownTimer my;    private ImageView imageView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_splash);        timer = (TextView) findViewById(R.id.text_time);        imageView = (ImageView) findViewById(R.id.image_ss);        my = new MycountDownTimer(5000, 500);        my.start();        timer.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //防止再次跳过                my.cancel();                Intent intent = new Intent(SplashActivity.this, Main2Activity.class);                startActivity(intent);                //销毁                SplashActivity.this.finish();            }        });    }    class MycountDownTimer extends CountDownTimer {        public MycountDownTimer(long millisInFuture, long countDownInterval) {            super(millisInFuture, countDownInterval);        }        @Override        public void onFinish() {            Intent intent = new Intent(SplashActivity.this, Main2Activity.class);            startActivity(intent);            SplashActivity.this.finish();        }        @Override        public void onTick(long millisUntilFinished) {            timer.setText("美女还有" + millisUntilFinished / 1000 + "秒" + "点我跳过");        }    }}代码不多,简单容易理解,各位老铁可以直接拿去跑下看看,不懂的老铁留言私聊,欢迎大家互相学习进步!
0 0
原创粉丝点击