今天更新了一个App,打开这个App后弹出的页面是一个广告倒计时的页面,倒计时完毕后进入主界面。于是我闲着没事儿简易实现了一下这个功能,如图:
实现这个效果也很容易,在相应布局问下中添加TextView控件,控件的值就是倒计时的数字,这里我给倒计时添加了一个动画效果,项目的目录结构如下:
AndroidManifest.xml
这里我配置了WelcomeActivity界面作为广告界面,配置它为应用的第一个activity,并且隐藏其标题。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.timewelcome" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > </activity> <activity android:name=".WelcomeActivity" android:label="@string/title_activity_welcome" android:theme="@android:style/Theme.NoTitleBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
WelcomeActivity.java
package com.example.timewelcome; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.view.Window; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.TextView; 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>
实际项目中,本案例及我之前写的两篇博客: ViewPager实现应用的欢迎界面 ,Animation Frame动画实现应用的欢迎界面 都是有逻辑问题的。那么如何正确的实现呢?下面提供我的个人思路:
一般应用的第一次开启都会先出现一个欢迎页面,然后出现导航页面或者广告页面等,然后再进入主页面。所以我们需要判断这个应用是否是第一安装并开启的,这里我们可以使用sharedpreference来保存应用是否开启这个标识符,如果是第一次开启,就从欢迎页面跳转到导航页,然后导航页再跳转到主页面,否则直接从欢迎页跳转到主页面。