Android Animation實戰(zhàn)之屏幕底部彈出PopupWindow
Android動畫的一個實戰(zhàn)內(nèi)容,從屏幕底部滑動彈出PopupWindow。 相信這種效果大家在很多APP上都遇到過,比如需要拍照或者從SD卡選擇圖片,再比如需要分享某些東西時,大多會采用這么一種效果:

那這種效果如何實現(xiàn)呢?
我們仿寫一個這種效果的實例吧:

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)現(xiàn)在定義動畫,要知道該Popupwindow出現(xiàn)時是從頁面底部向上滑動,消失時是從上向下滑動消失,,所以我們需要定義兩個動畫文件:
退出動畫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>
關(guān)于這兩個動畫,此處不再多做解析,讀過我之前博文的都應(yīng)該知道啦,很簡單的,若是看不懂?請點擊此文上方的鏈接學(xué)習(xí)之。
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();
}
});
// 設(shè)置按鈕監(jiān)聽
btn_pick_photo.setOnClickListener(itemsOnClick);
btn_take_photo.setOnClickListener(itemsOnClick);
// 設(shè)置外部可點擊
this.setOutsideTouchable(true);
// mMenuView添加OnTouchListener監(jiān)聽判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框
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;
}
});
/* 設(shè)置彈出窗口特征 */
// 設(shè)置視圖
this.setContentView(this.view);
// 設(shè)置彈出窗體的寬和高
this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
// 設(shè)置彈出窗體可點擊
this.setFocusable(true);
// 實例化一個ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
// 設(shè)置彈出窗體的背景
this.setBackgroundDrawable(dw);
// 設(shè)置彈出窗體顯示時的動畫,從底部向上彈出
this.setAnimationStyle(R.style.take_photo_anim);
}
}
定義要彈出的組件TakePhotoPopWin,它繼承自PopupWindow,具體如何實現(xiàn)的,我備注信息很詳細了。 有一個地方要提醒的是,就是最后要設(shè)置彈出窗體的顯示動畫,this.setAnimationStyle(R.style.take_photo_anim); 這是必不可少的,只有加上了它,才能應(yīng)用動畫效果!
看下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;
}
}
};
這下子,效果就和我一開始傳的圖一致啦!有木有學(xué)會了呢!?
拓展:
玩過APP的大家都知道,在你進入新頁面或者注冊登錄啥的時候,都會彈出一個等待的框框,表示網(wǎng)絡(luò)請求中,你需要耐心等待下,比如微信的等待請求框效果如下:

這里面其中也有個地方用到了動畫,那就是不停旋轉(zhuǎn)的那個小圖標,它其實用的就是旋轉(zhuǎn)動畫!
關(guān)于如何實現(xiàn)這么樣一個旋轉(zhuǎn)等待框,我以前寫過一篇介紹的文章,可查看: 《Android自定義ProgressDialog進度等待框》
- android PopupWindow 和 Activity彈出窗口實現(xiàn)方式
- android popwindow實現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
- Android編程實現(xiàn)popupwindow彈出后屏幕背景變成半透明效果
- Android實現(xiàn)底部彈出PopupWindow背景逐漸變暗效果
- Android入門之PopupWindow用法實例解析
- Android中PopupWindow響應(yīng)返回鍵并關(guān)閉的2種方法
- android使用PopupWindow實現(xiàn)頁面點擊頂部彈出下拉菜單
- Android之用PopupWindow實現(xiàn)彈出菜單的方法詳解
- Android PopupWindow實現(xiàn)右側(cè)、左側(cè)和底部彈出菜單
- Android簡單使用PopupWindow的方法
相關(guān)文章
Android Drawable和Bitmap的轉(zhuǎn)換實例詳解
這篇文章主要介紹了Android Drawable和Bitmap的轉(zhuǎn)換實例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android圖片的Base64編碼與解碼及解碼Base64圖片方法
Base64是網(wǎng)絡(luò)上最常見的用于傳輸8Bit字節(jié)碼的編碼方式之一,Base64就是一種基于64個可打印字符來表示二進制數(shù)據(jù)的方法。接下來通過本文給大家分享Android圖片的Base64編碼與解碼及解碼Base64圖片,需要的朋友參考下吧2017-12-12
android表格效果之ListView隔行變色實現(xiàn)代碼
首先繼承SimpleAdapter再使用重載的Adapter來達到效果,其實主要是需要重載SimpleAdapter,感興趣的朋友可以研究下,希望本文可以幫助到你2013-02-02
Flutter實現(xiàn)密碼強度校驗結(jié)果的示例詳解
我們經(jīng)常在一些網(wǎng)站上看到這樣的密碼強度指示,使用三段線,分別用不同的顏色來表示弱密碼、中等強度密碼和強密碼,本篇我們就用?Flutter?來實現(xiàn)這樣一個密碼強度校驗示例,希望對大家有所幫助2023-08-08
Android懸浮對話框(即點即關(guān)對話框)實現(xiàn)代碼
本文給大家介紹android懸浮對話框和即點即關(guān)閉對話框,本文介紹非常詳細,具有參考借鑒價值,感興趣的朋友一起學(xué)習(xí)吧2016-03-03
深入分析Android ViewStub的應(yīng)用詳解
本篇文章是對Android ViewStub的應(yīng)用進行了詳細的分析介紹,需要的朋友參考下2013-05-05

