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

标签 java android

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

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/

相关文章:

java - 在java中反转字符串数组

java - 使用 Apache HttpClient 发布 MultipartEntity 时,ByteArrayBody 可以工作,但 InputStreamBody 会抛出 NoHttpResponseException

java - 在 Scala 模板中,如何在没有空格的情况下结束变量?

java - JSONObject ["featured_image"] 不是字符串

c# - 如何将 post 请求从 Android 发送到 C# web API

java - 将.gif图像转换为base64以上传到服务器

android - Andengine,在不保持宽高比的情况下缩放图像以填充屏幕大小

java - 当我使用 Liberty 将 Java 应用程序部署到 IBM Bluemix 时,构建包将其重命名为 myapp.war。如何保留原来的war文件名?

Android:如何在 ListView 中永久删除一个项目?

android - 让我的 GPS 传感器停止时遇到一些问题