-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBadgeImageView.java
More file actions
152 lines (126 loc) · 4.04 KB
/
BadgeImageView.java
File metadata and controls
152 lines (126 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.mobiag.awto.utils;
/**
* Created by SBreit
*/
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
public class BadgeImageView extends android.support.v7.widget.AppCompatImageView {
/* private class attributes. Feel free to tweak them to fit your project */
private int mValue = 10;
private boolean mShow = true;
private int mContainerColor = Color.RED;
private int mTextColor = Color.WHITE;
private int mRadius = 0; // <--- don't worry, this gets calculated on draw
private int mTextSize = 12;
private int mTypeface = Typeface.BOLD;
private float mScale;
private Paint mPaint;
private Context mContext;
public BadgeImageView(Context context) {
super(context);
mContext = context;
}
public BadgeImageView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
}
public BadgeImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
}
private Paint getPaint(){
if(mPaint == null){
mPaint = new Paint();
Resources resources = mContext.getResources();
mScale = resources.getDisplayMetrics().density;
Typeface plain = mPaint.getTypeface();
Typeface typeface = Typeface.create(plain, mTypeface);
mPaint.setTypeface(typeface);
mPaint.setTextSize(mTextSize*mScale);
}
return mPaint;
}
public void reset(){
mValue = 0;
mTypeface = Typeface.BOLD;
mContainerColor = Color.WHITE;
mTextSize = 12;
mTextColor = Color.parseColor("lime");
mShow = true;
}
public void setBadgeValue(int badgeValue) {
//Don't show badge if value is less than one
if(badgeValue < 1)
mShow = false;
mValue = badgeValue;
invalidate();
}
public int getBadgeValue() {
return mValue;
}
public void showBadge(boolean showBadge){
mShow = showBadge;
invalidate();
}
public void setBadgeColor(int badgeColor) {
mContainerColor = badgeColor;
invalidate();
}
public int getBadgeColor() {
return mContainerColor;
}
public void setBadgeTextColor(int badgeTextColor) {
mTextColor = badgeTextColor;
invalidate();
}
public int getBadgeTextColor() {
return mTextColor;
}
public void setBadgeTextSize(int badgeTextSize) {
mTextSize = badgeTextSize;
mRadius = badgeTextSize*2/3;
invalidate();
}
public int getBadgeTextSize() {
return mTextSize;
}
public int getBadgeRadius() {
return mRadius;
}
public void setTypeface(int typeface) {
mTypeface = typeface;
invalidate();
}
public int getTypeface() {
return mTypeface;
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
if(mShow){
getPaint();
float pivotX = getPivotX();
float pivotY = getPivotY();
int viewHeight = getMeasuredHeight();
int viewWidth = getMeasuredWidth();
mRadius = (mTextSize * 2) / 3;
float textWidth;
textWidth = mPaint.measureText(mValue + "");
if (textWidth > mRadius * 2 * mScale) {
mRadius = (int) ((textWidth * 2 / 3) / mScale);
}
float x = pivotX + viewWidth / 2 - mRadius * mScale;
float y = pivotY - viewHeight / 2 + mRadius * mScale;
mPaint.setColor(mContainerColor);
canvas.drawCircle(x, y, mRadius * mScale, mPaint);
mPaint.setColor(mTextColor);
canvas.drawText(mValue + "", x - textWidth / 2,
y + mTextSize * mScale / 3, mPaint);
}
}
}