使用android.graphics.Path类自绘制PopupWindow背景

PopupWindow简单介绍

PopupWindow是悬浮在当前activity上的一个容器,用它能够展示随意的内容。
PopupWindow跟位置有关的API有以下几个:

  • showAsDropDown(View anchor, int xoff, int yoff, int gravity)
    显示在anchor的左下角,通过xoff,yoff调整距离。gravity是popup相对于anchor的对齐方式。假设popup超出屏幕,而且展示内容的根容器是滑动控件,将以滑动方式展示。假设展示内容根容器不是滑动控件,超出屏幕内容将不可见。
  • showAsDropDown (View anchor, int xoff, int yoff)
    同上
  • showAsDropDown (View anchor)
    同上
  • showAtLocation (View parent, int gravity, int x, int y)
    展示在屏幕的特定位置,假设内容超出屏幕将被裁剪。


    gravity 为NO_GRAVITY等同于 Gravity.LEFT | Gravity.TOP

showAsDropDown 还是showAtLocation?
假设有anchor,能够使用showAsDropDown 方法。假设没有anchor能够使用showAtLocation 方法,注意使用showAtLocation 方法popup内容超出屏幕即使内容放到ScrollView里也不会滚动。

使用Path类自绘制PopupWindow背景

这里选择showAtLocation方法,使用Path类自绘制PopupWindow背景。


绘制规则例如以下:
绘制规则
给定Popup锚点的x坐标,anchorX;y坐标,anchorYDown,anchorYUp,自己定义view会自己主动计算三角绘制位置。以及显示在anchor下方还是上方。默认显示在下方,下方显示不下再显示在上方。

不足是内容太长无法滚动显示

实现

package com.xxx;

import com.xxx.utils.log.LogUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.PathShape;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * TextView with popup style background (has triangle on top or bottom). The
 * anchor triangle will show accurately below or above the anchor position.
 * 
 * @author wangwenping
 * @date 2015-6-27
 */
@SuppressLint("DrawAllocation")
public class PopupTextView extends TextView
{
    private static final String TAG = "PopupTextView";
    private static final boolean IS_DEBUG = false;
    /**
     * x of anchor triangle in the popup
     */
    private float mTriangleX;
    /**
     * border color
     */
    private int mBorderColor = 0xff1fc38f;
    /**
     * border width
     */
    private int mBorderWidth = 2;
    /**
     * background color
     */
    private int mBgColor = 0xffffffff;
    /**
     * background color in dark mode
     */
    private int mBgColorDark = 0xff999999;
    /**
     * anchor height
     */
    private float mAnchorHeight = 20;
    /**
     * anchor width
     */
    private float mAnchorWidth = 30;
    /**
     * If content under anchor
     */
    private boolean mShowDown = true;
    /**
     * Below items for draw
     */
    private ShapeDrawable mBorderDrawable;
    private Path mBorderPath;
    private ShapeDrawable mBgDrawable;
    private Path mBgPath;
    private int mWidth;
    private int mHeight;
    /**
     * Keep a record of original padding.
     */
    private int mPadding;
    /**
     * Is night mode.
     */
    private boolean mIsNightMode;
    /**
     * anchor x, y in screen
     */
    private int mAnchorYUp;
    private int mAnchorYDown;
    private int mAnchorX;

    /**
     * screen height & width
     */
    private int mScreenHeight;
    private int mScreenWidth;
    private float mDensity;
    private PopupWindow mPopupWindow;
    private Context mCtx;
    /**
     * Touch listener
     */
    private OnTouchListener mOnTouchListener;
    private boolean mDismissAfterTouch = true;
    /**
     * The minimum margin to left or right.
     */
    private int TRIANGLE_MINIMUM_MARGIN = 10;

    public PopupTextView(Context context)
    {
        super(context);
        setFocusable(true);
        init(context);
    }

