您的位置:首页 > 移动开发 > Android开发

Android 动画效果弹出PopupWindow,同时屏幕背景变暗

2015-10-17 10:57 826 查看
最近做项目的时候,希望弹出一个PopupWindow,但是效果并不美观,所以自己写了个效果,动画弹出PopupWindow,是飞入/飞出,加上渐变的效果。在弹出PopupWindow的同时,改变屏幕背景的透明度,使屏幕背景变暗。实现效果如下









主页面布局只添加了一个Button,然后设置Button的点击事件来弹出PopupWindow

PopupWindow布局添加了一个TextView和三个Button,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="这是一个PopupWindow" />

<Button
android:id="@+id/bt_item1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Item1" />

<Button
android:id="@+id/bt_item2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Item2" />

<Button
android:id="@+id/bt_item3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Item3" />

</LinearLayout>


因为PopupWindow必须设置背景图片,我用XML文件写了个背景图

在res文件夹下新建drawable文件夹,里面添加popupwindow_background.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#e1859e" />

<corners android:radius="5dip" />

<stroke
android:width="2dip"
android:color="#1cb0ba" />

</shape>


添加动画效果

在res文件夹下新建anim文件夹,里面添加动画文件
PopupWindow隐藏动画popupwindow_hidden_anim.xml,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="10%p" />

<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />

</set>


PopupWindow显示动画popupwindow_hidden_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<translate
android:duration="1000"
android:fromYDelta="10%p"
android:toYDelta="0" />

<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />

</set>


最后,我们还需要在res/values文件夹下的style.xml文件里添加如下代码,用来设置PopupWindow显示/隐藏的动画效果

<!-- PopupWindow弹出/隐藏动画 -->
<style name="MyPopupWindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popupwindow_show_anim</item>
<item name="android:windowExitAnimation">@anim/popupwindow_hidden_anim</item>
</style>
至此便完成了PopupWindow的动画设置

MainActivity代码,主要是设置PopupWindow的一些参数,具体见注释:

public class MainActivity extends Activity {

private Button bt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bt = (Button) findViewById(R.id.button1);
bt.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
showpopupWindow(v);// 显示PopupWindow
}
});
}

@SuppressLint("InlinedApi")
private void showpopupWindow(View v) {
Button btItem1, btItem2, btItem3;

LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View view = layoutInflater.inflate(R.layout.popwindow_layout, null);

final PopupWindow popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.popupwindow_background));
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(R.style.MyPopupWindow_anim_style);

btItem1 = (Button) view.findViewById(R.id.bt_item1);
btItem1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Item1被点击", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
btItem2 = (Button) view.findViewById(R.id.bt_item2);
btItem2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Item2被点击", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
btItem3 = (Button) view.findViewById(R.id.bt_item3);
btItem3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Item3被点击", Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});

// PopupWindow弹出位置
popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);

backgroundAlpha(0.5f);
popupWindow.setOnDismissListener(new OnDismissListener() {

@Override
public void onDismiss() {
backgroundAlpha(1f);
}
});
}

// 设置屏幕透明度
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; // 0.0~1.0
getWindow().setAttributes(lp);
}

}


参考文章:Android PopupWindow的使用和分析android 之popupWindow 在指定位置上的显示

源代码下载:Android 动画效果弹出PopupWindow,同时屏幕背景变暗
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息