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

所属分类: 软件编程 / Android 阅读数: 412
收藏 0 赞 0 分享

以前编程的时候,遇到倒计时的功能时,经常自己去写,但其实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;
@Override
protected 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() {
@Override
public 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{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shan);
// 闪屏的核心代码
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(ActSplashScreen.this,DialogTest.class); //从启动动画ui跳转到主ui
startActivity(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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="闪一下"
>
</TextView>
</LinearLayout>

更多精彩内容其他人还在看

老生常谈Android HapticFeedback(震动反馈)

下面小编就为大家带来一篇老生常谈Android HapticFeedback(震动反馈)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详谈OnTouchListener与OnGestureListener的区别

下面小编就为大家带来一篇详谈OnTouchListener与OnGestureListener的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android仿知乎悬浮功能按钮FloatingActionButton效果

前段时间在看属性动画,恰巧这个按钮的效果可以用属性动画实现,下面通过本文给大家分享adroid仿知乎悬浮功能按钮FloatingActionButton效果,需要的朋友参考下吧
收藏 0 赞 0 分享

解决Android V7后自定义Toolbar、ActionBar左侧有空白问题

这篇文章主要介绍的Android V7后自定义Toolbar、ActionBar左侧有空白问题的解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android常见控件使用详解

这篇文章主要为大家详细介绍了Android常见控件的使用方法,包括ProgressBar进度条控件、AlertDialog对话框控件等,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android实现简洁的APP更新dialog数字进度条

这篇文章主要为大家详细介绍了Android实现简洁的APP更新dialog数字进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 判断当前语言环境是否是中文环境

本文主要介绍了Android 判断当前语言环境是否是中文环境的方法。具有很好的参考价值。下面跟着小编一起来看下吧
收藏 0 赞 0 分享

详谈Android中Matrix的set、pre、post的区别

下面小编就为大家带来一篇详谈Android中Matrix的set、pre、post的区别。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现登录界面记住密码的存储

这篇文章主要为大家详细介绍了Android SharedPreferrences实现登录界面记住密码的存储,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android 使用SharedPreferrences储存密码登录界面记住密码功能

Android存储方式有很多种,在这里所用的存储方式是SharedPreferrences, 其采用了Map数据结构来存储数据,以键值的方式存储,可以简单的读取与写入,下面通过实例代码给大家讲解下,需要的朋友参考下吧
收藏 0 赞 0 分享
查看更多