android 自定义显示三角型控件

public class TriangleView extends ImageView {
	private Paint mPaint;
	private float angle;// 旋转角度
          private float mWidth;
	private float mHeight;
         public static final int LEFT = 270;
	public static final int RIGHT = 90;public static final int UP = 0;
	public static final int DOWN = 180;
	public TriangleView(Context context) {
		super(context);
		init(context, null, 0);
	}
	public TriangleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs, 0);
	}
	public TriangleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs, defStyle);
	}
	private void init(Context context, AttributeSet attrs, int defStyle) {
		initPaint();
	}
	private void initPaint() {
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setColor(Color.RED);
		mPaint.setStrokeJoin(Join.ROUND);
		mPaint.setStrokeCap(Cap.ROUND);
		mPaint.setStrokeWidth(3);
	}
	public TriangleView setWidth(float width){
		this.mWidth = width;
		return this;
	}
	public TriangleView setHeight(float height){
		this.mHeight = height;
		return this;
	}
	/**
	 * 
	 * @Title: setAngle 
	 * @Description: 按角度调整箭头方向 0-360
	 * @param @param angle    
	 * @return TriangleView this 
	 * @throws
	 */
	public TriangleView setAngle(float angle) {
		this.angle = angle;
		invalidate();
		return this;
	}
	/**
	 * 
	 * @Title: setDirection 
	 * @Description: 调整箭头方向  LEFT,RIGHT,UP,DOWN
	 * @param @param direction    
	 * @return TriangleView this
	 * @throws
	 */
	public TriangleView setDirection(int direction){
		this.angle = direction;
		invalidate();
		return this;
	}
	/**
	 * 
	 * @Title: setColor 
	 * @Description: 给三角形填充颜色
	 * @param @param color    
	 * @return TriangleView this    
	 * @throws
	 */
	public TriangleView setColor(int color){
		mPaint.setColor(color);
		invalidate();
		return this;
	}
	private void drawTriangle(Canvas canvas) {
		mWidth = getWidth();
		mHeight = getHeight();
		float diameter = mWidth > mHeight ? mHeight : mWidth;// 直径
		RectF rf = new RectF(0, 0, diameter, diameter);
		float left = (mWidth - diameter) / 2;
		float top = (mHeight - diameter) / 2;
		rf.offsetTo(left, top);// 绘制区域移至控件中间,并且填充宽、高中较窄的边
		Path path = new Path();
		path.moveTo(rf.left + rf.width() / 2, 0);
		path.lineTo(rf.left + rf.width() / 2 + calTriangleSileLength(diameter)
				/ 2, rf.top + 3 * diameter / 4);
		path.lineTo(rf.left + rf.width() / 2 - calTriangleSileLength(diameter)
				/ 2, rf.top + 3 * diameter / 4);
		canvas.save();
		canvas.rotate(angle, rf.left + rf.width() / 2, rf.top + rf.height() / 2);
		canvas.drawPath(path, mPaint);
		canvas.restore();
	}
	/**
	 * 
	 * @Title: calTriangleSileLength
	 * @Description: 计算三角形边长
	 * @param @param diameter 
	 * @param @return
	 * @return float
	 * @throws
	 */
	private float calTriangleSileLength(float diameter) {
		return (float) (2 * Math.sqrt(Math.pow(diameter / 2, 2)
				- Math.pow(diameter / 4, 2)));
	}

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

}


前面写过一个给控件设置圆角的背景的drawble类,用起来蛮顺手。今天写一个自定义三角型小控件,有用的拿去

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值