TextDrawable.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * ownCloud Android client application
  3. *
  4. * @author Andy Scherzinger
  5. * @author Tobias Kaminsiky
  6. * Copyright (C) 2016 ownCloud Inc.
  7. * Copyright (C) 2018 Andy Scherzinger
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2,
  11. * as published by the Free Software Foundation.
  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 General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. package com.owncloud.android.ui;
  22. import android.graphics.Canvas;
  23. import android.graphics.Color;
  24. import android.graphics.ColorFilter;
  25. import android.graphics.Paint;
  26. import android.graphics.PixelFormat;
  27. import android.graphics.drawable.Drawable;
  28. import android.support.annotation.NonNull;
  29. import com.owncloud.android.authentication.AccountUtils;
  30. import com.owncloud.android.utils.BitmapUtils;
  31. import com.owncloud.android.utils.NextcloudServer;
  32. import java.io.UnsupportedEncodingException;
  33. import java.security.NoSuchAlgorithmException;
  34. import java.util.Locale;
  35. /**
  36. * A Drawable object that draws text (1 character) on top of a circular/filled background.
  37. */
  38. public class TextDrawable extends Drawable {
  39. /**
  40. * the text to be rendered.
  41. */
  42. private String mText;
  43. /**
  44. * the text paint to be rendered.
  45. */
  46. private Paint mTextPaint;
  47. /**
  48. * the background to be rendered.
  49. */
  50. private Paint mBackground;
  51. /**
  52. * the radius of the circular background to be rendered.
  53. */
  54. private float mRadius;
  55. /**
  56. * Create a TextDrawable with the given radius.
  57. *
  58. * @param text the text to be rendered
  59. * @param r rgb red value
  60. * @param g rgb green value
  61. * @param b rgb blue value
  62. * @param radius circle radius
  63. */
  64. public TextDrawable(String text, int r, int g, int b, float radius) {
  65. mRadius = radius;
  66. mText = text;
  67. mBackground = new Paint();
  68. mBackground.setStyle(Paint.Style.FILL);
  69. mBackground.setAntiAlias(true);
  70. mBackground.setColor(Color.rgb(r, g, b));
  71. mTextPaint = new Paint();
  72. mTextPaint.setColor(Color.WHITE);
  73. mTextPaint.setTextSize(radius);
  74. mTextPaint.setAntiAlias(true);
  75. mTextPaint.setTextAlign(Paint.Align.CENTER);
  76. }
  77. /**
  78. * creates an avatar in form of a TextDrawable with the first letter of the account name in a circle with the
  79. * given radius.
  80. *
  81. * @param accountName the account name
  82. * @param radiusInDp the circle's radius
  83. * @return the avatar as a TextDrawable
  84. * @throws UnsupportedEncodingException if the charset is not supported when calculating the color values
  85. * @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
  86. */
  87. @NonNull
  88. @NextcloudServer(max = 12)
  89. public static TextDrawable createAvatar(String accountName, float radiusInDp) throws
  90. UnsupportedEncodingException, NoSuchAlgorithmException {
  91. String username = AccountUtils.getAccountUsername(accountName);
  92. return createNamedAvatar(username, radiusInDp);
  93. }
  94. /**
  95. * creates an avatar in form of a TextDrawable with the first letter of the account name in a circle with the
  96. * given radius.
  97. *
  98. * @param userId userId to use
  99. * @param radiusInDp the circle's radius
  100. * @return the avatar as a TextDrawable
  101. * @throws UnsupportedEncodingException if the charset is not supported when calculating the color values
  102. * @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
  103. */
  104. @NonNull
  105. @NextcloudServer(max = 12)
  106. public static TextDrawable createAvatarByUserId(String userId, float radiusInDp) throws
  107. UnsupportedEncodingException, NoSuchAlgorithmException {
  108. return createNamedAvatar(userId, radiusInDp);
  109. }
  110. /**
  111. * creates an avatar in form of a TextDrawable with the first letter of a name in a circle with the
  112. * given radius.
  113. *
  114. * @param name the name
  115. * @param radiusInDp the circle's radius
  116. * @return the avatar as a TextDrawable
  117. * @throws UnsupportedEncodingException if the charset is not supported when calculating the color values
  118. * @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
  119. */
  120. @NonNull
  121. public static TextDrawable createNamedAvatar(String name, float radiusInDp) throws
  122. UnsupportedEncodingException, NoSuchAlgorithmException {
  123. int[] hsl = BitmapUtils.calculateHSL(name);
  124. int[] rgb = BitmapUtils.HSLtoRGB(hsl[0], hsl[1], hsl[2], 1);
  125. return new TextDrawable(name.substring(0, 1).toUpperCase(Locale.getDefault()), rgb[0], rgb[1], rgb[2],
  126. radiusInDp);
  127. }
  128. /**
  129. * Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color
  130. * filter (set via setColorFilter) a circular background with a user's first character.
  131. *
  132. * @param canvas The canvas to draw into
  133. */
  134. @Override
  135. public void draw(@NonNull Canvas canvas) {
  136. canvas.drawCircle(mRadius, mRadius, mRadius, mBackground);
  137. canvas.drawText(mText, mRadius, mRadius - ((mTextPaint.descent() + mTextPaint.ascent()) / 2), mTextPaint);
  138. }
  139. @Override
  140. public void setAlpha(int alpha) {
  141. mTextPaint.setAlpha(alpha);
  142. }
  143. @Override
  144. public void setColorFilter(ColorFilter cf) {
  145. mTextPaint.setColorFilter(cf);
  146. }
  147. @Override
  148. public int getOpacity() {
  149. return PixelFormat.TRANSLUCENT;
  150. }
  151. }