日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android 弹出PopupWindow后背景逐渐变暗

發布時間:2024/4/15 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 弹出PopupWindow后背景逐渐变暗 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


今天,簡單講講android在彈出 

PopupWindow后如何使背景逐漸變暗。


我之前做的效果是在PopupWindow彈出時立即將背景變暗,這個也很簡單。

在PopupWindow彈出時,將背景變暗:

//popWindow消失監聽方法 WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 0.5f; getWindow().setAttributes(lp);


在PopupWindow消失時,恢復背景:

window.setOnDismissListener(new PopupWindow.OnDismissListener() {@Override public void onDismiss() {WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 1f; getWindow().setAttributes(lp); } });


但是最近發現一個app在彈出PopupWindow時背景是逐漸變暗的,這個是怎么做到的?在網上搜索了資料,發現其實也很簡單。這里記錄一下。

效果圖:


PopupWindow實現:

先看彈出window布局window_popup.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginLeft="@dimen/activity_horizontal_margin"android:layout_marginRight="@dimen/activity_horizontal_margin"android:background="#dadada"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"><Buttonandroid:id="@+id/camera"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="拍照"android:background="#f0f0f0"/><TextViewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#2d2c2c"/><Buttonandroid:background="#f0f0f0"android:id="@+id/gallery"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="從手機相冊選擇"/><TextViewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#2d2c2c"/><Buttonandroid:background="#f0f0f0"android:id="@+id/savepicture"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="保存圖片"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:orientation="vertical"><Buttonandroid:background="#f0f0f0"android:id="@+id/cancel"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="取消"/></LinearLayout> </LinearLayout>


布局的效果圖:


創建popupWindow并為其添加點擊事件:

void bottomwindow(View view) {if (popupWindow != null && popupWindow.isShowing()) {return;}LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.window_popup, null);popupWindow = new PopupWindow(layout,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);//點擊空白處時,隱藏掉pop窗口popupWindow.setFocusable(true);popupWindow.setBackgroundDrawable(new BitmapDrawable());//添加彈出、彈入的動畫popupWindow.setAnimationStyle(R.style.Popupwindow);int[] location = new int[2];view.getLocationOnScreen(location);popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.BOTTOM, 0, -location[1]);//添加按鍵事件監聽setButtonListeners(layout);//添加pop窗口關閉事件,主要是實現關閉時改變背景的透明度popupWindow.setOnDismissListener(new poponDismissListener());backgroundAlpha(1f);}


事件監聽的函數setButtonListeners() :

private void setButtonListeners(LinearLayout layout) {Button camera = (Button) layout.findViewById(R.id.camera);Button gallery = (Button) layout.findViewById(R.id.gallery);Button savepicture = (Button) layout.findViewById(R.id.savepicture);Button cancel = (Button) layout.findViewById(R.id.cancel);camera.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (popupWindow != null && popupWindow.isShowing()) {//在此處添加你的按鍵處理 xxxpopupWindow.dismiss();}}});gallery.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (popupWindow != null && popupWindow.isShowing()) {//在此處添加你的按鍵處理 xxxpopupWindow.dismiss();}}});savepicture.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (popupWindow != null && popupWindow.isShowing()) {//在此處添加你的按鍵處理 xxxpopupWindow.dismiss();}}});cancel.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {if (popupWindow != null && popupWindow.isShowing()) {popupWindow.dismiss();}}});}

彈出、收回的動畫:

若res文件夾下沒有anim目錄,則自己添加一個:new–>Android resource directory 名字填anim。然后新建兩個tranlate文件:

彈出 window_out.xml :

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/decelerate_interpolator"android:fromYDelta="100%" android:toYDelta="0"android:duration="300"/>


收回 window_back.xml:

<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"android:fromYDelta="0" android:toYDelta="100%"android:duration="200"/>


然后在style.xml中添加我們的這兩個動畫:

<style name="Popupwindow"><item name="android:windowEnterAnimation">@anim/window_out</item><item name="android:windowExitAnimation">@anim/window_back</item></style>


還是上面的同一個MainActivity,把按鈕點擊事件的處理函數換成popupwindow的即可:

btnmenu.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {bottomwindow(btnmenu);}}


以上,就可以實現這樣的點擊按鈕從屏幕底部彈出window窗口的效果,如下:


底部彈出

但是,這樣的效果并不好,我們希望彈出windows的時候,其他背景可以變成半透明,這樣可以突出重點。網上的方法是通過這段代碼來改變背景的透明度的:

/*** 設置添加屏幕的背景透明度* @param bgAlpha*/public void backgroundAlpha(float bgAlpha){WindowManager.LayoutParams lp = getWindow().getAttributes();lp.alpha = bgAlpha; //0.0-1.0getWindow().setAttributes(lp); getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);}


然后在彈出的時候將背景設為半透明:

bottomwindow(btnmenu);
backgroundAlpha(0.5f);

在返回的時候設置回來:

backgroundAlpha(1f);

這的確是可以實現效果,但是點擊的時候突然變暗和變亮,效果不太好!如下


我希望是彈出的過程中,慢慢變暗。是有一個過程的,而不是一下子就暗下來了。這里利用延時和Handler來動態地改變背景的透明度。

//在調用彈出的方法后,開啟一個子線程@Overridepublic void onClick(View view) {bottomwindow(btnmenu);new Thread(new Runnable(){@Overridepublic void run() {while(alpha>0.5f){try {//4是根據彈出動畫時間和減少的透明度計算Thread.sleep(4);} catch (InterruptedException e) {e.printStackTrace();}Message msg =mHandler.obtainMessage();msg.what = 1;//每次減少0.01,精度越高,變暗的效果越流暢alpha-=0.01f;msg.obj =alpha ;mHandler.sendMessage(msg);}}}).start();}


同理,返回的時候把透明度跳回來:

/*** 返回或者點擊空白位置的時候將背景透明度改回來*/class poponDismissListener implements PopupWindow.OnDismissListener{@Overridepublic void onDismiss() {// TODO Auto-generated method stubnew Thread(new Runnable(){@Overridepublic void run() {//此處while的條件alpha不能<= 否則會出現黑屏while(alpha<1f){try {Thread.sleep(4);} catch (InterruptedException e) {e.printStackTrace();}Log.d("HeadPortrait","alpha:"+alpha);Message msg =mHandler.obtainMessage();msg.what = 1;alpha+=0.01f;msg.obj =alpha ;mHandler.sendMessage(msg);}}}).start();}}


在Handler里面我們調用改變背景透明的方法即可:

Handler mHandler = new Handler(){@Overridepublic void handleMessage(Message msg) {switch (msg.what){case 1:backgroundAlpha((float)msg.obj);break;}}};

這樣修改以后,效果是這樣的:


這里簡單講講,關鍵的代碼就是在PopupWindow彈出時,開啟一個線程,然后逐漸使背景變暗。關閉時同樣開啟一個線程,使背景逐漸還原。還有一個使用屬性動畫是PopupWindow背景變暗的功能暫時不講了,因為這篇博客已經太長了。大家有興趣的自己可以去查找資料。


android 彈出PopupWindow后背景逐漸變暗就講完了。


就這么簡單。

總結

以上是生活随笔為你收集整理的android 弹出PopupWindow后背景逐渐变暗的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。