Drawable display wrongly in CircleImageView but correcty in ImageView
See original GitHub issueI created my custom Drawable and set it to CircleImageView.
ImageView iv = (ImageView) findViewById(R.id.image);
AvatarPlaceholder avatarPlaceholder = new AvatarPlaceholder("Lebron");
iv.setImageDrawable(avatarPlaceholder);
CircleImageView iv2 = (CircleImageView) findViewById(R.id.image2);
AvatarPlaceholder avatarPlaceholder2 = new AvatarPlaceholder("Lebron");
iv2.setImageDrawable(avatarPlaceholder2);
Drawable display wrongly in CircleImageView but correcty in ImageView.
05-17 21:34:56.264 18397-18397/com.example.gongzelong.avatardrawable W/System.err: java.lang.IllegalArgumentException: width and height must be > 0
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.graphics.Bitmap.createBitmap(Bitmap.java:841)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.graphics.Bitmap.createBitmap(Bitmap.java:820)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.graphics.Bitmap.createBitmap(Bitmap.java:787)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at de.hdodenhof.circleimageview.CircleImageView.getBitmapFromDrawable(CircleImageView.java:337)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at de.hdodenhof.circleimageview.CircleImageView.initializeBitmap(CircleImageView.java:354)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at de.hdodenhof.circleimageview.CircleImageView.setImageDrawable(CircleImageView.java:285)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at com.example.gongzelong.avatardrawable.MainActivity.onCreate(MainActivity.java:23)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.app.Activity.performCreate(Activity.java:6323)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2385)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2492)
05-17 21:34:56.265 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.app.ActivityThread.access$900(ActivityThread.java:153)
05-17 21:34:56.266 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
05-17 21:34:56.266 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
05-17 21:34:56.266 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.os.Looper.loop(Looper.java:148)
05-17 21:34:56.266 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5458)
05-17 21:34:56.266 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at java.lang.reflect.Method.invoke(Native Method)
05-17 21:34:56.266 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
05-17 21:34:56.266 18397-18397/com.example.gongzelong.avatardrawable W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
AvatarPlaceholder
package com.example.gongzelong.avatardrawable;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.text.TextUtils;
/**
* AvatarPlaceholder which displays user's name initial letter
* when the image was not provided.
*/
public class AvatarPlaceholder extends Drawable {
public static final String DEFAULT_PLACEHOLDER_STRING = "-";
private static final String DEFAULT_PLACEHOLDER_COLOR = "#3F51B5";
private static final String COLOR_FORMAT = "#FF%06X";
static final int DEFAULT_TEXT_SIZE_PERCENTAGE = 33;
private Paint mTextPaint;
private Paint mBackgroundPaint;
private RectF mPlaceholderBounds;
private String mAvatarText;
private int mTextSizePercentage;
private String mDefaultString;
private float mTextStartXPoint;
private float mTextStartYPoint;
public AvatarPlaceholder(String name) {
this(name, DEFAULT_TEXT_SIZE_PERCENTAGE, DEFAULT_PLACEHOLDER_STRING);
}
public AvatarPlaceholder(String name, @IntRange int textSizePercentage) {
this(name, textSizePercentage, DEFAULT_PLACEHOLDER_STRING);
}
public AvatarPlaceholder(String name, @NonNull String defaultString) {
this(name, DEFAULT_TEXT_SIZE_PERCENTAGE, defaultString);
}
public AvatarPlaceholder(String name, @IntRange int textSizePercentage,
@NonNull String defaultString) {
mDefaultString = resolveStringWhenNoName(defaultString);
mAvatarText = convertNameToAvatarText(name);
mTextSizePercentage = textSizePercentage;
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setColor(Color.parseColor("white"));
mTextPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
mBackgroundPaint = new Paint();
mBackgroundPaint.setAntiAlias(true);
mBackgroundPaint.setStyle(Paint.Style.FILL);
mBackgroundPaint.setColor(Color.parseColor(convertStringToColor(name)));
}
@Override
public void draw(@NonNull Canvas canvas) {
if (mPlaceholderBounds == null) {
mPlaceholderBounds = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
setAvatarTextValues();
}
canvas.drawRect(mPlaceholderBounds, mBackgroundPaint);
canvas.drawText(mAvatarText, mTextStartXPoint, mTextStartYPoint, mTextPaint);
}
@Override
public void setAlpha(int alpha) {
mTextPaint.setAlpha(alpha);
mBackgroundPaint.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
mTextPaint.setColorFilter(colorFilter);
mBackgroundPaint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
private void setAvatarTextValues() {
mTextPaint.setTextSize(calculateTextSize());
mTextStartXPoint = calculateTextStartXPoint();
mTextStartYPoint = calculateTextStartYPoint();
}
private float calculateTextStartXPoint() {
float stringWidth = mTextPaint.measureText(mAvatarText);
return (getBounds().width() / 2f) - (stringWidth / 2f);
}
private float calculateTextStartYPoint() {
return (getBounds().height() / 2f) - ((mTextPaint.ascent() + mTextPaint.descent()) / 2f);
}
private String resolveStringWhenNoName(String stringWhenNoName) {
return !TextUtils.isEmpty(stringWhenNoName) ? stringWhenNoName
: DEFAULT_PLACEHOLDER_STRING;
}
private String convertNameToAvatarText(String name) {
return !TextUtils.isEmpty(name) ? name.substring(0, 1).toUpperCase()
: mDefaultString;
}
private String convertStringToColor(String text) {
return !TextUtils.isEmpty(text) ? String.format(COLOR_FORMAT, (0xFFFFFF & text.hashCode()))
: DEFAULT_PLACEHOLDER_COLOR;
}
private float calculateTextSize() {
if (mTextSizePercentage < 0 || mTextSizePercentage > 100) {
mTextSizePercentage = DEFAULT_TEXT_SIZE_PERCENTAGE;
}
return getBounds().height() * (float) mTextSizePercentage / 100;
}
}
I always got this: drawable.getIntrinsicWidth() < 0;
Even if I setBounds to the drawable:
ImageView iv = (ImageView) findViewById(R.id.image);
AvatarPlaceholder avatarPlaceholder = new AvatarPlaceholder("Lebron");
iv.setImageDrawable(avatarPlaceholder);
CircleImageView iv2 = (CircleImageView) findViewById(R.id.image2);
AvatarPlaceholder avatarPlaceholder2 = new AvatarPlaceholder("Lebron");
avatarPlaceholder2.setBounds(0,0,iv2.getWidth(), iv2.getHeight());
iv2.setImageDrawable(avatarPlaceholder2);
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
android - ImageView in circular through XML - Stack Overflow
Even though the image view is circular, the images are smaller with square shape, looks terrible. Any suggestion? 5.0 and above works good....
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 >Android Capture Image from Camera and Gallery - DigitalOcean
In this tutorial we'll develop an application that picks an image from camera or gallery and display that in an ImageView.
Read more >Circular imageview in android studio xml - Weebly
This article aims to help with how to make a circular view of an image in an image using the Android app. A...
Read more >image view not showing image Code Example - Code Grepper
<androidx.appcompat.widget.AppCompatImageView. 2. android:layout_width="50dp". 3. android:layout_height="50dp". 4. app:srcCompat="@drawable/ic_calendar".
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I just ran into this. The problem is,
getIntrinsicWidth()
orgetIntrinsicHeight()
can be less than zero. However,Bitmap.createBitmap
requires width/height to be greater than zero. Workaround is to overridegetIntrinsicWidth
andgetIntrinsicHeight
and provide non-zero values that works for you.marked