2014-04-16 11 views
24

मुझे आश्चर्य हुआ कि वे कैसे उत्पन्न होते हैं और यदि वे हर बार उत्पन्न होते हैं तो मैं ऐप खोलता हूं या संग्रहीत (कैश) होता हूं।अक्षरों के साथ रंगीन रंगीन एक ला जीमेल

यह सिर्फ एक कैनवास (प्रोग्रामेटिक रूप से) है या वे एक्सएमएल का उपयोग करते हैं?

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
    <size android:width="1px" android:height="1dp"/> 
    <solid android:color="#FFFFF5EE/> 
</shape> 

उत्तर

45

हर उत्पन्न कर रहे हैं मैं ऐप्लिकेशन खोलें या जमा हो जाती है (कैश्ड)

लिटिल स्तंभ ए, छोटे स्तंभ बी नहीं है: कुछ इस तरह है, और फिर प्रोग्राम के रूप में वे पत्र जोड़ने एक छोटा कैश इस्तेमाल किया जाता है, लेकिन यह नहीं है कि आप क्या सोच रहे हैं। प्रत्येक टाइल को 4 टुकड़ों में विभाजित किया जा सकता है, कैश का उपयोग किसी विशेष स्थिति को पुनर्प्राप्त करने के लिए किया जाता है। जहां तक ​​पृष्ठभूमि जाती है, उसी ईमेल पते (या कुंजी) को हमेशा उसी रंग में मैप किया जाएगा, यह सुनिश्चित करने के लिए रंग String.hashCode का उपयोग करके मैप किए जाते हैं। लेकिन वास्तविक पत्र Canvas.drawText का उपयोग करके तैयार किया गया है।

/** 
* Used to create a {@link Bitmap} that contains a letter used in the English 
* alphabet or digit, if there is no letter or digit available, a default image 
* is shown instead 
*/ 
public class LetterTileProvider { 

    /** The number of available tile colors (see R.array.letter_tile_colors) */ 
    private static final int NUM_OF_TILE_COLORS = 8; 

    /** The {@link TextPaint} used to draw the letter onto the tile */ 
    private final TextPaint mPaint = new TextPaint(); 
    /** The bounds that enclose the letter */ 
    private final Rect mBounds = new Rect(); 
    /** The {@link Canvas} to draw on */ 
    private final Canvas mCanvas = new Canvas(); 
    /** The first char of the name being displayed */ 
    private final char[] mFirstChar = new char[1]; 

    /** The background colors of the tile */ 
    private final TypedArray mColors; 
    /** The font size used to display the letter */ 
    private final int mTileLetterFontSize; 
    /** The default image to display */ 
    private final Bitmap mDefaultBitmap; 

    /** 
    * Constructor for <code>LetterTileProvider</code> 
    * 
    * @param context The {@link Context} to use 
    */ 
    public LetterTileProvider(Context context) { 
     final Resources res = context.getResources(); 

     mPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL)); 
     mPaint.setColor(Color.WHITE); 
     mPaint.setTextAlign(Align.CENTER); 
     mPaint.setAntiAlias(true); 

     mColors = res.obtainTypedArray(R.array.letter_tile_colors); 
     mTileLetterFontSize = res.getDimensionPixelSize(R.dimen.tile_letter_font_size); 

     mDefaultBitmap = BitmapFactory.decodeResource(res, android.R.drawable.sym_def_app_icon); 
    } 

    /** 
    * @param displayName The name used to create the letter for the tile 
    * @param key The key used to generate the background color for the tile 
    * @param width The desired width of the tile 
    * @param height The desired height of the tile 
    * @return A {@link Bitmap} that contains a letter used in the English 
    *   alphabet or digit, if there is no letter or digit available, a 
    *   default image is shown instead 
    */ 
    public Bitmap getLetterTile(String displayName, String key, int width, int height) { 
     final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     final char firstChar = displayName.charAt(0); 

     final Canvas c = mCanvas; 
     c.setBitmap(bitmap); 
     c.drawColor(pickColor(key)); 

     if (isEnglishLetterOrDigit(firstChar)) { 
      mFirstChar[0] = Character.toUpperCase(firstChar); 
      mPaint.setTextSize(mTileLetterFontSize); 
      mPaint.getTextBounds(mFirstChar, 0, 1, mBounds); 
      c.drawText(mFirstChar, 0, 1, 0 + width/2, 0 + height/2 
        + (mBounds.bottom - mBounds.top)/2, mPaint); 
     } else { 
      c.drawBitmap(mDefaultBitmap, 0, 0, null); 
     } 
     return bitmap; 
    } 

    /** 
    * @param c The char to check 
    * @return True if <code>c</code> is in the English alphabet or is a digit, 
    *   false otherwise 
    */ 
    private static boolean isEnglishLetterOrDigit(char c) { 
     return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z' || '0' <= c && c <= '9'; 
    } 

    /** 
    * @param key The key used to generate the tile color 
    * @return A new or previously chosen color for <code>key</code> used as the 
    *   tile background color 
    */ 
    private int pickColor(String key) { 
     // String.hashCode() is not supposed to change across java versions, so 
     // this should guarantee the same key always maps to the same color 
     final int color = Math.abs(key.hashCode()) % NUM_OF_TILE_COLORS; 
     try { 
      return mColors.getColor(color, Color.BLACK); 
     } finally { 
      mColors.recycle(); 
     } 
    } 

} 

