我想创建类似于谷歌联系人应用程序的图标 我希望提供背景颜色和文字并生成图标。
我现在正在尝试的是解决方案here. 我正在使用CircularImageView external library,这似乎是问题所在。事实上,如果我尝试做:
CircularImageView imageView;
....
Drawable d = new TextDrawable("My Text", mycolor, textSize);
imageView.setImageDrawable(d);
我收到此错误:
java.lang.IllegalArgumentException:width和height必须是> 0
在android.graphics.Bitmap.createBitmap(Bitmap.java:829)
在android.graphics.Bitmap.createBitmap(Bitmap.java:808)
在android.graphics.Bitmap.createBitmap(Bitmap.java:775)
在com.pkmmte.view.CircularImageView.drawableToBitmap(CircularImageView.java:327)
如果我使用标准 ImageView ,它可以正常工作。有什么建议?
答案 0 :(得分:1)
我已经在GitHub上找到了一个库,它会产生你想要的效果:
TextDrawable
这个轻量级的库提供了像Gmail应用程序一样的字母/文字图像。它扩展了
Drawable
类,因此可以与现有/自定义/网络ImageView
类一起使用。还包括fluent interface用于创建drawable和可自定义的ColorGenerator
。
![]()
![]()
如果您希望创建自己的实现,请尝试仔细阅读此库的代码并逐步实现您自己的版本
希望有所帮助
答案 1 :(得分:0)
您看到的问题是因为您使用的库不考虑没有固定尺寸的drawable。
您可以看到here它使用了可绘制的内在高度和宽度,默认为-1
,表明drawable只会填充任何大小的位图。
要解决您的问题,您只需覆盖getIntrinsicHeight()
中的TextDrawable
即可返回值> 0
@Override
public int getIntrinsicHeight() {
return 88;
}
@Override
public int getIntrinsicWidth() {
return 88;
}
这表明您的drawable大小为88x88px。为了进一步改善这一点,你需要提供dp - > px值,以便您的drawable不会在更高分辨率的设备上拉伸或挤压在低dpi设备上。