Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
  2. <size android:width="1px" android:height="1dp"/>
  3. <solid android:color="#FFFFF5EE/>
  4. </shape>
  5.  
  6. /**
  7. * Used to create a {@link Bitmap} that contains a letter used in the English
  8. * alphabet or digit, if there is no letter or digit available, a default image
  9. * is shown instead
  10. */
  11. public class LetterTileProvider {
  12.  
  13. /** The number of available tile colors (see R.array.letter_tile_colors) */
  14. private static final int NUM_OF_TILE_COLORS = 8;
  15.  
  16. /** The {@link TextPaint} used to draw the letter onto the tile */
  17. private final TextPaint mPaint = new TextPaint();
  18. /** The bounds that enclose the letter */
  19. private final Rect mBounds = new Rect();
  20. /** The {@link Canvas} to draw on */
  21. private final Canvas mCanvas = new Canvas();
  22. /** The first char of the name being displayed */
  23. private final char[] mFirstChar = new char[1];
  24.  
  25. /** The background colors of the tile */
  26. private final TypedArray mColors;
  27. /** The font size used to display the letter */
  28. private final int mTileLetterFontSize;
  29. /** The default image to display */
  30. private final Bitmap mDefaultBitmap;
  31.  
  32. /**
  33. * Constructor for <code>LetterTileProvider</code>
  34. *
  35. * @param context The {@link Context} to use
  36. */
  37. public LetterTileProvider(Context context) {
  38. final Resources res = context.getResources();
  39.  
  40. mPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
  41. mPaint.setColor(Color.WHITE);
  42. mPaint.setTextAlign(Align.CENTER);
  43. mPaint.setAntiAlias(true);
  44.  
  45. mColors = res.obtainTypedArray(R.array.letter_tile_colors);
  46. mTileLetterFontSize = res.getDimensionPixelSize(R.dimen.tile_letter_font_size);
  47.  
  48. mDefaultBitmap = BitmapFactory.decodeResource(res, android.R.drawable.sym_def_app_icon);
  49. }
  50.  
  51. /**
  52. * @param displayName The name used to create the letter for the tile
  53. * @param key The key used to generate the background color for the tile
  54. * @param width The desired width of the tile
  55. * @param height The desired height of the tile
  56. * @return A {@link Bitmap} that contains a letter used in the English
  57. * alphabet or digit, if there is no letter or digit available, a
  58. * default image is shown instead
  59. */
  60. public Bitmap getLetterTile(String displayName, String key, int width, int height) {
  61. final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  62. final char firstChar = displayName.charAt(0);
  63.  
  64. final Canvas c = mCanvas;
  65. c.setBitmap(bitmap);
  66. c.drawColor(pickColor(key));
  67.  
  68. if (isEnglishLetterOrDigit(firstChar)) {
  69. mFirstChar[0] = Character.toUpperCase(firstChar);
  70. mPaint.setTextSize(mTileLetterFontSize);
  71. mPaint.getTextBounds(mFirstChar, 0, 1, mBounds);
  72. c.drawText(mFirstChar, 0, 1, 0 + width / 2, 0 + height / 2
  73. + (mBounds.bottom - mBounds.top) / 2, mPaint);
  74. } else {
  75. c.drawBitmap(mDefaultBitmap, 0, 0, null);
  76. }
  77. return bitmap;
  78. }
  79.  
  80. /**
  81. * @param c The char to check
  82. * @return True if <code>c</code> is in the English alphabet or is a digit,
  83. * false otherwise
  84. */
  85. private static boolean isEnglishLetterOrDigit(char c) {
  86. return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9';
  87. }
  88.  
  89. /**
  90. * @param key The key used to generate the tile color
  91. * @return A new or previously chosen color for <code>key</code> used as the
  92. * tile background color
  93. */
  94. private int pickColor(String key) {
  95. // String.hashCode() is not supposed to change across java versions, so
  96. // this should guarantee the same key always maps to the same color
  97. final int color = Math.abs(key.hashCode()) % NUM_OF_TILE_COLORS;
  98. try {
  99. return mColors.getColor(color, Color.BLACK);
  100. } finally {
  101. mColors.recycle();
  102. }
  103. }
  104.  
  105. }
  106.  
  107. <!-- All of the possible tile background colors -->
  108. <array name="letter_tile_colors">
  109. <item>#f16364</item>
  110. <item>#f58559</item>
  111. <item>#f9a43e</item>
  112. <item>#e4c62e</item>
  113. <item>#67bf74</item>
  114. <item>#59a2be</item>
  115. <item>#2093cd</item>
  116. <item>#ad62a7</item>
  117. </array>
  118.  
  119. <!-- The default letter tile text size -->
  120. <dimen name="tile_letter_font_size">33sp</dimen>
  121. <!-- The deafult tile size -->
  122. <dimen name="letter_tile_size">64dp</dimen>
  123.  
  124. @Override
  125. public void onCreate(Bundle savedInstanceState) {
  126. super.onCreate(savedInstanceState);
  127.  
  128. final Resources res = getResources();
  129. final int tileSize = res.getDimensionPixelSize(R.dimen.R.dimen.letter_tile_size);
  130.  
  131. final LetterTileProvider tileProvider = new LetterTileProvider(this);
  132. final Bitmap letterTile = tileProvider.getLetterTile("name", "key", tileSize, tileSize);
  133.  
  134. getActionBar().setIcon(new BitmapDrawable(getResources(), letterTile));
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement