amulyakhare/TextDrawable

Does not work with vinc3m1/RoundedImageView

Opened this issue · 12 comments

imageView = (RoundedImageView) findViewById(R.id.imageView1);
int mineColor = getResources().getColor(R.color.thumbnail_bg_one);
TextDrawable drawable;
drawable = TextDrawable.builder().buildRoundRect("R", mineColor, 6);
imageView.setImageDrawable(drawable);

The RoundedImageView only has a background but not text is draw.

This is the same issue as issue#78.
Quoted from the answer in isssue#78: "You don't need to use RoundedImageView since TextDrawable already supports rounded corners. Just use a normal ImageView."

This is exactly what I was trying to Say Zeph.

@zephzeph what if I want to load the image from web, if not then use TextDrawable

@byandby @zephzeph @caraus @rachitmishra
Hi,
I am using CircleImageView which is a derivative of RoundedImageView, I have not been able to use this library because Bitmap#createBitmap thrown this exception. java.lang.IllegalArgumentException: width and height must be > 0 . Have you guys been able to find a solution to this while using the CircleImageView?

ok! awesome, will use that!

I've got it working with CircleImageView (https://github.com/hdodenhof/CircleImageView).

What I did was set the TextDrawable to a scale of the length/width of my CircleImageView. For example my CircleImageView is L:56 W:56. I set the TextDrawable to L:112 and W:112 and converted the TextDrawable to a bitmap. Then I passed the bitmap to the CircleImageView.

@KtodaZ How exactly do you set L, W? setIntrinsicWidth(500) doesn't seem to work. Would you mind sharing your solution?

Sure. Here is my code below:

 private final int circleImageViewWidth = 112;
 private final int circleImageViewTextSize = 60;
// Create drawable
TextDrawable drawable = TextDrawable.builder(this)
                .beginConfig()
                .useFont(Typeface.DEFAULT_BOLD)
                .fontSize(circleImageViewTextSize)
                .height(circleImageViewWidth)
                .width(circleImageViewWidth)
                .endConfig()
                .buildRound(Character.toString(firstName.charAt(0)).toUpperCase(), color);
// Convert to bitmap (generic solution found on stackoverflow)
if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;

and my recycler_text_view.xml

<de.hdodenhof.circleimageview.CircleImageView
                        android:id="@+id/circleImageView"
                        android:layout_width="56dp"
                        android:layout_height="56dp"
                        android:src="@drawable/ic_contact_picture" />

@divonas Hope this helps

@KtodaZ Thank you for sharing.

For anyone having problems with RoundedImageView, just set width and height in the TextDrawable builder. This way the RoundedImageView will be able to display it.

@divonas you rock, setting the height and width is all that's needed. I'm using com.makeramen.roundedimageview.RoundedImageView

text color is not set even after setting the color
alPha = TextDrawable.builder() .beginConfig() .height(40) .width(40) .useFont(ViewUtils.getMontesaratfont()) .textColor(R.color.white) .endConfig() .buildRound(letter, generator.getColor(position)); this is my code