Android動畫的一個實戰內容,從螢幕底部滑動彈出PopupWindow。 相信這種效果大家在很多APP上都遇到過,比如需要拍照或者從SD卡選擇圖片,再比如需要分享某些東西時,大多會採用這麼一種效果:
那這種效果如何?呢?
我們仿寫一個這種效果的執行個體吧:
1)我們首先定義一下,快顯視窗的頁面配置組件:take_photo_pop.xml
<?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
<?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:
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的定義:
<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>
就這麼幾步,一個可以從螢幕底部滑動彈出的組件
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進度等待框》