博客
关于我
自定义View实现三角形(正三角,倒三角)
阅读量:457 次
发布时间:2019-03-06

本文共 3543 字,大约阅读时间需要 11 分钟。

自定义的属性如下:

具体代码如下:

import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.support.annotation.IntDef;import android.support.annotation.Nullable;import android.util.AttributeSet;import android.view.View;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;/** * 三角形 */public class TriangleView extends View{    private Paint paint;    private Path path;    private int color;    private int mode;    private final int DEFAULT_WIDTH=48;    private final int DEFAULT_HEIGHT=24;    private int width = 0;    private int height =0;    /**     * 倒三角     */    public static final int INVERTED = 0;    /**     * 正三角     */    public static final int REGULAR = 1;    @IntDef({INVERTED, REGULAR})    @Retention(RetentionPolicy.SOURCE)    public @interface ShapeMode {}    public TriangleView(Context context) {        this(context,null);    }    public TriangleView(Context context,  @Nullable  AttributeSet attrs) {        this(context,attrs,0);    }    public TriangleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init(context,attrs);    }    private void init(Context context,AttributeSet attrs){        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TriangleView);        color = typedArray.getColor(R.styleable.TriangleView_tlv_color, Color.BLACK);        mode = typedArray.getInt(R.styleable.TriangleView_tlv_mode, INVERTED);        typedArray.recycle();        paint = new Paint();        paint.setColor(color);        paint.setAntiAlias(true);        paint.setStyle(Paint.Style.FILL);        path= new Path();    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        width = measureSize(widthMeasureSpec, DEFAULT_WIDTH);        height = measureSize(heightMeasureSpec, DEFAULT_HEIGHT);        setMeasuredDimension(width, height);    }    private int measureSize(int measureSpec, int defaultSize) {        int newSize = 0;        int mode = MeasureSpec.getMode(measureSpec);        int size = MeasureSpec.getSize(measureSpec);        switch (mode) {            case MeasureSpec.AT_MOST:                newSize = Math.min(size, defaultSize);                break;            case MeasureSpec.EXACTLY:                newSize = size;                break;            case MeasureSpec.UNSPECIFIED:                newSize = defaultSize;                break;        }        return newSize;    }    public void setColor(int color){        this.color=color;        paint.setColor(color);        invalidate();    }    public void setMode(@ShapeMode int mode){        this.mode=mode;        invalidate();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        drawTriangle(canvas);    }    private void drawTriangle(Canvas canvas) {        if(mode==INVERTED) {            path.moveTo(0f, 0f);            path.lineTo(width, 0f);            path.lineTo(width / 2.0f, height);        }        else {            path.moveTo(width/2.0f,0f);            path.lineTo(0,height);            path.lineTo(width,height);        }        path.close();        canvas.drawPath(path, paint);    }}

 

转载地址:http://ftbfz.baihongyu.com/

你可能感兴趣的文章
MySQL 证明为什么用limit时,offset很大会影响性能
查看>>
Mysql 语句操作索引SQL语句
查看>>
MySQL 误操作后数据恢复(update,delete忘加where条件)
查看>>
MySQL 调优/优化的 101 个建议!
查看>>
mysql 转义字符用法_MySql 转义字符的使用说明
查看>>
mysql 输入密码秒退
查看>>
mysql 递归查找父节点_MySQL递归查询树状表的子节点、父节点具体实现
查看>>
mysql 通过查看mysql 配置参数、状态来优化你的mysql
查看>>
mysql 里对root及普通用户赋权及更改密码的一些命令
查看>>
Mysql 重置自增列的开始序号
查看>>
mysql 锁机制 mvcc_Mysql性能优化-事务、锁和MVCC
查看>>
MySQL 错误
查看>>
mysql 随机数 rand使用
查看>>
MySQL 面试题汇总
查看>>
MySQL 面试,必须掌握的 8 大核心点
查看>>
MySQL 高可用性之keepalived+mysql双主
查看>>
MySQL 高性能优化规范建议
查看>>
mysql 默认事务隔离级别下锁分析
查看>>
Mysql--逻辑架构
查看>>
MySql-2019-4-21-复习
查看>>