TextDrawable.java 4.9 KB

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