自定义view实现三角形布局可用于 PopupWindow

package com.jingyun.wallet.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.jingyun.wallet.R;

import androidx.annotation.Nullable;

/**
 * @author dkjuan
 */
public class TriangleView extends View {

	private final int TOP = 0;
	private final int BOTTOM = 1;
	private final int RIGHT = 2;
	private final int LEFT = 3;

	/**
	 * 画三角的画笔
	 */
	private Paint mPaint = new Paint();

	/**
	 * 三角的颜色
	 */
	private int color = 0xFF000000;

	/**
	 * 三角的宽度
	 */
	private int width = 50;

	/**
	 * 三角的高度
	 */
	private int height = 50;

	/**
	 * 三角的方向
	 */
	private int direction = TOP;

	public TriangleView(Context context) {
		super(context);
	}

	public TriangleView(Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
		TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TriangleView, 0, 0);
		color = a.getColor(R.styleable.TriangleView_triangle_color, color);
		width = (int) a.getDimension(R.styleable.TriangleView_resolutionWidth, width);
		height = (int) a.getDimension(R.styleable.TriangleView_resolutionHeight, height);
		direction = a.getInt(R.styleable.TriangleView_direction, direction);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(width, height);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		//设置画笔的颜色
		mPaint.setColor(color);
		//设置画笔抗锯齿
		mPaint.setAntiAlias(true);
		//设置画笔为实心的
		mPaint.setStyle(Paint.Style.FILL);
		//设置画笔的路径
		Path path = new Path();
		switch (direction) {
			case TOP:
				path.moveTo(0, width);
				path.lineTo(width, height);
				path.lineTo(width / 2, 0);
				break;
			case BOTTOM:
				path.moveTo(0, 0);
				path.lineTo(width / 2, height);
				path.lineTo(width, 0);
				break;
			case RIGHT:
				path.moveTo(0, 0);
				path.lineTo(0, height);
				path.lineTo(width, height / 2);
				break;
			case LEFT:
				path.moveTo(0, height / 2);
				path.lineTo(width, height);
				path.lineTo(width, 0);
				break;
			default:
				break;
		}

		path.close();
		canvas.drawPath(path, mPaint);
	}

}
<declare-styleable name="TriangleView">
    <attr name="resolutionWidth" format="dimension" />
    <attr name="resolutionHeight" format="dimension" />
    <attr name="triangle_color" format="color" />
    <attr name="direction">
        <enum name="top" value="0" />
        <enum name="bottom" value="1" />
        <enum name="right" value="2" />
        <enum name="left" value="3" />
    </attr>
</declare-styleable>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值