天天看點

Android面試時的問題,實作半透明的popupwindow的源碼

1.設定半透明主題

2.設定window的alpha值

// WindowManager.LayoutParams lp = getWindow().getAttributes();

// lp.alpha = 0.5f; //0.0-1.0

// getWindow().setAttributes(lp);

發現這兩種都不能滿足要求,起碼的顔色就不太對。想做好點,做成類似alertDialog的樣子,帶邊框,彈出視窗帶動畫效果,之後背景置灰,那多帥。

看到那個仿uc浏覽器的源碼,是用alertdialog做的,達到那種效果,加點動畫就行了。下圖是從那個ucweb源碼裡面弄出來的。

上面的代碼就不貼了,我上傳的項目檔案裡面也有。

下面是彈出popupwindow的圖檔,第一張是動畫中,第二張是完全彈出的:

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

popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,

LayoutParams.FILL_PARENT, true);

popupWindow.showAtLocation(findViewById(R.id.parent), Gravity.CENTER

| Gravity.CENTER, 0, 0);

popupWindow.setAnimationStyle(R.style.PopupAnimation);

// 加上下面兩行可以用back鍵關閉popupwindow,否則必須調用dismiss();

ColorDrawable dw = new ColorDrawable(-00000);

popupWindow.setBackgroundDrawable(dw);

popupWindow.update();

下面是實作步驟:

1。背景置灰:

popupWindow = new PopupWindow(menuView, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, true);

第二三個參數必須是LayoutParams.FILL_PARENT,這樣才能填充整個螢幕,達到背景置灰的目的。

整個popupwindow裡面是一個GridView,圖檔什麼的也是用的那個仿UC浏覽器界面項目的,在此謝謝了。

關鍵的東西都在xml裡面。

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent"

android:gravity="center" android:layout_height="fill_parent"

android:layout_gravity="center" android:background="#b0000000" >

<LinearLayout android:orientation="vertical"

android:layout_width="wrap_content" android:gravity="center"

android:layout_height="wrap_content" android:layout_gravity="center"

android:background="@drawable/downbutton_corner">

<GridView android:id="@+id/gridview" android:layout_width="wrap_content"

android:layout_height="wrap_content" android:numColumns="4"

android:verticalSpacing="5dip" android:horizontalSpacing="5dip"

android:stretchMode="columnWidth" android:gravity="center"

android:layout_gravity="center" /></LinearLayout></LinearLayout>

複制代碼第一個linearlayout裡面的android:background="#b0000000",就是全屏背景,網上搜的好多半透明都是“#e0000000”,我覺得那顔色太深,“#b0000000”更合适。

第二個linearlayout是popupwind的背景,裡面的android:background="@drawable/downbutton_corner"是關鍵,邊框,圓角都是裡面定義的。

2。popupwindow的邊框,圓角背景。downbutton_corne.xml<shape xmlns:android="http://schemas.android.com/apk/res/android"

android:shape="rectangle">

<gradient android:startColor="#c0000000" android:endColor="#c0000000"

android:angle="90" /><!--背景顔色漸變 -->

<stroke android:dashWidth="2dp" android:dashGap="2dp"

android:width="2dp" android:color="#FF00ff00"></stroke>

<!--描邊 -->

<corners android:bottomRightRadius="5dp"

android:bottomLeftRadius="5dp" android:topLeftRadius="5dp"

android:topRightRadius="5dp" /><!--設定圓角-->

</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秒了。<set xmlns:android="http://schemas.android.com/apk/res/android">

<scale android:fromXScale="0.6" android:toXScale="1.0"

android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"

android:pivotY="50%" android:duration="5000" />

<alpha android:interpolator="@android:anim/decelerate_interpolator"

android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="5000" />

</set>

複制代碼

大概就是這些了。

還有一個關鍵的問題。彈出pop之後,back鍵無效了,必須在pop裡面設定事件dismiss掉

style.xml檔案

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="PopupAnimation" parent="android:Animation" mce_bogus="1">

<item name="android:windowEnterAnimation">@anim/popup_enter</item>

<item name="android:windowExitAnimation">@anim/popup_exit</item>

</style>

</resources>

popup_enter.xml檔案

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

<scale android:fromXScale="0.6" android:toXScale="1.0"

android:fromYScale="0.6" android:toYScale="1.0" android:pivotX="50%"

android:pivotY="50%" android:duration="1000" />

<alpha android:interpolator="@android:anim/decelerate_interpolator"

android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />

</set>

popup_exit.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

<scale android:fromXScale="1.0" android:toXScale="0.5"

android:fromYScale="1.0" android:toYScale="0.5" android:pivotX="50%"

android:pivotY="50%" android:duration="500" />

<alpha android:interpolator="@android:anim/accelerate_interpolator"

android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="500" />

</set>

final PopupWindow pw = new PopupWindow(vPopupWindow,300,100,true);

pw.setAnimationStyle(R.style.PopupAnimation);

ColorDrawable dw = new ColorDrawable(-00000);

pw.setBackgroundDrawable(dw);

pw.update();

繼續閱讀