PopWindow 從底部動畫效果彈出。

MelissaBall 7年前發布 | 15K 次閱讀 安卓開發 Android開發 移動開發

popwindow從底部彈出,動畫,和適配不同手機:

現在的app中,popwindow的使用的情況挺多的,列入拍照的時候的彈出的對話框。

彈窗

需求

實現一個對話框,從底部逐漸移動出現,。消失的時候,逐漸向下移除屏幕,(伴隨著動畫)。

  1. 點擊 顯示 按鈕時,一個dialog對話框從底部慢慢向上彈出。
  2. 關閉dialog時, dialog緩慢的移動向底部消失。很平滑的效果。
  3. 當對話框彈出來的時候,底層的布局為灰色,對話框消失的時候,屏幕可以恢復回來。

    動態

    點擊按鈕彈出對話框

    btn_pop_anim.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    PopWindow(v);
    } 
    });

    popwindow的的代碼

    動態圖是放大后的效果,實際的是三個item。

private void PopWindow(View v) { 
Button btItem1, btItem2, btItem3; 
View view = LayoutInflater.from(PopWindowActivity.this).inflate(R.layout.popwindow_layout, null);
 //設置屏幕的高度和寬度 
final PopupWindow pop = new PopupWindow(view, getScreenWidth(this)*4/5, getScreentHeight()*3/10);
 //如果不設置背景顏色的話,無法是pop dimiss掉。
pop.setBackgroundDrawable(getResources().getDrawable(R.drawable.popupwindow_background));  
pop.setOutsideTouchable(true); pop.setAnimationStyle(R.style.MyPopupWindow_anim_style);

btItem1 = (Button) view.findViewById(R.id.bt_item1); 
btItem1.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
Toast.makeText(PopWindowActivity.this, "相機", Toast.LENGTH_SHORT).show(); pop.dismiss(); } 
});

btItem2 = (Button) view.findViewById(R.id.bt_item2); 
btItem2.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) {
Toast.makeText(PopWindowActivity.this, "圖庫", Toast.LENGTH_SHORT).show(); pop.dismiss(); } 
});
btItem3= (Button) view.findViewById(R.id.bt_item3); 
btItem3.setOnClickListener(new View.OnClickListener() {
 @Override 
public void onClick(View v) { Toast.makeText(PopWindowActivity.this, "取消", Toast.LENGTH_SHORT).show(); pop.dismiss(); } }); 

/** * 設置popwindow的彈出的位置. *
 1:首先要判斷是否有navigation bar。如果有的的話,要把他們的高度給加起來。 * * 
2:showAtLocation();是pop相對于屏幕而言的。 * * 
3:如果是 pop.showAsDropDown();則是相對于你要點擊的view的位置。設置的坐標。
 */
 if(checkDeviceHasNavigationBar2(this)){ 
int heigth_tobottom =100+getNavigationBarHeight(); 
pop.showAtLocation(v, Gravity.BOTTOM,0,heigth_tobottom);
}else { 
pop.showAtLocation(v, Gravity.BOTTOM,0,100);
} 
//設置 背景的顏色為 0.5f 的透明度 
backgroundAlpha(0.5f); pop.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() {
 //當popwindow消失的時候,恢復背景的顏色。 backgroundAlpha(1.0f); } 
});
}

這是popwindow布局的代碼

popWindow 布局 R.layout.popwindow_layout。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:id="@+id/textView1" android:layout_width="match_parent"
android:gravity="center"
android:layout_height="wrap_content" 
android:layout_margin="2dp" 
android:text="這是PopupWindow" /> 
<Button android:id="@+id/bt_item1" android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_margin="3dp" 
android:text="相機" /> 
<Button android:id="@+id/bt_item2" android:layout_width="match_parent" android:layout_height="wrap_content" 
android:layout_margin="3dp" 
android:text="圖庫" /> 
<Button android:id="@+id/bt_item3" android:layout_width="match_parent" android:layout_height="wrap_content"
android:layout_margin="3dp" android:text="取消" /></LinearLayout>

獲取屏幕的寬和高

1 獲取屏幕的寬和高的代碼。主要有兩種方式。

2 方法1 的方法已經被遺棄了,推薦用方法2 的方法,除獲得屏幕的寬和高外還可以獲得屏幕的密度。

方法一,代碼如下:

WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);  
Display display = wm.getDefaultDisplay();
  Log.i(tag, "屏幕尺寸1: 寬度 = "+display.getWidth()+"高度 = :"+display.getHeight()  )

方法二,代碼如下:

DisplayMetrics dm =getResources().getDisplayMetrics(); 
 int w_screen = dm.widthPixels;  int h_screen = dm.heightPixels;

為popwindow設置動畫。

設置動畫

pop.setAnimationStyle(R.style.MyPopupWindow_anim_style);

  • 設置style 在res-->values-->style 中設置。代碼如下:
    <!-- PopupWindow彈出/隱藏動畫 12-28 --> 
    <style name="MyPopupWindow_anim_style"> 
    <itemname="android:windowEnterAnimation">@anim/popupwindow_show_anim</item> 
    <itemname="android:windowExitAnimation">@anim/popupwindow_hidden_anim</item> 
    </style>
  • 進入時的動畫,代碼如下:
    popupwindow_show_anim.xml> 要在res--> 下面建立一個文件夾anim。進入和推出是的動畫。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<translate android:duration="1000" android:fromYDelta="10%p" 
android:toYDelta="0" /> 
<alpha android:duration="1000" android:fromAlpha="0.0" 
android:toAlpha="1.0" />
</set>
  • 推出時的動畫,代碼如下:

    popupwindow_hidden_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
<translate android:duration="1000" android:fromYDelta="0" 
android:toYDelta="10%p" /> 
<alpha android:duration="1000" 
android:fromAlpha="1.0" android:toAlpha="0.0" />
</set>

需要注意的是要為 popwindow設置 背景顏色,不然點擊外部區域,無法使其消失,背景顏色如下:res--> drawable下面popupwindow_background.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
<solid android:color="#e1859e" />
<corners android:radius="20dip" /> 
<stroke android:width="2dip" android:color="#1cb0ba" />
</shape>

上面就是簡單的popwindow 動畫。

  1. 設置背景顏色

    pop.setBackgroundDrawable(getResources().getDrawable (R.drawable.popupwindow_background));
  2. pop.setOutsideTouchable(true);

  3. 設置動畫
    pop.setAnimationStyle(R.style.MyPopupWindow_anim_style);
  4. 在彈出pop的時候屏幕背景顏色的變化 .
    rubybackgroundAlpha(0.5f);
  5. 在消失的時候 .
    rubypop.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { backgroundAlpha(1.0f); } });
  6. 從屏幕下部彈出 可以選擇很多的方向。
    pop.showAtLocation(v, Gravity.BOTTOM,0,100);

遇到的問題

  1. 在nexus 5 測試的時候一直存在的問題是,我設置為距離屏幕底部為100 但是實際的效果是在屏幕的下方,緊貼著屏幕出現

    效果

    后來在網上搜索是因為 nexus 有Navigationbar的原因,如果設置的高度 小于 navigation的高度。系統會自動判斷讓效果如圖。
  2. 所以要判斷手機的系統是否有Navigationbar效果。
  3. 第一種方法 :
    /** * 判斷設備是否有虛擬按鍵(navifationbar)。第一種方法 */
    public static boolean checkDeviceHasNavigationBar(Context activity) {
    //通過判斷設備是否有返回鍵、菜單鍵(不是虛擬鍵,是手機屏幕外的按鍵)來確定是否有navigation bar 
    boolean hasMenuKey = ViewConfiguration.get(activity) 
    .hasPermanentMenuKey(); boolean hasBackKey = KeyCharacterMap
    .deviceHasKey(KeyEvent.KEYCODE_BACK); 
    if (!hasMenuKey && !hasBackKey) { 
    // 做任何你需要做的,這個設備有一個導航欄 
    return true; 
    } 
    return false;
    }
  4. 第二種方法 :
    /** * /獲取是否存在虛擬按鍵 NavigationBar:如果是有就返回true,如果是沒有就是返回的false。第二種方法 */ 
    private static boolean checkDeviceHasNavigationBar2(Context context) {
    boolean hasNavigationBar = false; Resources rs = context.getResources(); 
    int id = rs.getIdentifier("config_showNavigationBar", "bool", "android");
    if (id > 0) { hasNavigationBar = rs.getBoolean(id); }
    try { Class systemPropertiesClass = Class.forName("android.os.SystemProperties"); 
    Method m = systemPropertiesClass.getMethod("get", String.class);
    String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys"); 
    if ("1".equals(navBarOverride)) { hasNavigationBar = false; 
    } else if ("0".equals(navBarOverride)) { 
    hasNavigationBar = true; } 
    } catch (Exception e) { } return hasNavigationBar; }``` * 獲取navigationbar的高度。代碼如下:``` ruby/** * 獲取navigationbar的高度。 */ private int getNavigationBarHeight() { Resources resources = this.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android"); int height = resources.getDimensionPixelSize(resourceId); return height; }

    解決的思路是如果是有navigationbar的手機,

    pop.showAtLocation(v, Gravity.BOTTOM,X,Y);
    如果有Y則加上navigationbar的高度。代碼如下:
    if(checkDeviceHasNavigationBar2(this)){ 
    int heigth_tobottom =100+getNavigationBarHeight();
    pop.showAtLocation(v, Gravity.BOTTOM,0,heigth_tobottom); 
    }else {
    pop.showAtLocation(v, Gravity.BOTTOM,0,100); 
    }
    效果 如下圖

    效果圖

    有錯誤的地方請大家多指正,評論,十分感謝你的耐心閱讀:relaxed:,圖片太大不知道在簡書,如何調整大小。。。

 

來自:http://www.jianshu.com/p/1a009b964d00

 

 本文由用戶 MelissaBall 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!