android - 如何在android中通过代码创建Drawables?

标签 android android-drawable

<分区>

我想通过 java 代码创建一个简单的蓝色 Drawable。我不想在 res/drawable 文件夹中添加“蓝色”图像。我如何实现这一目标?我需要在 ImageView 中将此可绘制对象用作图像。

enter image description here

最佳答案

只需使用 BitmapDrawable ,这将使您可以将位图设置为源。您可以使用许多函数非常轻松地创建位图,例如:

Bitmap myBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
myBitmap.setPixel(x, y, Color.BLUE);

如果您想要纯色,可以使用该像素创建一个 1x1 位图并根据需要将其放大。

或者,如果您想要更大的灵 active ,您可以使用 Canvas这让您可以使用更多绘图方法(用于矩形、圆形、文本等)。

关于android - 如何在android中通过代码创建Drawables?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15037257/

相关文章:

android - 如何重叠系统底部导航栏。?

Android:加速度计传感器中z值的范围在不同的设备上是不同的

android - SimpleCursorAdapter 中的图像

android - 单独的圆角(不是所有角)

android - 带有 LayerDrawable 的 ImageView 由两个 StateListDrawable 组成,不在一层上绘制按下状态

java - 黑线意外出现在错误的地方

Android 线性布局 : Dividers won't show

android - ProGuard 明显跑不起来

android - 在 Android WebView 中嵌入来自 vimeo 的视频

android - android studio中drawable文件夹的路径