android 带圆角的View(原创)

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.android_rrhy.Activity.R;

/**
 * Created by fanqibo on 2018/2/2.
 */

public class AngleView extends View {
    private Paint paint = new Paint();
    //颜色
    private int bgColor = Color.YELLOW;
    //百分比
    private float proportion = 1f;
    //高度
    private int bgHeight = 0;

    public AngleView(Context context) {
        super(context);
    }

    public AngleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public AngleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        @SuppressLint("Recycle")
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.AngleView, defStyle, 0);
        try {
            bgColor = a.getColor(R.styleable.AngleView_bg_color, bgColor);
            proportion = a.getFloat(R.styleable.AngleView_percentage, proportion);
            bgHeight = a.getDimensionPixelSize(R.styleable.AngleView_bg_height, (int) dp2px(8));
        } finally {
            a.recycle();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, measureHeight(heightMeasureSpec));
    }

    private int measureHeight(int measureSpec) {
        int specMode = MeasureSpec.getMode(measureSpec);
        int specSize = MeasureSpec.getSize(measureSpec);
        int result = (int) (bgHeight + getPaddingTop() + getPaddingBottom());
        if (specMode == MeasureSpec.AT_MOST) {
            result = (int) (bgHeight + getPaddingTop() + getPaddingBottom());
        } else if (specMode == MeasureSpec.EXACTLY) {
            result = specSize;
        }
        return result;
    }

    public void setBgHeight(float bgHeight) {
        if (bgHeight == this.bgHeight) {
            return;
        }
        invalidate();
    }

    public void setPercentage(float percentage) {
        if (percentage == this.proportion) {
            return;
        }
        if (percentage >= 0) {
            this.proportion = Math.min(percentage, 1f);
            invalidate();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStrokeWidth(bgHeight);
        paint.setAntiAlias(true);
        paint.setColor(bgColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeCap(Paint.Cap.ROUND);
        canvas.drawLine(getPaddingLeft() + bgHeight / 2, getPaddingTop() + bgHeight / 2, proportion * (getPaddingRight() + getWidth() - bgHeight / 2), getPaddingTop() + bgHeight / 2, paint);
    }

    public float dp2px(float dp) {
        return (getContext().getResources().getDisplayMetrics().density * dp + 0.5f);
    }

    public void setBgColor(@ColorInt int bgColor) {
        if (bgColor == this.bgColor) {
            return;
        }
        bgColor = bgColor;
        paint.setColor(bgColor);
        invalidate();
    }

    @Deprecated
    public void setBgColorResource(@ColorRes int borderColorRes) {
        setBgColor(getContext().getResources().getColor(borderColorRes));
    }
}
attr
 
<declare-styleable name="AngleView">
    <attr name="bg_color" format="color" />
    <attr name="bg_height" format="dimension" />
    <attr name="percentage" format="float"/>
</declare-styleable>

 
用法:
 
<com.android_rrhy.Activity.statistics.view.AngleView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:bg_height="4dp"
    app:percentage="0.12"
    app:bg_color="@color/red" />


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值