APP实用开发——SplashScreen页面倒计时跳转

来源:互联网 发布:pb数据窗口列标题修改 编辑:程序博客网 时间:2024/06/16 12:44

Handler对象的postDelayed方法

闪屏,就是SplashScreen,也可以说是启动画面,就是启动的时候,闪(展示)一下,持续数秒后,自动关闭。
android的实现非常简单,使用Handler对象的postDelayed方法就可以实现。在这个方法里传递一个Runnable对象和一个延迟的时间。该方法实现了一个延迟执行的效果,延迟的时间由第2个参数指定,单位是毫秒。第一个参数是Runnable对象,里面包含了延迟后需要执行的操作。demo代码如下:

public class JumpActivity extends Activity {    TextView textView;    int time=3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.jump);        textView= (TextView) findViewById(R.id.textview);        handler.postDelayed(runnable,3000);    }    Handler handler=new Handler();    Runnable runnable=new Runnable() {        @Override        public void run() {            time--;            handler.postDelayed(this,2000);            textView.setText("跳转:"+time+"秒");            if(time==0){                Intent intent=new Intent(JumpActivity.this,TestActivity.class);                startActivity(intent);                finish();            }else {                textView.setOnClickListener(new View.OnClickListener() {                    @Override                    public void onClick(View v) {                        Intent intent=new Intent(JumpActivity.this,TestActivity.class);                        startActivity(intent);                        //结束线程                        handler.removeCallbacks(runnable);                        finish();                    }                });            }        }    };}

CountDownTimer类

以前编程的时候,遇到倒计时的功能时,经常自己去写,但其实Android已经帮封装好了一个倒计时类CountDownTimer,其实是将后台线程的创建和Handler队列封装成为了一个方便的类调用。
闪屏页用到了handler和CountDownTimer类,还需配置一下Activity的主题,这里是:android:theme=”@android:style/Theme.NoTitleBar.Fullscreen” 全屏主题的意思。

public class Welcome extends Activity {      private static final String MY_DB_FirstCheck = "my_db";      private TextView tv;      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          this.requestWindowFeature(Window.FEATURE_NO_TITLE);          this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                  WindowManager.LayoutParams.FLAG_FULLSCREEN);          setContentView(R.layout.welcome_layout);          tv= (TextView) findViewById(R.id.TView_countdown);          final SharedPreferences sharedpreferences = getSharedPreferences(                  MY_DB_FirstCheck, Context.MODE_PRIVATE);          final boolean hasVisited = sharedpreferences.getBoolean("hasVisited",                  false);          countdowntimer = new MyCountdownTimer(3000, 1000);          countdowntimer.start();          handler.postDelayed(new Runnable() {              @Override              public void run() {                  if (!hasVisited) {                      Intent intent = new Intent();                      intent.setClass(Welcome.this, Login.class);                      startActivity(intent);                      Editor edit = sharedpreferences.edit();                      edit.putBoolean("hasVisited", true);                      edit.commit();                  } else {                      Intent intent = new Intent();                      intent.setClass(Welcome.this, MainActivity.class);                      startActivity(intent);                  }                  finish();              }          }, 3000);      }      private Handler handler = new Handler();      private MyCountdownTimer countdowntimer;  /** * 继承 CountDownTimer 防范 * 参数:  倒计时总数,单位为毫秒、每隔多久调用onTick一次 * 重写 父类的方法 onTick() 、 onFinish() */    protected class MyCountdownTimer extends CountDownTimer {          public MyCountdownTimer(long millisInFuture, long countDownInterval) {              super(millisInFuture, countDownInterval);          }          @Override          public void onTick(long millisUntilFinished) {             tv.setText("倒计时(" + millisUntilFinished / 1000 + ")");          }          @Override          public void onFinish() {              tv.setText("正在跳转");         }      }  }  

sendEmptyMessageDelayed

这里写图片描述

public class WelcomeActivity extends Activity {    // 声明控件对象    private TextView textView;    private int count = 5;    private Animation animation;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        // 去除标题        requestWindowFeature(Window.FEATURE_NO_TITLE);        setContentView(R.layout.activity_welcome);        // 初始化控件对象        textView = (TextView) findViewById(R.id.textView);        animation = AnimationUtils.loadAnimation(this, R.anim.animation_text);        //textView.startAnimation(animation);        handler.sendEmptyMessageDelayed(0, 1000);    }    private int getCount() {        count--;        if (count == 0) {            Intent intent = new Intent(this, MainActivity.class);            startActivity(intent);            finish();        }        return count;    }    private Handler handler = new Handler() {        public void handleMessage(android.os.Message msg) {            if (msg.what == 0) {                textView.setText(getCount()+"");                handler.sendEmptyMessageDelayed(0, 1000);                animation.reset();                textView.startAnimation(animation);            }        };    };}

activity_welcome.xml

<FrameLayout 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"    android:background="@drawable/image"    tools:context="${relativePackage}.${activityClass}" >    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="right"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="right"            android:text="广告倒计时:"            android:textColor="#ffffff"            android:textSize="20sp" />        <TextView            android:id="@+id/textView"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="right"            android:text="5"            android:textColor="#ffffff"            android:textSize="20sp" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="right"            android:text="秒"            android:textColor="#ffffff"            android:textSize="20sp" />    </LinearLayout></FrameLayout>

动画的xml文件animation_text.xml存放在了res/anim目录下,实现透明度和缩放的动画。
animation_text.xml

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <alpha        android:duration="1000"        android:fromAlpha="0.0"        android:toAlpha="1.0" />    <scale        android:duration="800"        android:fromXScale="1.5"        android:fromYScale="1.5"        android:pivotX="50%"        android:pivotY="50%"        android:toXScale="1.0"        android:toYScale="1.0" /></set>
0 0
原创粉丝点击