您的位置:首页 > 移动开发 > Android开发

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

2016-02-23 10:06 1081 查看

以前编程的时候,遇到倒计时的功能时,经常自己去写,但其实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>

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息