Android中闪屏实现方法小结(普通闪屏、倒计时闪屏、倒计时+动画闪屏)
程序员文章站
2024-02-21 22:47:10
一、项目目录结构
二、activity_main.xml代码
一、项目目录结构
二、activity_main.xml代码
<relativelayout 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:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.zgs.splashscreenbyxml.mainactivity" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="主页面" /> </relativelayout>
三、activity_splashscreen.xml代码
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/splash" android:orientation="vertical" android:id="@+id/ll_splashactivity"> <textview android:id="@+id/tv_countdown" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:layout_marginend="20dp" android:layout_margintop="20dp" android:textcolor="@android:color/white" android:textsize="20sp" /> </linearlayout>
四、splashscreenactiviy.java代码
package com.zgs.splashscreenbyxml; import android.app.activity; import android.content.intent; import android.os.bundle; import android.os.countdowntimer; import android.os.handler; import android.view.view; import android.view.window; import android.view.windowmanager; import android.view.animation.animation; import android.view.animation.animation.animationlistener; import android.view.animation.translateanimation; import android.widget.linearlayout; import android.widget.textview; import com.zgs.commonlysplashscreen.r; public class splashscreenactiviy extends activity { private textview tv_countdown; private linearlayout ll_splashactivity; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // 通过下面两行代码也可实现全屏无标题栏显示activity // getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); // this.requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_splashscreen); tv_countdown = (textview) findviewbyid(r.id.tv_countdown); ll_splashactivity = (linearlayout) findviewbyid(r.id.ll_splashactivity); /******************************************************************************** * * 普通闪屏实现方式 * * ******************************************************************************/ /*new handler().postdelayed(new runnable() { @override public void run() { intent intent = new intent(getapplicationcontext(), mainactivity.class); startactivity(intent); finish(); } }, 1000*4);*/ /******************************************************************************** * * 倒计时闪屏实现方式 * * ******************************************************************************/ /*mycountdowntimer mc = new mycountdowntimer(4000, 1000); mc.start(); new handler().postdelayed(new runnable() { @override public void run() { intent intent = new intent(getapplicationcontext(), mainactivity.class); startactivity(intent); finish(); } }, 1000*4);*/ /******************************************************************************** * * 倒计时+动画闪屏实现方式 * * ******************************************************************************/ mycountdowntimer mc = new mycountdowntimer(4000, 1000); mc.start(); new handler().postdelayed(new runnable() { @override public void run() { //左移动画 translateanimation ta = new translateanimation(animation.relative_to_parent, 0, animation.relative_to_parent, -1, animation.relative_to_parent, 0, animation.relative_to_parent, 0); ta.setduration(2000); //设置动画执行的时间 ta.setfillafter(true);//当动画结束后 动画停留在结束位置,然后等启动主界面后将其销毁 ll_splashactivity.startanimation(ta); ta.setanimationlistener(new animationlistener() { @override public void onanimationstart(animation arg0) { } @override public void onanimationrepeat(animation arg0) { } @override public void onanimationend(animation arg0) { intent intent = new intent(getapplicationcontext(), mainactivity.class); startactivity(intent); finish(); } }); } }, 1000*4); } class mycountdowntimer extends countdowntimer { //millisinfuture:倒计时的总数,单位毫秒 //例如 millisinfuture=1000;表示1秒 //countdowninterval:表示间隔多少毫秒,调用一次ontick方法() //例如: countdowninterval =1000;表示每1000毫秒调用一次ontick() public mycountdowntimer(long millisinfuture, long countdowninterval) { super(millisinfuture, countdowninterval); } public void onfinish() { tv_countdown.settext("开始跳转……"); } public void ontick(long millisuntilfinished) { tv_countdown.settext("倒计时(" + millisuntilfinished / 1000 + ")"); } } }
五、mainactivity.java代码
package com.zgs.splashscreenbyxml; import android.app.activity; import android.os.bundle; import com.zgs.commonlysplashscreen.r; public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //为了让闪屏结束后更自然的过度到主界面,去除主界面的启动动画,将下面函数的第一个参数设为0即可 overridependingtransition(0, 0); } }
六、操作演示
以上所述是小编给大家介绍的android中闪屏实现方法小结(普通闪屏、倒计时闪屏、倒计时+动画闪屏),希望对大家有所帮助