首页 > 系统 > Android > 正文

Android 实现闪屏页和右上角的倒计时跳转实例代码

2020-04-11 10:45:58
字体:
来源:转载
供稿:网友

以前编程的时候,遇到倒计时的功能时,经常自己去写,但其实Android已经帮封装好了一个倒计时类CountDownTimer,其实是将后台线程的创建和Handler队列封装成为了一个方便的类调用。

闪屏页用到了handler和CountDownTimer类,还需配置一下Activity的主题,这里是:android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 全屏主题的意思。

给大家展示下效果图:

代码如下所示:

package com.example.shanping;import java.lang.ref.WeakReference;import com.example.shanping.MyActivity.MyCountDownTimer;import android.os.Bundle;import android.os.CountDownTimer;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.content.Intent;import android.util.Log;import android.view.Menu;import android.widget.TextView;public class MainActivity extends Activity {private MyCountDownTimer mc; private TextView tv;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);tv = (TextView) findViewById(R.id.textView1); mc = new MyCountDownTimer(3000, 1000); mc.start();handler.postDelayed(new Runnable() {@Overridepublic void run() {Intent intent=new Intent(MainActivity.this,MyActivity.class);startActivity(intent);}}, 3000);}private Handler handler=new Handler();/** * 继承 CountDownTimer 防范 * * 重写 父类的方法 onTick() 、 onFinish() */class MyCountDownTimer extends CountDownTimer { /** * * @param millisInFuture * 表示以毫秒为单位 倒计时的总数 * * 例如 millisInFuture=1000 表示1秒 * * @param countDownInterval * 表示 间隔 多少微秒 调用一次 onTick 方法 * * 例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick() * */public MyCountDownTimer(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } public void onFinish() { tv.setText("正在跳转"); } public void onTick(long millisUntilFinished) { tv.setText("倒计时(" + millisUntilFinished / 1000 + ")"); } }}

下面给大家分享一段代码关于Android实现启动闪屏界面效果

闪屏,就是SplashScreen,也可以说是启动画面,就是启动的时候,闪(展示)一下,持续数秒后,自动关闭。

android的实现非常简单,使用Handler对象的postDelayed方法就可以实现。在这个方法里传递一个Runnable对象和一个延迟的时间。该方法实现了一个延迟执行的效果,延迟的时间由第2个参数指定,单位是毫秒。第一个参数是Runnable对象,里面包含了延迟后需要执行的操作。demo代码如下:

java code:

package com.mstar;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.os.Handler;public class ActSplashScreen extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.shan);// 闪屏的核心代码new Handler().postDelayed(new Runnable() {@Overridepublic void run() {Intent intent = new Intent(ActSplashScreen.this,DialogTest.class); //从启动动画ui跳转到主uistartActivity(intent);ActSplashScreen.this.finish(); // 结束启动动画界面}}, 3000); //启动动画持续3秒钟}}

xml code:

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="fill_parent"android:text="闪一下"></TextView></LinearLayout>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表