實現半透明popupWindow


1.設置半透明主題
2.設置window的alpha值
  1. //                        WindowManager.LayoutParams lp = getWindow().getAttributes();
  2. //            lp.alpha = 0.5f; //0.0-1.0
  3. //            getWindow().setAttributes(lp);
復制代碼

發現這兩種都不能滿足要求,起碼的顏色就不太對。想做好點,做成類似alertDialog的樣子,帶邊框,彈出窗口帶動畫效果,之后背景置灰,那多帥。
看到那個仿uc瀏覽器的源碼,是用alertdialog做的,達到那種效果,加點動畫就行了。下圖是從那個ucweb源碼里面弄出來的。



       上面的代碼就不貼了,我上傳的項目文件里面也有。
        下面是彈出popupwindow的圖片,第一張是動畫中,第二張是完全彈出的:




  


         彈出popwindow的代碼如下,比較亂,多包涵:


  1. popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,
  2.                                 LayoutParams.FILL_PARENT, true);
  3.                 popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER
  4.                                 | Gravity.CENTER, 0, 0);
  5.                 popupWindow.setAnimationStyle(R.style.PopupAnimation);
  6.                 // 加上下面兩行可以用back鍵關閉popupwindow,否則必須調用dismiss();
  7.                 ColorDrawable dw = new ColorDrawable(-00000);
  8.                 popupWindow.setBackgroundDrawable(dw);
  9.                 popupWindow.update();
復制代碼

下面是實現步驟:    
1。背景置灰:
     popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, true);
     第二三個參數必須是LayoutParams.FILL_PARENT,這樣才能填充整個屏幕,達到背景置灰的目的。
     整個popupwindow里面是一個GridView,圖片什么的也是用的那個仿UC瀏覽器界面項目的,在此謝謝了。
     關鍵的東西都在xml里面。
      
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.            android:orientation="vertical" android:layout_width="fill_parent"
  4.             android:gravity="center" android:layout_height="fill_parent"
  5.            android:layout_gravity="center" android:background="#b0000000" >
  6.          <LinearLayout android:orientation="vertical"
  7.                    android:layout_width="wrap_content" android:gravity="center"
  8.                    android:layout_height="wrap_content" android:layout_gravity="center"
  9.                    android:background="@drawable/downbutton_corner">
  10.                  <GridView android:id="@+id/gridview" android:layout_width="wrap_content"
  11.                             android:layout_height="wrap_content" android:numColumns="4"
  12.                             android:verticalSpacing="5dip" android:horizontalSpacing="5dip"
  13.                             android:stretchMode="columnWidth" android:gravity="center"
  14.                             android:layout_gravity="center" /></LinearLayout></LinearLayout>
復制代碼

第一個linearlayout里面的android:background="#b0000000",就是全屏背景,網上搜的好多半透明都是“#e0000000”,我覺得那顏色太深,“#b0000000”更合適。
第二個linearlayout是popupwind的背景,里面的android:background="@drawable/downbutton_corner"是關鍵,邊框,圓角都是里面定義的。

2。popupwindow的邊框,圓角背景。downbutton_corne.xml
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  2.         android:shape="rectangle">
  3.         <gradient android:startColor="#c0000000" android:endColor="#c0000000"
  4.                 android:angle="90" /><!--背景顏色漸變 -->
  5.         <stroke android:dashWidth="2dp" android:dashGap="2dp"
  6.                 android:width="2dp" android:color="#FF00ff00"></stroke>
  7.         <!--描邊 -->
  8.         <corners android:bottomRightRadius="5dp"
  9.                 android:bottomLeftRadius="5dp" android:topLeftRadius="5dp"
  10.                 android:topRightRadius="5dp" /><!--設置圓角-->
  11. </shape>
復制代碼

這個涉及到shape畫圖,要是不懂的話。網上很多資料,搜一下就是了。我博客里面也有, http://blog.csdn.net/ymdcr/archive/2010/12/01/6048256.aspx
<gradient android:startColor="#c0000000" android:endColor="#c0000000" android:angle="90" /><!--背景顏色漸變 -->
我就設置了一個固定的顏色"#c0000000"。android:angle="90"這個是設置顏色漸變方向,從上到下啊,從左到右啊,貌似只能90的倍數,也只有四個方向嘛。
<stroke ></stroke>,邊框就是這個實現的。
dashWidth指的是邊線的寬度 dashGap 指的是每條線之間的間距,(因為是邊線是很多小橫線組成的)。

3。淡入淡出動畫
popupWindow.setAnimationStyle(R.style.PopupAnimation);
這條代碼是設置style的,動畫文件就是在style文件里面引入的。下面是淡入的動畫,動畫教程網上也很多。淡出的動畫就這些參數值交換位置就是了。android:duration這個是持續時間,為了截圖,我把它弄成5秒了。
  1. <set xmlns:android="http://schemas.android.com/apk/res/android">
  2.         <scale android:fromXScale="0.6" android:toXScale="1.0"
  3.                 android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"
  4.                 android:pivotY="50%" android:duration="5000" />
  5.         <alpha android:interpolator="@android:anim/decelerate_interpolator"
  6.                 android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="5000" />
  7. </set>
復制代碼


大概就是這些了。

還有一個關鍵的問題。彈出pop之后,back鍵無效了,必須在pop里面設置事件dismiss掉。下面是問題的描述,哪位解決了,告訴我一下,謝謝。我的郵箱:ytdcr@tom.com
問題解決了,是因為沒設置背景的原因。    
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//把這一行放在
showAtLocation前面就行了,以前是放在后面的,粗心了。
popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER
                | Gravity.CENTER, 0, 0);
  
網上也有很多人說,彈出pop之后,不響應鍵盤事件了,這個其實是焦點在pop里面的view去了。
以這個為例,焦點就在gridview上面去了。28樓的兄弟提示的,謝了。
在gridview加上
setOnKeyListener,就能解決。
  1. menuGrid.setOnKeyListener(new OnKeyListener() {
  2.             @Override
  3.             public boolean onKey(View v, int keyCode, KeyEvent event) {
  4.                 switch (keyCode) {
  5.                 case KeyEvent.KEYCODE_MENU:
  6.                     if (popupWindow != null && popupWindow.isShowing()) {
  7.                         popupWindow.dismiss();
  8.                         System.out.println("menuGridfdsfdsfdfd");
  9.                     }
  10.                     break;
  11.                 }
  12.             
  13.                 return true;
  14.             }
  15.         });
復制代碼
[ code]/**
     * ColorDrawable dw = new ColorDrawable(-00000);
     * popupWindow.setBackgroundDrawable(dw);
     * 本來看了個示例,加上上面這兩行就不用調用dismiss,點擊窗口之外的部位,或者按back鍵都能關閉窗口。 但是我這樣寫了,還是不行。
     * 而且竟然捕獲不到鍵盤事件,杯具,希望哪個解決了這個問題告訴我,謝謝。  ytdcr@tom.com
     */
    /*
     * @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch
     * (keyCode) { case KeyEvent.KEYCODE_BACK: if (popupWindow != null) {
     * popupWindow.dismiss(); }
     *
     * Toast.makeText(this, "fd", 1000).show(); break;
     *
     * } return super.onKeyUp(keyCode, event); }
     */[/code]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM