Colored boxed with letters a la Gmail

前端 未结 2 714
一整个雨季
一整个雨季 2020-12-04 21:53

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?

2条回答
  •  醉酒成梦
    2020-12-04 22:36

    Building up on @adneal's response, if we want to generate circular icons we can modify the getLetterTile function as follows:

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

提交回复
热议问题