自定义View实现三角形(正三角,倒三角)

Posted rainboy2010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自定义View实现三角形(正三角,倒三角)相关的知识,希望对你有一定的参考价值。

自定义的属性如下:

 <declare-styleable name="TriangleView">
        <!--模式-->
        <attr name="tlv_mode">
            <!--倒三角-->
            <enum name="inverted" value="0" />
            <!--正三角-->
            <enum name="regular" value="1" />
        </attr>
        <!--颜色-->
        <attr name="tlv_color" format="color|reference" />
    </declare-styleable>

具体代码如下:

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);
    }
}

 

以上是关于自定义View实现三角形(正三角,倒三角)的主要内容,如果未能解决你的问题,请参考以下文章

自定义View——仿爱奇艺加载动画...

自定义View——仿爱奇艺加载动画...

为啥保守光栅化无法为某些三角形调用片段着色器?

为啥这个 CSS 片段可以画一个三角形? [复制]

搅拌机;自定义导出“平面”三角形顶点

Android (基础自定义组件)viewpagertap滑动器