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
|
package de.pixart.messenger.ui.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import androidx.core.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import de.pixart.messenger.R;
import de.pixart.messenger.ui.util.StyledAttributes;
public class UnreadCountCustomView extends View {
private int unreadCount;
private Paint paint, textPaint;
private int backgroundColor = 0xff0091ea;
public UnreadCountCustomView(Context context) {
super(context);
init(context);
}
public UnreadCountCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
initXMLAttrs(context, attrs);
init(context);
}
public UnreadCountCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initXMLAttrs(context, attrs);
init(context);
}
private void initXMLAttrs(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.UnreadCountCustomView);
//setBackgroundColor(a.getColor(a.getIndex(0), ContextCompat.getColor(context, R.color.accent)));
setBackgroundColor(StyledAttributes.getColor(context, R.attr.colorAccent));
a.recycle();
}
void init(Context context) {
paint = new Paint();
paint.setColor(StyledAttributes.getColor(context, R.attr.colorAccent));
paint.setAntiAlias(true);
textPaint = new Paint();
textPaint.setColor(Color.WHITE);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setAntiAlias(true);
textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float midx = canvas.getWidth() / 2.0f;
float midy = canvas.getHeight() / 2.0f;
float radius = Math.min(canvas.getWidth(), canvas.getHeight()) / 2.0f;
float textOffset = canvas.getWidth() / 6.0f;
textPaint.setTextSize(0.95f * radius);
canvas.drawCircle(midx, midy, radius * 0.94f, paint);
canvas.drawText(unreadCount > 999 ? "\u221E" : String.valueOf(unreadCount), midx, midy + textOffset, textPaint);
}
public void setUnreadCount(int unreadCount) {
this.unreadCount = unreadCount;
invalidate();
}
public void setBackgroundColor(int backgroundColor) {
this.backgroundColor = backgroundColor;
}
}
|