popwindow点击外部取消不了和popwindow弹出抖动动画

在项目中,为了增强用户体验,通常会在退出登录时使用带抖动动画的PopWindow来提示用户。本文将分享如何实现这样一个功能,包括PopWindow的属性设置及点击外部不关闭的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

因为项目有需要,在退出登录的时候做一个抖动的对话框,来提醒用户。下面啥也不说了,上代码:

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>




}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值