博客
关于我
自定义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中关于geometry类型的数据_空的时候如何插入处理_需用null_空字符串插入会报错_Cannot get geometry object from dat---MySql工作笔记003
查看>>
mysql中出现Incorrect DECIMAL value: '0' for column '' at row -1错误解决方案
查看>>
mysql中出现Unit mysql.service could not be found 的解决方法
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
Mysql中各类锁的机制图文详细解析(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>
Mysql中存储引擎简介、修改、查询、选择
查看>>
Mysql中存储过程、存储函数、自定义函数、变量、流程控制语句、光标/游标、定义条件和处理程序的使用示例
查看>>
mysql中实现rownum,对结果进行排序
查看>>
mysql中对于数据库的基本操作
查看>>
Mysql中常用函数的使用示例
查看>>
MySql中怎样使用case-when实现判断查询结果返回
查看>>
Mysql中怎样使用update更新某列的数据减去指定值
查看>>
Mysql中怎样设置指定ip远程访问连接
查看>>
mysql中数据表的基本操作很难嘛,由这个实验来带你从头走一遍
查看>>
Mysql中文乱码问题完美解决方案
查看>>
mysql中的 +号 和 CONCAT(str1,str2,...)
查看>>
Mysql中的 IFNULL 函数的详解
查看>>
mysql中的collate关键字是什么意思?
查看>>
MySql中的concat()相关函数
查看>>