Created
July 4, 2017 02:44
-
-
Save toliuweijing/c74205579387833e995af3c8c7e03cf0 to your computer and use it in GitHub Desktop.
123
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.polythinking.weijingliu.contentview; | |
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.graphics.Rect; | |
import android.graphics.drawable.GradientDrawable; | |
import android.support.annotation.Nullable; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v4.view.ViewCompat; | |
import android.text.TextPaint; | |
import android.util.AttributeSet; | |
import android.util.TypedValue; | |
import android.view.View; | |
/** | |
* Created by weijingliu on 6/29/17. | |
*/ | |
public class TallyView extends View { | |
public static final String INITIAL_TEXT = "0000"; | |
private static final int NUM_DIGIT = 4; | |
private static final int MAX_COUNT = (int) Math.pow(10, NUM_DIGIT); | |
public static final int LINE_STROKE_WIDTH = 2; | |
// TODO: What is paint flat. | |
private Paint mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
private TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG); | |
private Rect mTextRect = new Rect(); | |
private int mCount = 0; | |
private String mCountText = "0000"; | |
public static final int LINE_VERTICAL_OFFSET = 20; | |
private float mTextBaseLine; | |
private float mTextOriginX; | |
private float mLineStartX; | |
private float mLineStartY; | |
private float mLineEndX; | |
public TallyView(Context context) { | |
this(context, null); | |
} | |
public TallyView(Context context, @Nullable AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public TallyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { | |
super(context, attrs, defStyleAttr); | |
TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.TallyView, defStyleAttr, 0); | |
mCount = array.getInt(R.styleable.TallyView_count, 0); | |
array.recycle(); | |
mLinePaint.setColor(ContextCompat.getColor(context, R.color.dark_alpha_10)); | |
mLinePaint.setStrokeWidth(dp(LINE_STROKE_WIDTH)); | |
mTextPaint.setColor(Color.WHITE); | |
mTextPaint.setTextSize(dp(100)); | |
mTextPaint.getTextBounds(mCountText, 0, mCountText.length(), mTextRect); | |
if (getBackground() == null) { | |
GradientDrawable backgroundDrawable = new GradientDrawable(); | |
backgroundDrawable.setColor(ContextCompat.getColor(context, R.color.colorPrimary)); | |
backgroundDrawable.setCornerRadius(dp(4)); | |
ViewCompat.setBackground(this, backgroundDrawable); | |
} | |
} | |
private float sp(int sp) { | |
return TypedValue.applyDimension( | |
TypedValue.COMPLEX_UNIT_SP, | |
sp, | |
getResources().getDisplayMetrics()); | |
} | |
private int dp(int dp) { | |
return (int) Math.round( | |
TypedValue.applyDimension( | |
TypedValue.COMPLEX_UNIT_DIP, | |
dp, | |
getResources().getDisplayMetrics())); | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
super.onMeasure(widthMeasureSpec, heightMeasureSpec); | |
int widthSize = mTextRect.width(); | |
int heightSize = mTextRect.height() + | |
(dp(LINE_STROKE_WIDTH) + dp(LINE_VERTICAL_OFFSET)) * 2; | |
setMeasuredDimension( | |
resolveSize(widthSize + getPaddingTop() + getPaddingBottom(), widthMeasureSpec), | |
resolveSize(heightSize + getPaddingLeft() + getPaddingRight(), heightMeasureSpec)); | |
} | |
@Override | |
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { | |
super.onLayout(changed, left, top, right, bottom); | |
float centerX = getWidth() / 2.0f; | |
float centerY = getHeight() / 2.0f; | |
mTextBaseLine = centerY + mTextRect.height() / 2.0f; | |
mTextOriginX = centerX - mTextRect.width() / 2.0f; | |
mLineStartX = mTextOriginX; | |
mLineStartY = mTextBaseLine + dp(LINE_VERTICAL_OFFSET); | |
mLineEndX = mTextOriginX + mTextRect.width(); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
super.onDraw(canvas); | |
canvas.drawText(mCountText, mTextOriginX, mTextBaseLine, mTextPaint); | |
canvas.drawLine(mLineStartX, mLineStartY, mLineEndX, mLineStartY, mLinePaint); | |
} | |
public void setCount(int count) { | |
mCount = count % MAX_COUNT; | |
String tmp = String.valueOf(mCount); | |
StringBuilder stringBuilder = new StringBuilder(); | |
for (int i = 0; i < NUM_DIGIT - tmp.length(); ++i) { | |
stringBuilder.append('0'); | |
} | |
mCountText = stringBuilder.append(tmp).toString(); | |
invalidate(mTextRect); | |
} | |
public void increaseCount() { | |
setCount(mCount + 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment