android實(shí)現(xiàn)篩選菜單效果
前言
由于android M的popupwindow與之前版本不一致,筆者找不到能夠代碼監(jiān)聽(tīng)物理返回鍵的方式,故另尋方式實(shí)現(xiàn)篩選菜單。5.0及之前的版本可用popupwindow實(shí)現(xiàn),詳情請(qǐng)參考popupwindow用法。
本篇采用Dialog實(shí)現(xiàn)。
實(shí)現(xiàn)步驟
1、設(shè)置主題
一般設(shè)置如下
<style name="Translucent_NoTitle" parent="android:style/Theme.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:background">#00000000</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowAnimationStyle">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:colorBackgroundCacheHint">@null</item> <item name="android:windowIsTranslucent">true</item> <item name="android:backgroundDimEnabled">false</item><span style="white-space:pre"> </span>背景暗淡效果 </style>
也可使用android.R.style.Theme_Panel和android.R.style.Theme_Light_Panel。android.R.style.Theme_Panel代碼如下,其與上面是一樣的。
<style name="Theme.Panel"> <item name="windowBackground">@color/transparent</item> <item name="colorBackgroundCacheHint">@null</item> <item name="windowFrame">@null</item> <item name="windowContentOverlay">@null</item> <item name="windowAnimationStyle">@null</item> <item name="windowIsFloating">true</item> <item name="backgroundDimEnabled">false</item> <item name="windowIsTranslucent">true</item> <item name="windowNoTitle">true</item> </style>
2、設(shè)置內(nèi)容的寬高
我們通過(guò)WindowManager.LayoutParams實(shí)現(xiàn)。
WindowManager.LayoutParams layoutParams = getWindow().getAttributes(); layoutParams.width = screenWidth; layoutParams.height = contentHeight; layoutParams.gravity = Gravity.TOP; layoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; //不阻塞事件傳遞到后面的窗口 getWindow().setAttributes(layoutParams);
這里,設(shè)置layoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; 則后面窗口的按鈕可響應(yīng)觸摸事件(例,HorizontalScrollView能橫向滾動(dòng))。
3、設(shè)置動(dòng)畫
通過(guò)ValueAnimator實(shí)現(xiàn)。
enter = ValueAnimator.ofFloat(0, 1f).setDuration(350); enter.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { dialogContent.setTranslationY((1 - animation.getAnimatedFraction()) * -contentHeight); } }); out = ValueAnimator.ofFloat(0, 1f).setDuration(350); out.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { dialogContent.setTranslationY(animation.getAnimatedFraction() * -contentHeight); } }); out.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { dismiss(); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } });
上面enter和out進(jìn)行一系列設(shè)置,對(duì)out動(dòng)畫加開(kāi)始結(jié)束監(jiān)聽(tīng)。enter的start()方法在onStart()中調(diào)用
@Override protected void onStart() { super.onStart(); dialogContent.post(new Runnable() { @Override public void run() { enter.start(); } }); }
通過(guò)view的post方式,enter.start()會(huì)在view hierarchy(view樹)構(gòu)建完后執(zhí)行(即視圖構(gòu)建完后執(zhí)行)。view.post源碼:
public boolean post(Runnable action) { final AttachInfo attachInfo = mAttachInfo; if (attachInfo != null) { return attachInfo.mHandler.post(action); } // Assume that post will succeed later ViewRootImpl.getRunQueue().post(action); return true; }
第七行為關(guān)鍵代碼,ViewRootImpl為視圖層級(jí)的頂部,實(shí)現(xiàn)了view和WindowManager之間的必要協(xié)議。RunQueue:運(yùn)行隊(duì)列用來(lái)排入沒(méi)有handler關(guān)聯(lián)的view的以后工作。
所以這里dialog的視圖顯示時(shí)會(huì)調(diào)用enter.start()方法.
監(jiān)聽(tīng)返回鍵:
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { out.start(); return true; } return super.onKeyDown(keyCode, event); }
out動(dòng)畫執(zhí)行完后,onAnimationEnd中調(diào)用dismiss()方法。
4、在點(diǎn)擊的view下顯示出來(lái)
public void showAsDropView(View view) { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.width = screenWidth; int[] location = new int[2]; view.getLocationOnScreen(location); // view.getLocationInWindow(location);<span style="white-space:pre"> </span>這里跟上面一句的效果一樣,不知有什么區(qū)別 lp.y = location[1]-PhoneConstant.statusHeight+view.getHeight(); lp.gravity = Gravity.TOP; getWindow().setAttributes(lp); contentTop = location[1]; show(); }
PhoneConstant.statusHeight為狀態(tài)欄的高度,其通過(guò)反射獲取
//反射獲取狀態(tài)欄高度 Class<?> c = null; Object obj = null; Field field = null; int x = 0, sbar = 0; try { c = Class.forName("com.android.internal.R$dimen"); obj = c.newInstance(); field = c.getField("status_bar_height"); x = Integer.parseInt(field.get(obj).toString()); PhoneConstant.statusHeight = getResources().getDimensionPixelSize(x); } catch(Exception e1) { e1.printStackTrace(); }
也可通過(guò)以下方式獲取
Rect outRect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
不過(guò)直接放在activity的onCreate中無(wú)效,只有界面繪制出來(lái)了才能獲取到,可通過(guò)view.post()方式獲取。
效果圖:
另外,繼承自AlertDialog的自定義dialog點(diǎn)擊edittext不彈出軟鍵盤,所以一般繼承自Dialog。
控制對(duì)話框輸入法的彈出,調(diào)用
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android studio 運(yùn)行main 函數(shù)的方法
這篇文章主要介紹了Android studio 運(yùn)行main 函數(shù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子
這篇文章介紹了Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子,有需要的朋友可以參考一下2013-08-08android實(shí)現(xiàn)視頻的加密和解密(使用AES)
本篇文章主要介紹了android實(shí)現(xiàn)視頻的加密和解密(使用AES),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05Android仿微信圖片選擇器ImageSelector使用詳解
這篇文章主要為大家詳細(xì)介紹了Android仿微信圖片選擇器ImageSelector的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12優(yōu)化SimpleAdapter適配器加載效率的方法
下面小編就為大家?guī)?lái)一篇優(yōu)化SimpleAdapter適配器加載效率的方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04Android音視頻之視頻采集(系統(tǒng)API預(yù)覽)
這篇文章主要為大家詳細(xì)介紹了Android音視頻之視頻采集,系統(tǒng)API預(yù)覽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12Android自定義FloatingText仿點(diǎn)贊+1特效
這篇文章主要為大家詳細(xì)介紹了Android自定義FloatingText仿點(diǎn)贊+1特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03Android使用Gridview單行橫向滾動(dòng)顯示
這篇文章主要為大家詳細(xì)介紹了Android使用Gridview單行橫向滾動(dòng)顯示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07