amulyakhare / TextDrawable

This light-weight library provides images with letter/text like the Gmail app. It extends the Drawable class thus can be used with existing/custom/network ImageView classes. Also included is a fluent interface for creating drawables and a customizable ColorGenerator.
MIT License
3.16k stars 622 forks source link

Convert TextDrawable to Bitmap generates no text #22

Closed MarcAragones closed 9 years ago

MarcAragones commented 9 years ago

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?

niteshkhatri commented 9 years ago

I am having same issue. Did you solved it?

caraus commented 9 years ago

?. Have you run debugger. Dev console. Will show any conflicts. If height= height > 0 Else} Height=1

Developer's console will point out any arguments. Android studio is rich in knowledge. Some devices do not work the same. Code is slightly different.

MarcAragones commented 9 years ago

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
goodlifechris commented 8 years ago
    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));
drayan85 commented 6 years ago

When I try to setImageDrawable on API 16 inside ActionBar ImageView crashing The I have converted to Bitmap and setImageBitmap is working.

android.graphics.Rect.<init> (Rect.java:72)
android.graphics.drawable.ShapeDrawable.mutate (ShapeDrawable.java:377)
android.widget.ImageView.applyColorMod (ImageView.java:1150)
android.widget.ImageView.updateDrawable (ImageView.java:684)
android.widget.ImageView.setImageDrawable (ImageView.java:398)
android.support.v7.widget.AppCompatImageView.setImageDrawable (AppCompatImageView.java:99)

Thanks @MarcAragones , @goodlifechris