| 1 | package tw.nekomimi.nekogram; |
| 2 | |
| 3 | import android.content.Context; |
| 4 | import android.graphics.Canvas; |
| 5 | import android.graphics.Paint; |
| 6 | import android.graphics.RectF; |
| 7 | import android.text.Layout; |
| 8 | import android.text.StaticLayout; |
| 9 | import android.view.View; |
| 10 | |
| 11 | import org.telegram.messenger.AndroidUtilities; |
| 12 | import org.telegram.ui.ActionBar.Theme; |
| 13 | |
| 14 | public class UnreadCountBadgeView extends View { |
| 15 | |
| 16 | public String countString; |
| 17 | StaticLayout countLayout; |
| 18 | int countWidth; |
| 19 | private RectF rect = new RectF(); |
| 20 | |
| 21 | public UnreadCountBadgeView(Context context, String countString) { |
| 22 | super(context); |
| 23 | this.countString = countString; |
| 24 | countWidth = Math.max(AndroidUtilities.dp(12), (int) Math.ceil(Theme.dialogs_countTextPaint.measureText(countString))); |
| 25 | countLayout = new StaticLayout(countString, Theme.dialogs_countTextPaint, countWidth, Layout.Alignment.ALIGN_CENTER, 1.0f, 0.0f, false); |
| 26 | } |
| 27 | |
| 28 | @Override |
| 29 | protected void onDraw(Canvas canvas) { |
| 30 | Paint paint = Theme.dialogs_countPaint; |
| 31 | paint.setAlpha(255); |
| 32 | Theme.dialogs_countTextPaint.setAlpha(255); |
| 33 | int countLeft = AndroidUtilities.dp(5.5f); |
| 34 | int countTop = AndroidUtilities.dp(12.5f); |
| 35 | int x = 0; |
| 36 | rect.set(x, countTop, x + countWidth + AndroidUtilities.dp(11), countTop + AndroidUtilities.dp(23)); |
| 37 | canvas.drawRoundRect(rect, 11.5f * AndroidUtilities.density, 11.5f * AndroidUtilities.density, paint); |
| 38 | |
| 39 | if (countLayout != null) { |
| 40 | canvas.save(); |
| 41 | canvas.translate(countLeft, countTop + AndroidUtilities.dp(4)); |
| 42 | countLayout.draw(canvas); |
| 43 | canvas.restore(); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | @Override |
| 48 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 49 | setMeasuredDimension( |
| 50 | countWidth + AndroidUtilities.dp(29), |
| 51 | AndroidUtilities.dp(48) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | |