在登陸一些頁面時(shí),通常能看見“一閃而過”效果并進(jìn)入頁面。下面看看是怎樣實(shí)現(xiàn)這樣的效果的吧
首先,在布局里(可以說和平常沒有什么不同),劃線部分是進(jìn)度條:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
< 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:background = "@drawable/bg" > < ImageView android:id = "@+id/welcome" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_centerHorizontal = "true" android:layout_marginBottom = "45dp" android:src = "@drawable/welcome" /> < ProgressBar android:id = "@+id/progressBar" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_below = "@+id/welcome" android:layout_centerHorizontal = "true" android:layout_marginBottom = "70dp" /> < TextView android:id = "@+id/textView1" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_below = "@+id/progressBar" android:layout_centerHorizontal = "true" android:padding = "@dimen/padding_medium" android:text = "@string/welcome" android:textAppearance = "?android:attr/textAppearanceMedium" tools:context = ".MainActivity" /> </ RelativeLayout > |
在String中定義:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< resources > < string name = "app_name" >ShanP01</ string > < string name = "welcome" >歡迎加入!\n一起快樂學(xué)習(xí)!</ string >//(\n)實(shí)現(xiàn)換行 < string name = "menu_settings" >Settings</ string > < string name = "title_activity_main" >MainActivity</ string > < string name = "title_study" >學(xué)習(xí)</ string > < string name = "title_search" >搜查</ string > < string name = "title_game" >游戲</ string > < string name = "title_save" >保存</ string > < string name = "title_help" >幫助</ string > < string name = "title_activity_welcome" >WelcomeActivity</ string > </ resources > |
如果想在運(yùn)行項(xiàng)目時(shí)不顯示標(biāo)題欄,則在想隱藏標(biāo)題欄的Activity中加一句即可(在AndroidManifest.xml文件中):
1
|
android:theme= "@android:style/Theme.NoTitleBar" |
最主要的實(shí)現(xiàn)方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
public class WelcomeActivity extends Activity { private ImageView welcomeImage; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); welcomeImage=(ImageView) this .findViewById(R.id.welcome); AlphaAnimation alphaAnimation= new AlphaAnimation( 0 .1f, 1 .0f); ////定義一個(gè)具有淡入效果的對(duì)象 alphaAnimation.setDuration( 3000 ); //定義閃屏?xí)r間(毫秒) welcomeImage.startAnimation(alphaAnimation); alphaAnimation.setAnimationListener( new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { Intent intent= new Intent(); intent.setClass(WelcomeActivity. this , MainActivity. class ); //定義閃屏效果從哪一界面跳到哪一頁面 startActivity(intent); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_welcome, menu); return true ; } } |
其實(shí)閃屏效果不止這一種,但這是我認(rèn)為簡便的一種。還有一種:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); welcomeImage=(ImageView) this .findViewById(R.id.welcome); AlphaAnimation alphaAnimation= new AlphaAnimation( 0 .1f, 1 .0f); ////定義1個(gè)具有淡入效果的對(duì)象 welcomeImage.startAnimation(alphaAnimation); new Handler().postDelayed( new Runnable() { @Override public void run() { Intent intent= new Intent(); intent.setClass(WelcomeActivity. this , MainActivity. class ); startActivity(intent); finish(); } }, 3000 ); //細(xì)心不要漏了 } |
你覺得哪一種更適合你呢?
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/duyuping/article/details/8221437