Skip to content

Commit 59bc6c3

Browse files
wcshiafohrman
authored andcommitted
Add boilerplate code (getters, setters, override abstract methods) define custom attributes for new custom drawable, BadgeDrawable.
PiperOrigin-RevId: 235037646
1 parent 7087f8e commit 59bc6c3

File tree

4 files changed

+260
-0
lines changed

4 files changed

+260
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- Copyright (C) 2019 The Android Open Source Project
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
-->
16+
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
18+
package="com.google.android.material.badge">
19+
20+
<application/>
21+
</manifest>
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
/*
2+
* Copyright (C) 2019 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.material.badge;
18+
19+
import android.content.res.ColorStateList;
20+
import android.graphics.Canvas;
21+
import android.graphics.ColorFilter;
22+
import android.graphics.Paint;
23+
import android.graphics.PixelFormat;
24+
import android.graphics.Rect;
25+
import androidx.annotation.ColorInt;
26+
import androidx.annotation.RestrictTo;
27+
import androidx.annotation.RestrictTo.Scope;
28+
import com.google.android.material.shape.MaterialShapeDrawable;
29+
import android.text.TextPaint;
30+
31+
/**
32+
* BadgeDrawable contains all the layout and draw logic for a badge.
33+
*
34+
* @hide
35+
*/
36+
@RestrictTo(Scope.LIBRARY)
37+
public class BadgeDrawable extends MaterialShapeDrawable {
38+
39+
// Value of -1 denotes an icon only badge.
40+
private static final int ICON_ONLY_BADGE_NUMBER = -1;
41+
42+
private final TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
43+
44+
private int number = ICON_ONLY_BADGE_NUMBER;
45+
private int maxCharacterCount;
46+
private int alpha = 255;
47+
48+
// Getters and setters for attributes.
49+
50+
/**
51+
* Returns this badge's background color.
52+
*
53+
* @see #setbackgroundColor(int)
54+
* @attr ref com.google.android.material.R.styleable#Badge_backgroundColor
55+
*/
56+
@ColorInt
57+
public int getBackgroundColor() {
58+
return getFillColor().getDefaultColor();
59+
}
60+
61+
/**
62+
* Sets this badge's background color.
63+
*
64+
* @param backgroundColor This badge's background color.
65+
* @attr ref com.google.android.material.R.styleable#Badge_backgroundColor
66+
*/
67+
public void setBackgroundColor(@ColorInt int backgroundColor) {
68+
if (filledPaint.getColor() != backgroundColor) {
69+
setFillColor(ColorStateList.valueOf(backgroundColor));
70+
invalidateSelf();
71+
}
72+
}
73+
74+
/**
75+
* Returns this badge's text color.
76+
*
77+
* @see #setBadgeTextColor(int)
78+
* @attr ref com.google.android.material.R.styleable#Badge_badgeTextColor
79+
*/
80+
@ColorInt
81+
public int getBadgeTextColor() {
82+
return textPaint.getColor();
83+
}
84+
85+
/**
86+
* Sets this badge's text color.
87+
*
88+
* @param badgeTextColor This badge's text color.
89+
* @attr ref com.google.android.material.R.styleable#Badge_badgeTextColor
90+
*/
91+
public void setBadgeTextColor(@ColorInt int badgeTextColor) {
92+
if (textPaint.getColor() != badgeTextColor) {
93+
textPaint.setColor(badgeTextColor);
94+
invalidateSelf();
95+
}
96+
}
97+
98+
/**
99+
* Returns this badge's number.
100+
*
101+
* @see #setNumber(int)
102+
* @attr ref com.google.android.material.R.styleable#Badge_number
103+
*/
104+
public int getNumber() {
105+
return number;
106+
}
107+
108+
/**
109+
* Sets this badge's number. Only non-negative integer numbers are supported. If the number is
110+
* negative, it will be clamped to 0. The specified value will be displayed, unless its number of
111+
* digits exceeds {@code maxCharacterCount} in which case a truncated version will be shown.
112+
*
113+
* @param number This badge's number.
114+
* @attr ref com.google.android.material.R.styleable#Badge_number
115+
*/
116+
public void setNumber(int number) {
117+
number = Math.max(0, number);
118+
if (this.number != number) {
119+
this.number = number;
120+
invalidateSelf();
121+
}
122+
}
123+
124+
/** Resets any badge number so that only an icon badge will be displayed. */
125+
public void clearBadgeValue() {
126+
number = ICON_ONLY_BADGE_NUMBER;
127+
invalidateSelf();
128+
}
129+
130+
/**
131+
* Returns this badge's max character count.
132+
*
133+
* @see #setMaxCharacterCount(int)
134+
* @attr ref com.google.android.material.R.styleable#Badge_maxCharacterCount
135+
*/
136+
public int getMaxCharacterCount() {
137+
return maxCharacterCount;
138+
}
139+
140+
/**
141+
* Sets this badge's max character count.
142+
*
143+
* @param maxCharacterCount This badge's max character count.
144+
* @attr ref com.google.android.material.R.styleable#Badge_maxCharacterCount
145+
*/
146+
public void setMaxCharacterCount(int maxCharacterCount) {
147+
if (this.maxCharacterCount != maxCharacterCount) {
148+
this.maxCharacterCount = maxCharacterCount;
149+
invalidateSelf();
150+
}
151+
}
152+
153+
@Override
154+
public void setColorFilter(ColorFilter colorFilter) {
155+
// Intentionally empty.
156+
}
157+
158+
@Override
159+
public int getAlpha() {
160+
return alpha;
161+
}
162+
163+
@Override
164+
public void setAlpha(int alpha) {
165+
if (this.alpha != alpha) {
166+
this.alpha = alpha;
167+
textPaint.setAlpha(alpha);
168+
invalidateSelf();
169+
}
170+
}
171+
172+
@Override
173+
public int getOpacity() {
174+
return PixelFormat.TRANSLUCENT;
175+
}
176+
177+
@Override
178+
public void draw(Canvas canvas) {
179+
Rect bounds = getBounds();
180+
if (bounds.isEmpty() || getAlpha() == 0 || !isVisible()) {
181+
return;
182+
}
183+
super.draw(canvas);
184+
if (number >= 0) {
185+
drawText(canvas);
186+
}
187+
}
188+
189+
private void drawText(Canvas canvas) {
190+
// TODO: Add logic.
191+
}
192+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2019 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<resources>
19+
20+
<declare-styleable name="Badge">
21+
<attr name="backgroundColor" format="color"/>
22+
<attr name="badgeTextColor" format="color"/>
23+
<attr name="maxCharacterCount" format="integer"/>
24+
<attr name="number" format="integer"/>
25+
</declare-styleable>
26+
27+
</resources>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2019 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<resources>
19+
<integer name="badge_max_character_count">4</integer>
20+
</resources>

0 commit comments

Comments
 (0)