यहाँ डिफ़ॉल्ट रंग और पाठ आकार के होते हैं:

यहाँ एक उदाहरण है

<!-- All of the possible tile background colors --> 
<array name="letter_tile_colors"> 
    <item>#f16364</item> 
    <item>#f58559</item> 
    <item>#f9a43e</item> 
    <item>#e4c62e</item> 
    <item>#67bf74</item> 
    <item>#59a2be</item> 
    <item>#2093cd</item> 
    <item>#ad62a7</item> 
</array> 

<!-- The default letter tile text size --> 
<dimen name="tile_letter_font_size">33sp</dimen> 
<!-- The deafult tile size --> 
<dimen name="letter_tile_size">64dp</dimen> 

कार्यान्वयन

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    final Resources res = getResources(); 
    final int tileSize = res.getDimensionPixelSize(R.dimen.letter_tile_size); 

    final LetterTileProvider tileProvider = new LetterTileProvider(this); 
    final Bitmap letterTile = tileProvider.getLetterTile("name", "key", tileSize, tileSize); 

    getActionBar().setIcon(new BitmapDrawable(getResources(), letterTile)); 
} 

"टी" के परिणाम, "ई "," एस "," टी ":

T

E

S

T

+4

उस कोड के लिए लाइसेंस क्या है? –

+0

कुंजी क्या होनी चाहिए? – SMahdiS

+0

ऐसा लगता है कि कोड [मेल ऐप से व्युत्पन्न] है (https://android.googlesource.com/platform/packages/apps/UnifiedEmail/+/184ec73/src/com/android/mail/photomanager/LetterTileProvider.java) और __Apache लाइसेंस 2.0__ के तहत लाइसेंस प्राप्त है। – Phil

0

@ adneal की प्रतिक्रिया पर बिल्डिंग, अगर हम इस प्रकार वृत्ताकार आइकन हम getLetterTile समारोह संशोधित कर सकते हैं उत्पन्न करना चाहते हैं:

public Bitmap getLetterTile(String displayName, String key, int width, int height, boolean round) { 
    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
    final char firstChar = displayName.charAt(0); 

    final Canvas c = mCanvas; 
    c.setBitmap(bitmap); 

    if (round) { 
     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 
     paint.setColor(pickColor(key)); 
     c.drawCircle(width/2, height/2 , width/2, paint); 
    } else { 
     c.drawColor(pickColor(key)); 
    } 

    if (isEnglishLetterOrDigit(firstChar)) { 
     mFirstChar[0] = Character.toUpperCase(firstChar); 
     mPaint.setTextSize(mTileLetterFontSize); 
     mPaint.getTextBounds(mFirstChar, 0, 1, mBounds); 
     c.drawText(mFirstChar, 0, 1, 0 + width/2, 0 + height/2 
       + (mBounds.bottom - mBounds.top)/2, mPaint); 
    } else { 
     c.drawBitmap(mDefaultBitmap, 0, 0, null); 
    } 
    return bitmap; 
} 
संबंधित मुद्दे