android動畫的一個實戰內容,從屏幕底部滑動彈出popupwindow。 相信這種效果大家在很多app上都遇到過,比如需要拍照或者從sd卡選擇圖片,再比如需要分享某些東西時,大多會采用這么一種效果:
那這種效果如何實現呢?
我們仿寫一個這種效果的實例吧:
1)我們首先定義一下,彈出窗口的頁面布局組件:take_photo_pop.xml
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
<?xml version= "1.0" encoding= "utf-8" ?> <relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:gravity= "center_horizontal" android:orientation= "vertical" > <linearlayout android:id= "@+id/pop_layout" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:background= "#ffffff" android:layout_alignparentbottom= "true" android:gravity= "center_horizontal" android:orientation= "vertical" > <textview android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:clickable= "false" android:gravity= "center" android:text= "修改頭像" android:textcolor= "#8a8a8a" android:textsize= "15sp" /> <view android:layout_width= "fill_parent" android:layout_height= "0.1dp" android:layout_marginleft= "10dp" android:layout_marginright= "10dp" android:background= "#00c7c0" /> <button android:id= "@+id/btn_take_photo" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "拍照" android:textcolor= "#0e61aa" android:textsize= "18sp" /> <button android:id= "@+id/btn_pick_photo" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "從相冊選擇" android:textcolor= "#0e61aa" android:textsize= "18sp" /> <button android:id= "@+id/btn_cancel" android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:layout_marginbottom= "15dip" android:layout_margintop= "15dip" android:text= "取消" android:textcolor= "#0e61aa" android:textsize= "18sp" android:textstyle= "bold" /> </linearlayout> </relativelayout> |
2)現在定義動畫,要知道該popupwindow出現時是從頁面底部向上滑動,消失時是從上向下滑動消失,,所以我們需要定義兩個動畫文件:
退出動畫pop_exit_anim.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "1.0" encoding= "utf-8" ?> <set xmlns:android= "http://schemas.android.com/apk/res/android" > <translate android:duration= "200" android:fromydelta= "0" android:toydelta= "50%p" /> <alpha android:duration= "200" android:fromalpha= "1.0" android:toalpha= "0.0" /> </set> 顯示動畫pop_enter_anim.xml <?xml version= "1.0" encoding= "utf-8" ?> <set xmlns:android= "http://schemas.android.com/apk/res/android" > <translate android:duration= "200" android:fromydelta= "100%p" android:toydelta= "0" /> <alpha android:duration= "200" android:fromalpha= "0.0" android:toalpha= "1.0" /> </set> |
關于這兩個動畫,此處不再多做解析,讀過我之前博文的都應該知道啦,很簡單的,若是看不懂?請點擊此文上方的鏈接學習之。
3)自定義彈出框popupwindow:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
import android.content.context; import android.graphics.drawable.colordrawable; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.widget.button; import android.widget.popupwindow; import android.widget.relativelayout; public class takephotopopwin extends popupwindow { private context mcontext; private view view; private button btn_take_photo, btn_pick_photo, btn_cancel; public takephotopopwin(context mcontext, view.onclicklistener itemsonclick) { this .view = layoutinflater.from(mcontext).inflate(r.layout.take_photo_pop, null ); btn_take_photo = (button) view.findviewbyid(r.id.btn_take_photo); btn_pick_photo = (button) view.findviewbyid(r.id.btn_pick_photo); btn_cancel = (button) view.findviewbyid(r.id.btn_cancel); // 取消按鈕 btn_cancel.setonclicklistener( new view.onclicklistener() { public void onclick(view v) { // 銷毀彈出框 dismiss(); } }); // 設置按鈕監聽 btn_pick_photo.setonclicklistener(itemsonclick); btn_take_photo.setonclicklistener(itemsonclick); // 設置外部可點擊 this .setoutsidetouchable( true ); // mmenuview添加ontouchlistener監聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框 this .view.setontouchlistener( new view.ontouchlistener() { public boolean ontouch(view v, motionevent event) { int height = view.findviewbyid(r.id.pop_layout).gettop(); int y = ( int ) event.gety(); if (event.getaction() == motionevent.action_up) { if (y < height) { dismiss(); } } return true ; } }); /* 設置彈出窗口特征 */ // 設置視圖 this .setcontentview( this .view); // 設置彈出窗體的寬和高 this .setheight(relativelayout.layoutparams.match_parent); this .setwidth(relativelayout.layoutparams.match_parent); // 設置彈出窗體可點擊 this .setfocusable( true ); // 實例化一個colordrawable顏色為半透明 colordrawable dw = new colordrawable( 0xb0000000 ); // 設置彈出窗體的背景 this .setbackgrounddrawable(dw); // 設置彈出窗體顯示時的動畫,從底部向上彈出 this .setanimationstyle(r.style.take_photo_anim); } } |
定義要彈出的組件takephotopopwin,它繼承自popupwindow,具體如何實現的,我備注信息很詳細了。 有一個地方要提醒的是,就是最后要設置彈出窗體的顯示動畫,this.setanimationstyle(r.style.take_photo_anim); 這是必不可少的,只有加上了它,才能應用動畫效果!
看下take_photo_anim style的定義:
1
2
3
4
|
<style name= "take_photo_anim" parent= "android:animation" > <item name= "android:windowenteranimation" > @anim /pop_enter_anim</item> <item name= "android:windowexitanimation" > @anim /pop_exit_anim</item> </style> |
就這么幾步,一個可以從屏幕底部滑動彈出的組件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public void showpopformbottom(view view) { takephotopopwin takephotopopwin = new takephotopopwin( this , onclicklistener); //showatlocation(view parent, int gravity, int x, int y) takephotopopwin.showatlocation(findviewbyid(r.id.main_view), gravity.center, 0 , 0 ); } private view.onclicklistener onclicklistener = new view.onclicklistener() { @override public void onclick(view v) { switch (v.getid()) { case r.id.btn_take_photo: system.out.println( "btn_take_photo" ); break ; case r.id.btn_pick_photo: system.out.println( "btn_pick_photo" ); break ; } } }; |
這下子,效果就和我一開始傳的圖一致啦!有木有學會了呢!?
拓展:
玩過app的大家都知道,在你進入新頁面或者注冊登錄啥的時候,都會彈出一個等待的框框,表示網絡請求中,你需要耐心等待下,比如微信的等待請求框效果如下:
這里面其中也有個地方用到了動畫,那就是不停旋轉的那個小圖標,它其實用的就是旋轉動畫!
關于如何實現這么樣一個旋轉等待框,我以前寫過一篇介紹的文章,可查看: 《android自定義progressdialog進度等待框》