StatusDrawable.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Nextcloud Android client application
  3. *
  4. * @author Tobias Kaminsky
  5. * Copyright (C) 2020 Tobias Kaminsky
  6. * Copyright (C) 2020 Nextcloud GmbH
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui;
  22. import android.content.Context;
  23. import android.graphics.Canvas;
  24. import android.graphics.Color;
  25. import android.graphics.ColorFilter;
  26. import android.graphics.Paint;
  27. import android.graphics.PixelFormat;
  28. import android.graphics.drawable.Drawable;
  29. import com.owncloud.android.utils.BitmapUtils;
  30. import androidx.annotation.DrawableRes;
  31. import androidx.annotation.NonNull;
  32. import androidx.core.content.res.ResourcesCompat;
  33. /**
  34. * A Drawable object that draws a status
  35. */
  36. public class StatusDrawable extends Drawable {
  37. private String text;
  38. private @DrawableRes int icon = -1;
  39. private Paint textPaint;
  40. private final Paint backgroundPaint;
  41. private final float radius;
  42. private Context context;
  43. public StatusDrawable(@DrawableRes int icon, float size, Context context) {
  44. radius = size;
  45. this.icon = icon;
  46. this.context = context;
  47. backgroundPaint = new Paint();
  48. backgroundPaint.setStyle(Paint.Style.FILL);
  49. backgroundPaint.setAntiAlias(true);
  50. backgroundPaint.setColor(Color.argb(200, 255, 255, 255));
  51. }
  52. public StatusDrawable(BitmapUtils.Color color, float size) {
  53. radius = size;
  54. backgroundPaint = new Paint();
  55. backgroundPaint.setStyle(Paint.Style.FILL);
  56. backgroundPaint.setAntiAlias(true);
  57. backgroundPaint.setColor(Color.argb(color.a, color.r, color.g, color.b));
  58. }
  59. public StatusDrawable(String icon, float size) {
  60. text = icon;
  61. radius = size;
  62. backgroundPaint = new Paint();
  63. backgroundPaint.setStyle(Paint.Style.FILL);
  64. backgroundPaint.setAntiAlias(true);
  65. backgroundPaint.setColor(Color.argb(200, 255, 255, 255));
  66. textPaint = new Paint();
  67. textPaint.setColor(Color.WHITE);
  68. textPaint.setTextSize(size);
  69. textPaint.setAntiAlias(true);
  70. textPaint.setTextAlign(Paint.Align.CENTER);
  71. }
  72. /**
  73. * Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color
  74. * filter (set via setColorFilter) a circular background with a user's first character.
  75. *
  76. * @param canvas The canvas to draw into
  77. */
  78. @Override
  79. public void draw(@NonNull Canvas canvas) {
  80. if (backgroundPaint != null) {
  81. canvas.drawCircle(radius, radius, radius, backgroundPaint);
  82. }
  83. if (text != null) {
  84. textPaint.setTextSize(1.6f * radius);
  85. canvas.drawText(text, radius, radius - ((textPaint.descent() + textPaint.ascent()) / 2), textPaint);
  86. }
  87. if (icon != -1) {
  88. Drawable drawable = ResourcesCompat.getDrawable(context.getResources(), icon, null);
  89. drawable.setBounds(0,
  90. 0,
  91. (int) (2 * radius),
  92. (int) (2 * radius));
  93. drawable.draw(canvas);
  94. }
  95. }
  96. @Override
  97. public void setAlpha(int alpha) {
  98. textPaint.setAlpha(alpha);
  99. }
  100. @Override
  101. public void setColorFilter(ColorFilter cf) {
  102. textPaint.setColorFilter(cf);
  103. }
  104. @Override
  105. public int getOpacity() {
  106. return PixelFormat.TRANSLUCENT;
  107. }
  108. }