彩色盒装字母一拉的Gmail [英] Colored boxed with letters a la Gmail

查看:131
本文介绍了彩色盒装字母一拉的Gmail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道他们是如何产生的,如果每次都产生他们我打​​开应用程序或存储(缓存)。

这只是一个画布(编程)或者使用XML?事情是这样的,然后以编程他们添加字母:

 <形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:形状=矩形>
    <大小机器人:宽=1px的机器人:身高=1DP/>
    [固体机器人:颜色=#FFFFF5EE />
< /形状>
 

解决方案
  

生成每次我打开应用程序或存储(缓存)

小A列,少列B还有用一个小的缓存,但它不是你在想什么。每个区域可以被分成4块,缓存用于检索的特定位置。至于后台去,颜色是使用映射 String.hash code ,以确保相同的电子邮件地址(或钥匙)总是被映射到相同的颜色。但是,实际的信是用画 Canvas.drawText

下面是一个例子:

  / **
 *用于创建{@link位图}包含在英文中使用的信
 *字母或数字,如果没有字母或数字可用,默认图像
 *显示,而不是
 * /
公共类LetterTileProvider {

    / **可用砖的颜色(见R.array.letter_tile_colors)的数量* /
    私有静态最终诠释NUM_OF_TILE_COLORS = 8;

    / **的{@link TextPaint}用于绘制信到瓦* /
    私人最终TextPaint mPaint =新TextPaint();
    / **是包围字母的边界* /
    私人最终矩形mBounds =新的矩形();
    / **的{@link}画布上绘制* /
    私人最终帆布mCanvas =新的Canvas();
    / **所显示的名称的第一个字符* /
    私人最终的char [] mFirstChar =新的char [1];

    / **瓦片的背景颜色* /
    私人最终TypedArray mColors;
    / **用于显示字母的字体大小* /
    私人最终诠释mTileLetterFontSize;
    / **的默认图像显示* /
    私人最终位图mDefaultBitmap;

    / **
     *构造< code取代; LetterTileProvider< / code取代;
     *
     *参数背景下的{@link语境}使用
     * /
    公共LetterTileProvider(上下文的背景下){
        最后的资源资源= context.getResources();

        mPaint.setTypeface(Typeface.create(无衬线光,Typeface.NORMAL));
        mPaint.setColor(Color.WHITE);
        mPaint.setTextAlign(Align.CENTER);
        mPaint.setAntiAlias​​(真正的);

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

        mDefaultBitmap = BitmapFactory.de codeResource(RES,android.R.drawable.sym_def_app_icon);
    }

    / **
     * @参数显示名称用于创建信为砖的名称
     * @参数键入用来生成的背景颜色瓷砖的关键
     * @参数宽度瓦片的所需宽度
     * @参数高度瓦片的所需的高度
     * @返回A {@link位图}包含在英文中使用的信
     *字母或数字,如果没有字母或数字可用,一个
     *默认的图像显示,而不是
     * /
    公共位图getLetterTile(字符串显示名,串键,诠释的宽度,高度INT){
        最后的位图位= Bitmap.createBitmap(宽度,高度,Bitmap.Config.ARGB_8888);
        最终炭firstChar = displayName.charAt(0);

        最后帆布C = mCanvas;
        c.setBitmap(位);
        c.drawColor(pickColor(键));

        如果(isEnglishLetterOrDigit(firstChar)){
            mFirstChar [0] = Character.toUpperCase(firstChar);
            mPaint.setTextSize(mTileLetterFontSize);
            mPaint.getTextBounds(mFirstChar,0,1,mBounds);
            c.drawText(mFirstChar,0,1,0 +宽/ 2,0 +高/ 2
                    +(mBounds.bottom  -  mBounds.top)/ 2,mPaint);
        } 其他 {
            c.drawBitmap(mDefaultBitmap,0,0,NULL);
        }
        返回的位图;
    }

    / **
     *参数C中的字符检查
     * @返回真,如果< code取代; C< / code取代;在英文字母或数字,
     *否则为false
     * /
    私有静态布尔isEnglishLetterOrDigit(焦三){
        回归'A'与LT = C和;&安培; C< ='Z'|| '一'< = C和;&安培; C< ='Z'|| '0'< = C和;&安培; ℃下='9';
    }

    / **
     * @参数键入用来生成瓦片颜色的关键
     返回:一个新的或previously选择颜色< code取代;键< / code取代;用作
     *瓷砖背景颜色
     * /
    私人诠释pickColor(字符串键){
        // String.hash code()是不应该跨越的Java版本发生变化,所以
        //这个应该保证同一个密钥映射到相同的颜色
        最终诠释色= Math.abs(key.hash code())%NUM_OF_TILE_COLORS;
        尝试 {
            返回mColors.getColor(颜色,Color.BLACK);
        } 最后 {
            mColors.recycle();
        }
    }

}
 

下面是默认的颜色和文字大小:

 <! - 所有可能的瓷砖背景颜色 - >
<数组名=letter_tile_colors>
    <项目>#f16364< /项目>
    <项目>#f58559< /项目>
    <项目>#f9a43e< /项目>
    <项目>#e4c62e< /项目>
    <项目>#67bf74< /项目>
    <项目>#59a2be< /项目>
    <项目>#2093cd< /项目>
    <项目>#ad62a7< /项目>
< /阵列>

<! - 默认信瓷砖文字大小 - >
<扪名=tile_letter_font_size> 33sp< /扪>
<! - 在deafult平铺尺寸 - >
<扪名=letter_tile_size> 64dp< /扪>
 

执行

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);

    最后的资源资源= getResources();
    最终诠释tileSize = res.getDimensionPixelSize(R.dimen.letter_tile_size);

    最后LetterTileProvider tileProvider =新LetterTileProvider(本);
    最后的位图letterTile = tileProvider.getLetterTile(名,重点,tileSize,tileSize);

    getActionBar()的setIcon(新BitmapDrawable(getResources(),letterTile))。
}
 

T,E,S,T结果:

I wondered how they are generated and if they are generated everytime I open the app or are stored (cached).

It's just a canvas (programmatically) or they use XML? Something like this, and then programmatically they add the letter:

<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>

解决方案

are generated everytime I open the app or are stored (cached)

Little column A, little column B. There is a small cache used, but it's not what you're thinking. Each tile can be split into 4 pieces, the cache is used to retrieve a particular position. As far as the background goes, the colors are mapped using String.hashCode to ensure the same email address (or key) will always be mapped to the same color. But the actual letter is drawn using Canvas.drawText.

Here's an example:

/**
 * 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();
        }
    }

}

Here are the default colors and text size:

<!-- 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>

Implementation

@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));
}

Results of "T", "E", "S", "T":

这篇关于彩色盒装字母一拉的Gmail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