亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android Animation實(shí)戰(zhàn)之屏幕底部彈出PopupWindow

 更新時(shí)間:2021年03月15日 08:32:25   作者:無(wú)緣公子  
這篇文章主要為大家介紹了Android Animation動(dòng)畫實(shí)戰(zhàn)項(xiàng)目,屏幕底部彈出PopupWindow,如何實(shí)現(xiàn)?文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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)度等待框》

相關(guān)文章

最新評(píng)論