    public PopupTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        init(context);
    }

    public PopupTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init(context);
    }

    private void init(Context c)
    {
        mCtx = c;
        mPadding = getPaddingBottom();
        DisplayMetrics dm = c.getResources().getDisplayMetrics();
        mScreenHeight = dm.heightPixels;
        mScreenWidth = dm.widthPixels;
        mDensity = dm.scaledDensity;
    }

    /**
     * Show as pop up window
     */
    public void show()
    {
        if (mPopupWindow != null)
        {
            mPopupWindow.dismiss();
        }

        if (IS_DEBUG)
        {
            LogUtils.d(TAG, "mAnchorX=" + mAnchorX + " mWidth=" + mWidth + " mHeight=" + mHeight);
        }

        mPopupWindow = new PopupWindow(this, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if (mOnTouchListener != null)
        {
            mPopupWindow.setTouchInterceptor(new OnTouchListener()
            {
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1)
                {
                    mOnTouchListener.onTouch(arg0, arg1);
                    if (mDismissAfterTouch && arg1.getAction() == MotionEvent.ACTION_UP)
                    {
                        mPopupWindow.dismiss();
                    }
                    return false;
                }
            });
        }
        mPopupWindow.setFocusable(true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setBackgroundDrawable(new BitmapDrawable());

        int popX = 0, popY = 0;
        if (mWidth <= 0 || mHeight <= 0)
        {
            // The first time we showthe pop up window out of the screen to get
            // the size of itself.
            popX = mScreenWidth;
            popY = mScreenHeight;
        }
        else
        {
            // The second time we calculate the pop up window's right position.
            Point pos = getLayoutValue();
            popX = pos.x;
            popY = pos.y;
            mTriangleX = mAnchorX - pos.x;
            mTriangleX = Math.max(mTriangleX, TRIANGLE_MINIMUM_MARGIN);
            mTriangleX = Math.min(mTriangleX, mWidth - TRIANGLE_MINIMUM_MARGIN - mAnchorWidth);
        }
        mPopupWindow.showAtLocation(this, Gravity.LEFT | Gravity.TOP, popX, popY);
    }

    /**
     * Calculate the pop up window's right position.
     * 
     * @return
     */
    private Point getLayoutValue()
    {
        int x = mAnchorX - mWidth / 2;
        if (x < 10 * mDensity)
        {
            x = (int) (10 * mDensity);
        }
        else if (x + mWidth > mScreenWidth - 10 * mDensity)
        {
            x = (int) (mScreenWidth - mWidth - 10 * mDensity);
        }
        boolean showDown = mAnchorYDown + mHeight < mScreenHeight || mAnchorYDown <= mScreenHeight / 2;
        setShowDown(showDown);
        int y = showDown ? mAnchorYDown : mAnchorYUp - mHeight;
        return new Point(x, y);
    }

    /**
     * Init drawble path.
     * 
     * @param width
     * @param height
     */
    private void initPath(int width, int height)
    {
        mBorderPath = new Path();
        mBgPath = new Path();

        if (mShowDown)
        {
            /**
             * ....|<----------------width-------->|<br>
             * ....|<--archorX------>|<br>
             * ....................2<br>
             * ..................../\ (anchor)<br>
             * ....0/7-------------1 3-----------4...........----<br>
             * ....|...............................|.............|<br>
             * ....|...............................|.............height<br>
             * ....|...............................|.............|<br>
             * ....6-------------------------------5............---<br>
             */
            PointF[] borderPoints = new PointF[] { new PointF(0, mAnchorHeight),
                    new PointF(mTriangleX - mAnchorWidth / 2, mAnchorHeight), new PointF(mTriangleX, 0),
                    new PointF(mTriangleX + mAnchorWidth / 2, mAnchorHeight), new PointF(width, mAnchorHeight),
                    new PointF(width, height), new PointF(0, height), new PointF(0, mAnchorHeight), };
            mBorderPath = createLIneToPath(borderPoints);

            PointF[] bgPoints = new PointF[] {
                    new PointF(borderPoints[0].x + mBorderWidth, borderPoints[0].y + mBorderWidth),
                    new PointF(borderPoints[1].x + mBorderWidth, borderPoints[1].y + mBorderWidth),
                    new PointF(borderPoints[2].x, borderPoints[2].y + mBorderWidth),
                    new PointF(borderPoints[3].x - mBorderWidth, borderPoints[3].y + mBorderWidth),
                    new PointF(borderPoints[4].x - mBorderWidth, borderPoints[4].y + mBorderWidth),
                    new PointF(borderPoints[5].x - mBorderWidth, borderPoints[5].y - mBorderWidth),
                    new PointF(borderPoints[6].x + mBorderWidth, borderPoints[6].y - mBorderWidth),
                    new PointF(borderPoints[7].x + mBorderWidth, borderPoints[7].y + mBorderWidth), };
            mBgPath = createLIneToPath(bgPoints);
        }
        else
        {
            /**
             * 0/7-----------------------------1<br>
             * |...............................|<br>
             * |...............................|<br>
             * 6------------------5..3---------2<br>
             * ....................\/<br>
             * ....................4<br>
             */
            PointF[] borderPoints = new PointF[] { new PointF(0, 0), new PointF(width, 0),
                    new PointF(width, height - mAnchorHeight),
                    new PointF(mTriangleX + mAnchorWidth / 2, height - mAnchorHeight), new PointF(mTriangleX, height),
                    new PointF(mTriangleX - mAnchorWidth / 2, height - mAnchorHeight),
                    new PointF(0, height - mAnchorHeight), new PointF(0, 0), };
            mBorderPath = createLIneToPath(borderPoints);

            PointF[] bgPoints = new PointF[] {
                    new PointF(borderPoints[0].x + mBorderWidth, borderPoints[0].y + mBorderWidth),
                    new PointF(borderPoints[1].x - mBorderWidth, borderPoints[1].y + mBorderWidth),
                    new PointF(borderPoints[2].x - mBorderWidth, borderPoints[2].y - mBorderWidth),
                    new PointF(borderPoints[3].x - mBorderWidth, borderPoints[3].y - mBorderWidth),
                    new PointF(borderPoints[4].x, borderPoints[4].y - mBorderWidth),
                    new PointF(borderPoints[5].x + mBorderWidth, borderPoints[5].y - mBorderWidth),
                    new PointF(borderPoints[6].x + mBorderWidth, borderPoints[6].y - mBorderWidth),
                    new PointF(borderPoints[7].x + mBorderWidth, borderPoints[7].y + mBorderWidth), };
            mBgPath = createLIneToPath(bgPoints);
        }
    }

    private Path createLIneToPath(PointF[] points)
    {
        Path path = new Path();
        if (points != null && points.length > 1)
        {
            path.moveTo(points[0].x, points[0].y);
            for (int i = 1; i < points.length; i++)
            {
                path.lineTo(points[i].x, points[i].y);
            }
        }
        path.close();
        return path;
    }

    public int getAnchorYUp()
    {
        return mAnchorYUp;
    }

    public void setAnchorYUp(int mAnchorYUp)
    {
        this.mAnchorYUp = mAnchorYUp;
    }

    public int getAnchorYDown()
    {
        return mAnchorYDown;
    }

    public void setAnchorYDown(int mAnchorYDown)
    {
        this.mAnchorYDown = mAnchorYDown;
    }

    public int getAnchorX()
    {
        return mAnchorX;
    }

    public void setAnchorX(int anchorX)
    {
        this.mAnchorX = anchorX;
    }

    public void setOnTouchListener(OnTouchListener l)
    {
        mOnTouchListener = l;
    }

    public void setDismissAfterTouch(boolean dismissAfterTouch)
    {
        mDismissAfterTouch = dismissAfterTouch;
    }

    public boolean getDismissAfterTouch()
    {
        return mDismissAfterTouch;
    }

    public void setShowDown(boolean showDown)
    {
        mShowDown = showDown;
        if (mShowDown)
        {
            setPadding(getPaddingLeft(), (int) mAnchorHeight + mPadding, getPaddingRight(), mPadding);
        }
        else
        {
            setPadding(getPaddingLeft(), mPadding, getPaddingRight(), (int) mAnchorHeight + mPadding);
        }
    }

    public void setNightMode(boolean isNightMode)
    {
        mIsNightMode = isNightMode;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        if (IS_DEBUG)
        {
            LogUtils.d(TAG, "w=" + w + " h=" + h + " oldw=" + oldw + " oldh=" + oldh);
        }
        mWidth = w;
        mHeight = h;
        show();
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        initPath(mWidth, mHeight);
        mBorderDrawable = new ShapeDrawable(new PathShape(mBorderPath, mWidth, mHeight));
        mBorderDrawable.getPaint().setColor(mBorderColor);
        mBgDrawable = new ShapeDrawable(new PathShape(mBgPath, mWidth, mHeight));
        int bgColor = mBgColor;
        if (mIsNightMode)
        {
            bgColor = mBgColorDark;
        }
        mBgDrawable.getPaint().setColor(bgColor);

        int x = 0;
        int y = 0;
        mBorderDrawable.setBounds(x, y, x + mWidth, y + mHeight);
        mBorderDrawable.draw(canvas);
        mBgDrawable.setBounds(x, y, x + mWidth, y + mHeight);
        mBgDrawable.draw(canvas);
        super.onDraw(canvas);
    }
}

