前些時候就是別人問我他的android APP怎么做一個廣告的歡迎界面,就是過幾秒后自動跳轉到主界面的實現(xiàn)。
也就是下面這種類似的效果。要插什么廣告的話你就換張圖吧。

那么我就思考了下,就用了android 的一個動畫類Animation...其實在Android 的API開發(fā)文檔上就有的一個東西。自己可以去查下看。就像下面的這個圖上面的一樣的。也是屬于界面View 下的一個類方法...

其實這個東西,怎么講呢。
咱主要的話還是來一個小白都看的懂的一個教程類的文章吧。
第一步的話
咱先開始在咱的項目中新建一個anim的文件夾用來存等會要用到的一些 倒計時 的文字的動態(tài)效果的吧。(想想還是截個屏吧,怕有些同志還是看不懂...沒別的意思)
看到了么
看到了么,就是這樣的,在你的Android項目下的存放資源的那個文件夾中新建一個anim文件夾,再新建一個animation_text.xml
的xml文件,待會就知道有啥用了。
咱下面
第二步的話,咱就開始添加內(nèi)容了。
<?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>
上面的效果的話,如果是不知道這些屬性是什么意思的話那你可以百度的,我這一一講的話就感覺有點啰嗦的了。
咱還是講正題吧,那上面這些寫的有什么用呢。就看下面了,那么我們下面就得開始把那個界面布局出來了吧,然后我們下面就開始吧,
做一個類似我上面的界面吧。咱就用FrameLayout布局了,如果知道是什么布局方式的話,我覺得應該看的懂吧。
<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/page24" 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="s" android:textColor="#ffffff" android:textSize="20sp" /> </LinearLayout></FrameLayout>下面的話咱就開始要寫怎么在app內(nèi)部實現(xiàn)的方法了吧,這就到了我們的Java的程序天地來了。
這時候我們就在項目下的src文件下的包里面寫上你的Java文件吧。咱慢慢來,別急。
/** * * 1.聲明界面 * 2.定義變量 * 3.調(diào)用類Animation * 4.寫方法讓它動起來 * @author Rain * */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 = (TextView) findViewById(R.id.textView); animation = AnimationUtils.loadAnimation(this, R.anim.animation_text); handler.sendEmptyMessageDelayed(0, 1000); } //咱在寫一個計算Welcome界面的廣告時間結束后進入主界面的方法 private int getCount() { count--; if (count == 0) { Intent intent = new Intent(this, MainActivity.class); startActivity(intent); finish(); } return count; } //進行一個消息的處理 @SuppressLint("HandlerLeak") 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); } }; };}用的時候可得注意導入下包哈。
這樣一個會自動跳轉到主界面的廣告界面就完成了。
總結
以上所述是小編給大家介紹的Android 實現(xiàn)廣告歡迎界面(倒計時),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!
新聞熱點
疑難解答
圖片精選