FloatingActionButton with text instead of image

后端 未结 9 1182
说谎
说谎 2020-12-01 00:09

I\'m trying to figure out how can be modified FloatingActionButton from android support library. Can it be used with the text instead of image?

Something like this o

9条回答
  •  -上瘾入骨i
    2020-12-01 00:19

    convert a text into bitmap and use it. its super easy.

    fab.setImageBitmap(textAsBitmap("OK", 40, Color.WHITE));
    
    //method to convert your text to image
    public static Bitmap textAsBitmap(String text, float textSize, int textColor) {
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(textSize);
        paint.setColor(textColor);
        paint.setTextAlign(Paint.Align.LEFT);
        float baseline = -paint.ascent(); // ascent() is negative
        int width = (int) (paint.measureText(text) + 0.0f); // round
        int height = (int) (baseline + paint.descent() + 0.0f);
        Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    
        Canvas canvas = new Canvas(image);
        canvas.drawText(text, 0, baseline, paint);
        return image;
    }
    

提交回复
热议问题