4 下载

Github

csdn下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PopupWindow是一种可以在当前界面上方显示的弹出窗口,通常用于显示一些额外的信息或者提供用户操作的选项。在Android中,可以使用PopupWindow类来创建弹出窗口。 以下是使用PopupWindow的一般步骤: 1. 创建PopupWindow对象:使用PopupWindow的构造函数创建一个PopupWindow对象。 2. 设置PopupWindow的属性:设置PopupWindow的大小、位置、背景等属性。 3. 设置PopupWindow的内容视图:使用setContentView方法设置PopupWindow的内容视图,这可以是一个布局文件或者一个View对象。 4. 显示PopupWindow使用showAsDropDown、showAtLocation等方法显示PopupWindow。 5. 处理PopupWindow的事件:设置PopupWindow的监听器,处理PopupWindow的各种事件。 以下是一个简单的例子,展示如何使用PopupWindow: ``` // 创建PopupWindow对象 PopupWindow popupWindow = new PopupWindow(context); // 设置PopupWindow的属性 popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setFocusable(true); // 设置PopupWindow的内容视图 View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); // 显示PopupWindow popupWindow.showAsDropDown(anchorView); // 处理PopupWindow的事件 contentView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 处理点击事件 popupWindow.dismiss(); } }); ``` 在上面的代码中,我们创建了一个PopupWindow对象,并设置了宽高、背景等属性。然后,我们使用LayoutInflater加载了一个布局文件作为PopupWindow的内容视图,并使用setContentView方法设置了PopupWindow的内容视图。最后,我们使用showAsDropDown方法显示了PopupWindow,并设置了一个点击事件处理器来处理用户点击PopupWindow的事件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值