淺談Android的廣告歡迎界面(倒計時)


前些時候就是別人問我他的android APP怎么做一個廣告的歡迎界面,就是過幾秒后自動跳轉到主界面的實現。

也就是下面這種類似的效果。要插什么廣告的話你就換張圖吧。

 

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

 

其實這個東西,怎么講呢。

咱主要的話還是來一個小白都看的懂的一個教程類的文章吧。

 

第一步的話

咱先開始在咱的項目中新建一個anim的文件夾用來存等會要用到的一些  倒計時 的文字的動態效果的吧。(想想還是截個屏吧,怕有些同志還是看不懂...沒別的意思)

        看到了么,就是這樣的,在你的Android項目下的存放資源的那個文件夾中新建一個anim文件夾,再新建一個animation_text.xml

的xml文件,待會就知道有啥用了。

 

咱下面

第二步的話,咱就開始添加內容了。

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <set xmlns:android="http://schemas.android.com/apk/res/android" >
 3 
 4     <alpha
 5         android:duration="1000"
 6         android:fromAlpha="0.0"
 7         android:toAlpha="1.0" />
 8 
 9     <scale
10         android:duration="800"
11         android:fromXScale="1.5"
12         android:fromYScale="1.5"
13         android:pivotX="50%"
14         android:pivotY="50%"
15         android:toXScale="1.0"
16         android:toYScale="1.0" />
17 
18 </set>

 上面的效果的話,如果是不知道這些屬性是什么意思的話那你可以百度的,我這一一講的話就感覺有點啰嗦的了。

咱還是講正題吧,那上面這些寫的有什么用呢。就看下面了,那么我們下面就得開始把那個界面布局出來了吧,然后我們下面就開始吧,

做一個類似我上面的界面吧。咱就用FrameLayout布局了,如果知道是什么布局方式的話,我覺得應該看的懂吧。

 1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:background="@drawable/page24"
 6     tools:context="${relativePackage}.${activityClass}" >
 7 
 8     <LinearLayout
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:layout_gravity="right"
12         android:orientation="horizontal" >
13 
14         <TextView
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"
17             android:layout_gravity="right"
18             android:text="廣告倒計時:"
19             android:textColor="#ffffff"
20             android:textSize="20sp" />
21 
22         <TextView
23             android:id="@+id/textView"
24             android:layout_width="wrap_content"
25             android:layout_height="wrap_content"
26             android:layout_gravity="right"
27             android:text="5"
28             android:textColor="#ffffff"
29             android:textSize="20sp" />
30 
31         <TextView
32             android:layout_width="wrap_content"
33             android:layout_height="wrap_content"
34             android:layout_gravity="right"
35             android:text="s"
36             android:textColor="#ffffff"
37             android:textSize="20sp" />
38     </LinearLayout>
39 
40 </FrameLayout>

下面的話咱就開始要寫怎么在app內部實現的方法了吧,這就到了我們的Java的程序天地來了。

這時候我們就在項目下的src文件下的包里面寫上你的Java文件吧。咱慢慢來,別急。

 1 /**
 2  * 
 3  * 1.聲明界面
 4  * 2.定義變量
 5  * 3.調用類Animation
 6  * 4.寫方法讓它動起來
 7  * @author Rain
 8  *
 9  */
10 public class WelcomeActivity extends Activity{
11 
12      // 聲明控件對象
13     private TextView textView;
14     //聲明時間有多少;
15     private int count = 5;
16     private Animation animation;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         // 下面的話就是去除標題的方法
22         requestWindowFeature(Window.FEATURE_NO_TITLE);
23         setContentView(R.layout.activity_welcome);
24         // 初始化控件對象textView
25         textView = (TextView) findViewById(R.id.textView);
26         animation = AnimationUtils.loadAnimation(this, R.anim.animation_text);
27         handler.sendEmptyMessageDelayed(0, 1000);
28     
29 
30     }
31 
32     //咱在寫一個計算Welcome界面的廣告時間結束后進入主界面的方法
33     private int getCount() {
34         count--;
35         if (count == 0) {
36             Intent intent = new Intent(this, MainActivity.class);
37             startActivity(intent);
38             finish();
39         }
40         return count;
41     }
42 
43     //進行一個消息的處理
44     @SuppressLint("HandlerLeak")
45     private Handler handler = new Handler() {
46         public void handleMessage(android.os.Message msg) {
47             if (msg.what == 0) {
48                 textView.setText(getCount()+"");
49                 handler.sendEmptyMessageDelayed(0, 1000);
50                 animation.reset();
51                 textView.startAnimation(animation);
52             }
53 
54         };
55 
56     };
57 
58 }

用的時候可得注意導入下包哈。

這樣一個會自動跳轉到主界面的廣告界面就完成了。

謝謝觀看。大家可以暢所欲言,發表看法,吾等虛心接受的。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM