Android Animation實(shí)戰(zhàn)之屏幕底部彈出PopupWindow
Android動(dòng)畫的一個(gè)實(shí)戰(zhàn)內(nèi)容,從屏幕底部滑動(dòng)彈出PopupWindow。 相信這種效果大家在很多APP上都遇到過(guò),比如需要拍照或者從SD卡選擇圖片,再比如需要分享某些東西時(shí),大多會(huì)采用這么一種效果:
那這種效果如何實(shí)現(xiàn)呢?
我們仿寫一個(gè)這種效果的實(shí)例吧:
1)我們首先定義一下,彈出窗口的頁(yè)面布局組件: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="從相冊(cè)選擇" 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)在定義動(dòng)畫,要知道該P(yáng)opupwindow出現(xiàn)時(shí)是從頁(yè)面底部向上滑動(dòng),消失時(shí)是從上向下滑動(dòng)消失,,所以我們需要定義兩個(gè)動(dòng)畫文件:
退出動(dòng)畫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> 顯示動(dòng)畫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)于這兩個(gè)動(dòng)畫,此處不再多做解析,讀過(guò)我之前博文的都應(yīng)該知道啦,很簡(jiǎn)單的,若是看不懂?請(qǐng)點(diǎn)擊此文上方的鏈接學(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)聽(tīng) btn_pick_photo.setOnClickListener(itemsOnClick); btn_take_photo.setOnClickListener(itemsOnClick); // 設(shè)置外部可點(diǎn)擊 this.setOutsideTouchable(true); // mMenuView添加OnTouchListener監(jiān)聽(tīng)判斷獲取觸屏位置如果在選擇框外面則銷毀彈出框 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è)置彈出窗體可點(diǎn)擊 this.setFocusable(true); // 實(shí)例化一個(gè)ColorDrawable顏色為半透明 ColorDrawable dw = new ColorDrawable(0xb0000000); // 設(shè)置彈出窗體的背景 this.setBackgroundDrawable(dw); // 設(shè)置彈出窗體顯示時(shí)的動(dòng)畫,從底部向上彈出 this.setAnimationStyle(R.style.take_photo_anim); } }
定義要彈出的組件TakePhotoPopWin,它繼承自PopupWindow,具體如何實(shí)現(xiàn)的,我備注信息很詳細(xì)了。 有一個(gè)地方要提醒的是,就是最后要設(shè)置彈出窗體的顯示動(dòng)畫,this.setAnimationStyle(R.style.take_photo_anim); 這是必不可少的,只有加上了它,才能應(yīng)用動(dò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>
就這么幾步,一個(gè)可以從屏幕底部滑動(dòng)彈出的組件
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; } } };
這下子,效果就和我一開(kāi)始傳的圖一致啦!有木有學(xué)會(huì)了呢???
拓展:
玩過(guò)APP的大家都知道,在你進(jìn)入新頁(yè)面或者注冊(cè)登錄啥的時(shí)候,都會(huì)彈出一個(gè)等待的框框,表示網(wǎng)絡(luò)請(qǐng)求中,你需要耐心等待下,比如微信的等待請(qǐng)求框效果如下:
這里面其中也有個(gè)地方用到了動(dòng)畫,那就是不停旋轉(zhuǎn)的那個(gè)小圖標(biāo),它其實(shí)用的就是旋轉(zhuǎn)動(dòng)畫!
關(guān)于如何實(shí)現(xiàn)這么樣一個(gè)旋轉(zhuǎn)等待框,我以前寫過(guò)一篇介紹的文章,可查看: 《Android自定義ProgressDialog進(jìn)度等待框》
- android PopupWindow 和 Activity彈出窗口實(shí)現(xiàn)方式
- android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
- Android編程實(shí)現(xiàn)popupwindow彈出后屏幕背景變成半透明效果
- Android實(shí)現(xiàn)底部彈出PopupWindow背景逐漸變暗效果
- Android入門之PopupWindow用法實(shí)例解析
- Android中PopupWindow響應(yīng)返回鍵并關(guān)閉的2種方法
- android使用PopupWindow實(shí)現(xiàn)頁(yè)面點(diǎn)擊頂部彈出下拉菜單
- Android之用PopupWindow實(shí)現(xiàn)彈出菜單的方法詳解
- Android PopupWindow實(shí)現(xiàn)右側(cè)、左側(cè)和底部彈出菜單
- Android簡(jiǎn)單使用PopupWindow的方法
相關(guān)文章
Android 數(shù)據(jù)存儲(chǔ)方式有哪幾種
android為數(shù)據(jù)存儲(chǔ)提供了五種方式,有SharedPreferences、文件存儲(chǔ)、SQLite數(shù)據(jù)庫(kù)、ContentProvider、網(wǎng)絡(luò)存儲(chǔ),對(duì)android數(shù)據(jù)存儲(chǔ)方式感興趣的朋友可以通過(guò)本文學(xué)習(xí)一下2015-11-11Android自定義View實(shí)現(xiàn)兩種二維碼的掃描效果
這篇文章主要為大家詳細(xì)介紹了Android如何自定義View實(shí)現(xiàn)兩種二維碼的掃描效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01Android應(yīng)用程序窗口(Activity)窗口對(duì)象(Window)創(chuàng)建指南
本文將詳細(xì)介紹Android應(yīng)用程序窗口(Activity)的窗口對(duì)象(Window)的創(chuàng)建過(guò)程,需要了解的朋友可以參考下2012-12-12Flutter?DateTime獲取本月的開(kāi)始時(shí)間與結(jié)束時(shí)間方法
這篇文章主要為大家介紹了Flutter?DateTime獲取本月的開(kāi)始時(shí)間與結(jié)束時(shí)間方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2023-05-05手機(jī)/移動(dòng)前端開(kāi)發(fā)需要注意的20個(gè)要點(diǎn)
本文主要介紹了手機(jī)/移動(dòng)前端開(kāi)發(fā)需要注意的20個(gè)要點(diǎn),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03Android實(shí)現(xiàn)viewpager實(shí)現(xiàn)循環(huán)輪播效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)viewpager實(shí)現(xiàn)循環(huán)輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Android5.0多種側(cè)滑欄效果實(shí)例代碼
這篇文章主要介紹了Android5.0多種側(cè)滑欄效果實(shí)例代碼的相關(guān)資料,本文圖文并茂介紹的非常詳細(xì),需要的朋友可以參考下2016-09-09解決android studio中使用monitor工具無(wú)法打開(kāi)data文件夾問(wèn)題
這篇文章主要介紹了解決android studio中使用monitor工具無(wú)法打開(kāi)data文件夾問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04