gpt4 book ai didi

java - Android - CircularImageView setImageDrawable 以编程方式创建可绘制对象

转载 作者:行者123 更新时间:2023-11-29 20:11:56 25 4
gpt4 key购买 nike

我想创建类似于谷歌联系人应用程序的图标。
我希望提供背景颜色和文本并生​​成图标。

enter image description here

我目前正在尝试的是解决方案 here.
我正在使用 CircularImageView external library这似乎是问题所在。事实上,如果我尝试这样做:

CircularImageView imageView;
....
Drawable d = new TextDrawable("My Text", mycolor, textSize);
imageView.setImageDrawable(d);

我收到这个错误:

java.lang.IllegalArgumentException: width and height must be > 0
at android.graphics.Bitmap.createBitmap(Bitmap.java:829)
at android.graphics.Bitmap.createBitmap(Bitmap.java:808)
at android.graphics.Bitmap.createBitmap(Bitmap.java:775)
at com.pkmmte.view.CircularImageView.drawableToBitmap(CircularImageView.java:327)

如果我使用标准的 ImageView 它就可以工作。有什么建议吗?

最佳答案

我已经在GitHub上找到了一个库,可以实现你想要的效果:

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.

https://github.com/amulyakhare/TextDrawable

如果您希望创建自己的实现,请尝试仔细阅读该库的代码并逐步实现您自己的版本

希望对你有帮助

关于java - Android - CircularImageView setImageDrawable 以编程方式创建可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34643277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com