question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Convert TextDrawable to Bitmap generates no text

See original GitHub issue

Hello, I am trying to generate a Bitmap from TextDrawable but the final result has no text, only appears the background.

Here is my declaration and the function I use:

Bitmap icon = Utilities.getBitmapFromUser(user);
// ...
TextDrawable userDrawable = TextDrawable.builder()
                .buildRound(firstLetter, colors[i]);
// ...
public static Bitmap drawableToBitmap (Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable)drawable).getBitmap();
        }

        int width = drawable.getIntrinsicWidth();
        width = width > 0 ? width : 1;
        int height = drawable.getIntrinsicHeight();
        height = height > 0 ? height : 1;

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }

Do you know any way to do it?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5

github_iconTop GitHub Comments

4reactions
goodlifechriscommented, Sep 6, 2016
    public static Bitmap drawableToBitmap (Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            return ((BitmapDrawable)drawable).getBitmap();
        }


        int width = drawable.getIntrinsicWidth();
        width = width > 0 ? width : 96; // Replaced the 1 by a 96
        int height = drawable.getIntrinsicHeight();
        height = height > 0 ? height : 96; // Replaced the 1 by a 96

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }``
 TextDrawable drawable = TextDrawable.builder()
                .buildRoundRect("A", Color.RED, 10); // radius in px

        Drawable d = new BitmapDrawable(drawableToBitmap(drawable));
1reaction
MarcAragonescommented, Apr 13, 2015

I could solve it by changing the default width and height:

int width = drawable.getIntrinsicWidth();
width = width > 0 ? width : 96; // Replaced the 1 by a 96
int height = drawable.getIntrinsicHeight();
height = height > 0 ? height : 96; // Replaced the 1 by a 96
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert a Drawable to a Bitmap? - Stack Overflow
This converts a BitmapDrawable to a Bitmap. Drawable d = ImagesArrayList.get(0); Bitmap bitmap = ((BitmapDrawable)d).getBitmap();.
Read more >
Drawables overview | Android Developers
When you need to display static images in your app, you can use the Drawable class and its subclasses to draw shapes and...
Read more >
TextDrawable: Draw Some Text! - Wires Are Obsolete
The Drawable framework in Android is a neat and really flexible way to create portions of your UI. Many times have I been...
Read more >
How to Convert Drawable to Bitmap in Android using Jetpack ...
Step by Step Implementation · Step 1: Create a New Project in Android Studio · Step 2: Adding a new color in the...
Read more >
How to convert Drawable to a Bitmap in Android?
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found