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" />