Android开发之PopupWindow仿QQ自定义弹窗

截图图


1.自定义弹窗布局confirm_dialog.xml

<?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/bg_pull"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/ll_chat"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/iv_chat" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_marginLeft="5dp"
            android:gravity="center_vertical"
            android:text="发起群聊"
            android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_friend"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:src="@drawable/iv_add_friends" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:layout_marginLeft="5dp"
            android:gravity="center_vertical"
            android:text="添加好友"
            android:textSize="16sp" />
    </LinearLayout>

</LinearLayout>


2.自定义视图

package com.fb.panoa.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.fb.panoa.R;

/**
 * 弹窗视图
 */
public class ConfirmPopWindow extends PopupWindow implements View.OnClickListener {
    private Context context;
    private View ll_chat, ll_friend;

    public ConfirmPopWindow(Context context) {
        super(context);
        this.context = context;
        initalize();
    }

    private void initalize() {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.confirm_dialog, null);
        ll_chat = view.findViewById(R.id.ll_chat);//发起群聊
        ll_friend = view.findViewById(R.id.ll_friend);//添加好友
        ll_chat.setOnClickListener(this);
        ll_friend.setOnClickListener(this);
        setContentView(view);
        initWindow();
    }

    private void initWindow() {
        DisplayMetrics d = context.getResources().getDisplayMetrics();
        this.setWidth((int) (d.widthPixels * 0.35));
        this.setHeight(LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        this.update();
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        backgroundAlpha((Activity) context, 0.8f);//0.0-1.0
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) context, 1f);
            }
        });
    }

    //设置添加屏幕的背景透明度
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public void showAtBottom(View view) {
        //弹窗位置设置
        showAsDropDown(view, Math.abs((view.getWidth() - getWidth()) / 2), 10);
        //showAtLocation(view, Gravity.TOP | Gravity.RIGHT, 10, 110);//有偏差
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_chat:
                Toast.makeText(context, "发起群聊", Toast.LENGTH_SHORT).show();
                break;
            case R.id.ll_friend:
                Toast.makeText(context, "添加好友", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }

}


3.在需要的地方调用该视图就可以了

new ConfirmPopWindow(getActivity()).showAtBottom(iv_menu);


  • 3
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
适用于任何视图的可自定义反弹动画库。Getting StartedIn your build.gradledependencies {     implementation 'hari.bounceview:bounceview:0.1.0'}UsageAdd animations to any views like so:Button button = view.findViewById(R.id.button);BounceView.addAnimTo(button);Use BounceView with dialogs:CustomDialog customDialog = new CustomDialog(getActivity());//Add animation to custom dialogBounceView.addAnimTo(customDialog);        //Call before showing the dialogcustomDialog.show();PopupWindow popupWindow;...//Add animation to popup windowBounceView.addAnimTo(popupWindow);        //Call before showing the popuppopupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0);AlertDialog dialog = builder.create();//Add animation to alert dialogBounceView.addAnimTo(dialog);        //Call before showing the dialogdialog.show();Some cool animations://Bounce animationBounceView.addAnimTo(button1)         .setScaleForPopOutAnim(1.1f, 1.1f);//Horizontal flip animationBounceView.addAnimTo(button2)         .setScaleForPopOutAnim(1f, 0f);//Vertical flip animationBounceView.addAnimTo(button3)         .setScaleForPopOutAnim(0f, 1f);//Flicker animationBounceView.addAnimTo(button4)         .setScaleForPopOutAnim(0f, 0f);Customize BounceView properties:Button button = view.findViewById(R.id.button);BounceView.addAnimTo(button)        //Default push in scalex: 0.9f , scaley: 0.9f         .setScaleForPushInAnim(BounceView.PUSH_IN_SCALE_X, BounceView.PUSH_IN_SCALE_Y)        //Default pop out scalex: 1.1f, scaley: 1.1f         .setScaleForPopOutAnim(BounceView.POP_OUT_SCALE_X, BounceView.POP_OUT_SCALE_Y)        //Default push in anim duration: 100 (in milliseconds)         .setPushInAnimDuration(BounceView.PUSH_IN_ANIM_DURATION)        //Default pop out anim duration: 100 (in milliseconds)         .setPopOutAnimDuration(BounceView.POP_OUT_ANIM_DURATION)        //Default interpolator: AccelerateDecelerateInterpolator()         .setInterpolatorPushIn(BounceView.DEFAULT_INTERPOLATOR)         .setInterpolatorPopOut(BounceView.DEFAULT_INTERPOLATOR);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

举儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值