博客
关于我
自定义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/

你可能感兴趣的文章
Mysql5.7深入学习 1.MySQL 5.7 中的新增功能
查看>>
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>