popwindow點選外部取消不了和popwindow彈出抖動動畫
阿新 • • 發佈:2019-01-24
因為專案有需要,在退出登入的時候做一個抖動的對話方塊,來提醒使用者。下面啥也不說了,上程式碼:
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 = newPopupWindow(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>
}