因为项目有需要,在退出登录的时候做一个抖动的对话框,来提醒用户。下面啥也不说了,上代码:
case R.id.btn_exit:showPopup(view);//显示break;
有需要popwindow的一些属性:
private void showPopup(View view) {
//加载布局
View contentViewt = LayoutInflater.from(SettingActivity.this).inflate( R.layout.exit_popwindow, null); final PopupWindow popWindow = new PopupWindow(contentViewt, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);// popWindow.setBackgroundDrawable(new BitmapDrawable());为啥这里要注释呢,因为如果设置了这个属性,当你点击popwindow外部的时候就不会触发dimiss()
popWindow.setWidth(ScreenUtil.getScreenWidth(this) * 4 / 5);//这边是获取到屏幕的宽度*5 popWindow.setFocusable(true);//设置SelectPicPopupWindow弹出窗体的背景 WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 0.7f; getWindow().setAttributes(lp); //设置菜单显示的位置 popWindow.showAtLocation(view, Gravity.CENTER_VERTICAL, 0, 0);
//重点的动画来了
ObjectAnimator animator = tada(contentViewt); // animator.setRepeatCount(ValueAnimator.INFINITE); //无限循环 animator.start();
//设置点击事件: contentViewt.findViewById(R.id.cancle_tv).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popWindow.dismiss(); } });
//监听菜单的关闭事件 popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 1f; getWindow().setAttributes(lp); } });
}
public static ObjectAnimator tada(View view) { return tada(view, 1f); } //添加退出效果动画 public static ObjectAnimator tada(View view, float shakeFactor) { PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofKeyframe(View.SCALE_X, Keyframe.ofFloat(0f, 1f), Keyframe.ofFloat(.1f, .9f), Keyframe.ofFloat(.2f, .9f), Keyframe.ofFloat(.3f, 1.1f), Keyframe.ofFloat(.4f, 1.1f), Keyframe.ofFloat(.5f, 1.1f), Keyframe.ofFloat(.6f, 1.1f), Keyframe.ofFloat(.7f, 1.1f), Keyframe.ofFloat(.8f, 1.1f), Keyframe.ofFloat(.9f, 1.1f), Keyframe.ofFloat(1f, 1f) ); PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofKeyframe(View.SCALE_Y, Keyframe.ofFloat(0f, 1f), Keyframe.ofFloat(.1f, .9f), Keyframe.ofFloat(.2f, .9f), Keyframe.ofFloat(.3f, 1.1f), Keyframe.ofFloat(.4f, 1.1f), Keyframe.ofFloat(.5f, 1.1f), Keyframe.ofFloat(.6f, 1.1f), Keyframe.ofFloat(.7f, 1.1f), Keyframe.ofFloat(.8f, 1.1f), Keyframe.ofFloat(.9f, 1.1f), Keyframe.ofFloat(1f, 1f) ); PropertyValuesHolder pvhRotate = PropertyValuesHolder.ofKeyframe(View.ROTATION, Keyframe.ofFloat(0f, 0f), Keyframe.ofFloat(.1f, -3f * shakeFactor), Keyframe.ofFloat(.2f, -3f * shakeFactor), Keyframe.ofFloat(.3f, 3f * shakeFactor), Keyframe.ofFloat(.4f, -3f * shakeFactor), Keyframe.ofFloat(.5f, 3f * shakeFactor), Keyframe.ofFloat(.6f, -3f * shakeFactor), Keyframe.ofFloat(.7f, 3f * shakeFactor), Keyframe.ofFloat(.8f, -3f * shakeFactor), Keyframe.ofFloat(.9f, 3f * shakeFactor), Keyframe.ofFloat(1f, 0) ); return ObjectAnimator.ofPropertyValuesHolder(view, pvhScaleX, pvhScaleY, pvhRotate). setDuration(1000);
效果图如下:
图一是成果图,图二是抖动时候,没有gif图。。。大家可以自己去试试!
为了方便大家,顺便布局一起丢了吧:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/shape_wuliu" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:gravity="center" android:text="退出登录" android:textColor="@color/fontColor2" android:textSize="@dimen/font_size_16sp" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="30px" android:layout_marginTop="30px" android:gravity="center" android:text="你确定退出登录吗?" android:textColor="@color/fontColor2" android:textSize="15sp" /> <TextView android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginLeft="@dimen/margin_size_1dp" android:layout_marginRight="@dimen/margin_size_1dp" android:background="@color/viewColor" /> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/cancle_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="取消" android:textColor="@color/background_tab_pressed" android:textSize="15sp" /> <TextView android:layout_width="1dp" android:layout_height="match_parent" android:layout_marginBottom="2px" android:layout_marginTop="1px" android:background="@color/viewColor" /> <TextView android:id="@+id/config_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="确定" android:textColor="@color/background_tab_pressed" android:textSize="15sp" /> </LinearLayout> </LinearLayout>
}