Android巧用DecorView實(shí)現(xiàn)對(duì)話框功能
如果還不知道DecorView,那也沒(méi)有什么關(guān)系 ^_^
先來(lái)看看實(shí)現(xiàn)的效果
實(shí)現(xiàn)的大致思路
- 首先需要明白什么是DecorView,他是android中界面的根布局。其實(shí)android的activity界面整個(gè)就是一個(gè)控件樹(shù),DecorView是根節(jié)點(diǎn),DecorView的孩子節(jié)點(diǎn)就是一個(gè)LinearLayout,這個(gè)LinearLayout的孩子系節(jié)點(diǎn)就包括狀態(tài)欄 + 和我們自己寫的布局
- DecorView是FramLayout的子類(可以從源碼中看到)
- 既然DecorView是根節(jié)點(diǎn),而且還是FrameLayout,所以我們可以把我們自己的布局 添加到DecorView 或者 從DecorView移除,這樣就模擬出了一個(gè)Dialog的效果~~ ,當(dāng)然這個(gè)Dialog的樣式,動(dòng)畫就可以自己想怎么寫就怎么寫了撒
- 通過(guò)activity.getWindow().getDecorView()可以獲得DecorView
[下面大量 代碼 ]
第一個(gè)對(duì)話框的實(shí)現(xiàn)
public class TipsDialog { private Activity activity; private View rootView; private TextView confirmTextView; private TextView cancelTextView; private TextView contentTextView; private boolean isShowing; public TipsDialog(Activity activity) { this.activity = activity; isShowing = false; rootView = LayoutInflater.from(activity).inflate(R.layout.view_tips_dialog,null); confirmTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_confirm); cancelTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_cancel); contentTextView = (TextView) rootView.findViewById(R.id.view_tips_dialog_tv_content); } public void show(){ if(activity == null){ return; } if(isShowing){ return; } ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); params.gravity = Gravity.CENTER; rootView.setLayoutParams(params); decorView.addView(rootView); rootView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); RotateAnimation rotateAnimation = new RotateAnimation(0,720f,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setDuration(2000); contentTextView.startAnimation(rotateAnimation); isShowing = true; } public void dismiss(){ if(!isShowing){ return; } isShowing = false; if(rootView.getParent() == null){ return; } ViewGroup parent = (ViewGroup) rootView.getParent(); parent.removeView(rootView); } public int getRandomColor(){ Random random = new Random(); return Color.argb(random.nextInt(200),random.nextInt(240),random.nextInt(240),random.nextInt(240)); } public boolean isShowing() { return isShowing; } }
其實(shí)就是show的時(shí)候?qū)⒉季痔砑拥紻ecorView上面去,dismiss的時(shí)候?qū)⒉季謴腄ecorView上面移除
提示的實(shí)現(xiàn)(沒(méi)有處理完善~~ 僅僅就是說(shuō)明哈DecorView)
public class TopTipDialog { private Activity activity; private View rootView; private boolean isShowing; private static final int VIEW_HEIGHT = 64;//px public TopTipDialog(Activity activity) { this.activity = activity; rootView = LayoutInflater.from(activity).inflate(R.layout.view_top_tip_dialog,null); } public void show(){ if(isShowing){ return; } ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, VIEW_HEIGHT); params.gravity = Gravity.TOP; params.setMargins(0,0,0,-VIEW_HEIGHT); rootView.setLayoutParams(params); TranslateAnimation translateAnimation = new TranslateAnimation(0,0,-VIEW_HEIGHT,0); translateAnimation.setDuration(1500); translateAnimation.setFillAfter(true); decorView.addView(rootView); rootView.startAnimation(translateAnimation); rootView.postDelayed(new Runnable() { @Override public void run() { TranslateAnimation translateAnimation1 = new TranslateAnimation(0,0,0,-VIEW_HEIGHT); translateAnimation1.setDuration(1500); translateAnimation1.setFillAfter(true); rootView.startAnimation(translateAnimation1); } },3000); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
解決Android Studio sdk emulator directory is missing問(wèn)題
這篇文章主要介紹了解決Android Studio sdk emulator directory is missing問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Android編程開(kāi)發(fā)之TextView文字顯示和修改方法(附TextView屬性介紹)
這篇文章主要介紹了Android編程開(kāi)發(fā)之TextView文字顯示和修改方法,結(jié)合實(shí)例詳細(xì)分析了Android中TextView控件關(guān)于文字的顯示及修改技巧,并附帶了TextView屬性介紹,需要的朋友可以參考下2015-12-12Android檢測(cè)Activity或者Service是否運(yùn)行的方法
下面小編就為大家分享一篇Android檢測(cè)Activity或者Service是否運(yùn)行的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03Android 利用反射+try catch實(shí)現(xiàn)sdk按需引入依賴庫(kù)的方法
這篇文章主要介紹了Android 利用反射+try catch來(lái)實(shí)現(xiàn)sdk按需引入依賴庫(kù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11快速調(diào)試Android應(yīng)用系統(tǒng)修改ro.debuggable屬性的兩種方式
這篇文章主要為大家介紹了快速調(diào)試Android應(yīng)用系統(tǒng)修改ro.debuggable屬性的兩種方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Android Bitmap的截取及狀態(tài)欄的隱藏和顯示功能
Bitmap是Android系統(tǒng)中的圖像處理中最重要類之一。Bitmap可以獲取圖像文件信息,對(duì)圖像進(jìn)行剪切、旋轉(zhuǎn)、縮放,壓縮等操作,并可以以指定格式保存圖像文件。這篇文章主要介紹了Android Bitmap的截取及狀態(tài)欄的隱藏和顯示功能,需要的朋友可以參考下2017-11-11Android實(shí)現(xiàn)仿今日頭條點(diǎn)贊動(dòng)畫效果實(shí)例
我想看到今日頭條的點(diǎn)贊效果,應(yīng)該都覺(jué)得很絢麗吧,下面這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)仿今日頭條點(diǎn)贊動(dòng)畫效果的相關(guān)資料,文中通過(guò)示例代價(jià)介紹的非常詳細(xì),需要的朋友可以參考下2022-02-02