|
| 1 | +package com.hackplan.androidarcmenu; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Canvas; |
| 5 | +import android.graphics.Color; |
| 6 | +import android.graphics.Paint; |
| 7 | +import android.support.annotation.ColorInt; |
| 8 | +import android.util.AttributeSet; |
| 9 | +import android.util.TypedValue; |
| 10 | +import android.view.View; |
| 11 | + |
| 12 | +/** |
| 13 | + * Created by Dacer on 19/11/2016. |
| 14 | + */ |
| 15 | + |
| 16 | +public class SimpleCirView extends View { |
| 17 | + |
| 18 | + private Paint bgPaint, textPaint; |
| 19 | + private int radiusInPx; |
| 20 | + private String textStr; |
| 21 | + |
| 22 | + private final static int DEFAULT_BG_COLOR = Color.parseColor("#03A9F4"); |
| 23 | + private final static int DEFAULT_TEXT_COLOR = Color.WHITE; |
| 24 | + private final static int DEFAULT_TEXT_SIZE_IN_SP = 22; |
| 25 | + |
| 26 | + public SimpleCirView(Context context) { |
| 27 | + this(context, null); |
| 28 | + } |
| 29 | + |
| 30 | + public SimpleCirView(Context context, AttributeSet attrs) { |
| 31 | + this(context, attrs, 0); |
| 32 | + } |
| 33 | + |
| 34 | + public SimpleCirView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 35 | + super(context, attrs, defStyleAttr); |
| 36 | + init(context); |
| 37 | + } |
| 38 | + |
| 39 | + public SimpleCirView setText(String text) { |
| 40 | + textStr = text; |
| 41 | + postInvalidate(); |
| 42 | + return this; |
| 43 | + } |
| 44 | + |
| 45 | + public SimpleCirView setTextColor(@ColorInt int color) { |
| 46 | + textPaint.setColor(color); |
| 47 | + postInvalidate(); |
| 48 | + return this; |
| 49 | + } |
| 50 | + |
| 51 | + public SimpleCirView setTextSizeInSp(int sizeInSp) { |
| 52 | + textPaint.setTextSize(spToPx(sizeInSp)); |
| 53 | + postInvalidate(); |
| 54 | + return this; |
| 55 | + } |
| 56 | + |
| 57 | + public SimpleCirView setTextSizeInPx(int sizeInPx) { |
| 58 | + textPaint.setTextSize(sizeInPx); |
| 59 | + postInvalidate(); |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + public SimpleCirView setBackgroundRadiusInPx(int radiusInPx) { |
| 64 | + this.radiusInPx = radiusInPx; |
| 65 | + postInvalidate(); |
| 66 | + return this; |
| 67 | + } |
| 68 | + |
| 69 | + public SimpleCirView setCirColor(@ColorInt int color) { |
| 70 | + bgPaint.setColor(color); |
| 71 | + postInvalidate(); |
| 72 | + return this; |
| 73 | + } |
| 74 | + |
| 75 | + private void init(Context context) { |
| 76 | + radiusInPx = context.getResources().getDimensionPixelSize(R.dimen.default_simple_cir_view_radius); |
| 77 | + bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 78 | + textPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 79 | + |
| 80 | + bgPaint.setColor(DEFAULT_BG_COLOR); |
| 81 | + textPaint.setColor(DEFAULT_TEXT_COLOR); |
| 82 | + textPaint.setTextSize(spToPx(DEFAULT_TEXT_SIZE_IN_SP)); |
| 83 | + textPaint.setTextAlign(Paint.Align.CENTER); |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void onDraw(Canvas canvas) { |
| 89 | + canvas.drawCircle(getWidth()/2, getHeight()/2, radiusInPx, bgPaint); |
| 90 | + int xPos = (canvas.getWidth() / 2); |
| 91 | + int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ; |
| 92 | + canvas.drawText(textStr, xPos, yPos, textPaint); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 97 | + setMeasuredDimension(radiusInPx * 2, radiusInPx * 2); |
| 98 | + } |
| 99 | + |
| 100 | + private int spToPx(float sp) { |
| 101 | + return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getContext().getResources().getDisplayMetrics()); |
| 102 | + } |
| 103 | +} |
0 commit comments