TextDrawable.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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.utils.BitmapUtils;
  29. import java.io.UnsupportedEncodingException;
  30. import java.security.NoSuchAlgorithmException;
  31. /**
  32. * A Drawable object that draws text (1 character) on top of a circular/filled background.
  33. */
  34. public class TextDrawable extends Drawable {
  35. /**
  36. * the text to be rendered.
  37. */
  38. private String mText;
  39. /**
  40. * the text paint to be rendered.
  41. */
  42. private Paint mTextPaint;
  43. /**
  44. * the background to be rendered.
  45. */
  46. private Paint mBackground;
  47. /**
  48. * the radius of the circular background to be rendered.
  49. */
  50. private float mRadius;
  51. /**
  52. * Create a TextDrawable with the given radius.
  53. *
  54. * @param text the text to be rendered
  55. * @param r rgb red value
  56. * @param g rgb green value
  57. * @param b rgb blue value
  58. * @param radius circle radius
  59. */
  60. public TextDrawable(String text, int r, int g, int b, float radius) {
  61. mRadius = radius;
  62. mText = text;
  63. mBackground = new Paint();
  64. mBackground.setStyle(Paint.Style.FILL);
  65. mBackground.setAntiAlias(true);
  66. mBackground.setColor(Color.rgb(r, g, b));
  67. mTextPaint = new Paint();
  68. mTextPaint.setColor(Color.WHITE);
  69. mTextPaint.setTextSize(radius);
  70. mTextPaint.setAntiAlias(true);
  71. mTextPaint.setTextAlign(Paint.Align.CENTER);
  72. }
  73. /**
  74. * creates an avatar in form of a TextDrawable with the first letter of the account name in a circle with the
  75. * given radius.
  76. *
  77. * @param accountName the account name
  78. * @param radiusInDp the circle's radius
  79. * @return the avatar as a TextDrawable
  80. * @throws UnsupportedEncodingException if the charset is not supported when calculating the color values
  81. * @throws NoSuchAlgorithmException if the specified algorithm is not available when calculating the color values
  82. */
  83. @NonNull
  84. public static TextDrawable createAvatar(String accountName, float radiusInDp) throws
  85. UnsupportedEncodingException, NoSuchAlgorithmException {
  86. int[] rgb = BitmapUtils.calculateRGB(accountName);
  87. TextDrawable avatar = new TextDrawable(
  88. accountName.substring(0, 1).toUpperCase(), rgb[0], rgb[1], rgb[2], radiusInDp);
  89. return avatar;
  90. }
  91. /**
  92. * Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color
  93. * filter (set via setColorFilter) a circular background with a user's first character.
  94. *
  95. * @param canvas The canvas to draw into
  96. */
  97. @Override
  98. public void draw(Canvas canvas) {
  99. canvas.drawCircle(mRadius, mRadius, mRadius, mBackground);
  100. canvas.drawText(mText, mRadius, mRadius - ((mTextPaint.descent() + mTextPaint.ascent()) / 2), mTextPaint);
  101. }
  102. @Override
  103. public void setAlpha(int alpha) {
  104. mTextPaint.setAlpha(alpha);
  105. }
  106. @Override
  107. public void setColorFilter(ColorFilter cf) {
  108. mTextPaint.setColorFilter(cf);
  109. }
  110. @Override
  111. public int getOpacity() {
  112. return PixelFormat.TRANSLUCENT;
  113. }
  114. }