PopupWindow设置动画效果
创建popupwindow的方法
Button menu; private void showPopupWindow() { //设置contentView float density = DensityUtil.Obtain(activity).density; View contentView = LayoutInflater.from(ActivityHomeImpl.this).inflate(R.layout.activity_home_menu, null); contentView.setBackgroundColor(0xff003333); mPopWindow = new PopupWindow(contentView, -2, DensityUtil.round(50* density), true);//DensityUtil.round(50* density) mPopWindow.setContentView(contentView); //设置各个控件的点击响应 ((Button)contentView.findViewById(R.id.btn1)).setOnClickListener(this); ((Button)contentView.findViewById(R.id.btn2)).setOnClickListener(this); ((Button)contentView.findViewById(R.id.btn3)).setOnClickListener(this); ((Button)contentView.findViewById(R.id.btn4)).setOnClickListener(this); ((Button)contentView.findViewById(R.id.btn5)).setOnClickListener(this); ((Button)contentView.findViewById(R.id.btn6)).setOnClickListener(this); mPopWindow.setTouchable(true); mPopWindow.setOutsideTouchable(true); // 实例化一个ColorDrawable颜色为半透明 //ColorDrawable dw = new ColorDrawable(0xd0000000); //mPopWindow.setBackgroundDrawable(dw); // 设置背景颜色变暗 WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 0.7f; getWindow().setAttributes(lp); mPopWindow.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 1f; getWindow().setAttributes(lp); mPopWindow.dismiss(); } }); // 设置popWindow的显示和消失动画 mPopWindow.setAnimationStyle(R.style.contextMenuAnim); mPopWindow.showAsDropDown(menu); }
popupwindow 动画
<style name="contextMenuAnim" parent="@android:style/Animation.Activity"> <item name="android:windowEnterAnimation">@anim/context_menu_enter</item> <!-- 指定显示时的动画xml --> <item name="android:windowExitAnimation">@anim/context_menu_exit</item> <!-- 指定消失时的动画xml --> </style>
activity的Theme设置
<style name="RedAppLaucherTheme" parent="AppTheme"> </style>
context_menu_enter.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="200" android:fromXDelta="-100%" android:fromYDelta="0" android:interpolator="@android:anim/anticipate_overshoot_interpolator" android:toXDelta="0" android:toYDelta="0" /> </set>
context_menu_exit.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="200" android:fromXDelta="0" android:fromYDelta="0" android:interpolator="@android:anim/overshoot_interpolator" android:toXDelta="-100%" android:toYDelta="0" /> </set>
布局文件activity_home_menu.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:layout_width="wrap_content" android:layout_height="50dp" android:orientation="horizontal" > <Button android:id="@+id/btn1" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:background="@drawable/item_connection" /> <Button android:id="@+id/btn2" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:background="@drawable/item_disk" /> <Button android:id="@+id/btn3" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:background="@drawable/item_upgrade" /> <Button android:id="@+id/btn4" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:background="@drawable/item_status" /> <Button android:id="@+id/btn5" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:background="@drawable/item_about" /> <Button android:id="@+id/btn6" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center_vertical" android:layout_marginLeft="5dp" android:background="@drawable/item_setting" /> </LinearLayout> </RelativeLayout>