【问题标题】:create a image with color in android在android中创建带有颜色的图像
【发布时间】:2012-12-11 17:04:08
【问题描述】:

设置背景似乎没有给出任何关于 android 大小的提示。
因此,我正在寻找一种方法来创建具有特定颜色的图像。
(如果能用xml就更好了)

在 iOS 中,这可以通过

来实现
+ (UIImage*)placeHolderImage
{
    static UIImage* image = nil;
    if(image != nil)
        return image;

    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Seashell color                                                                                                                                                                                                                                                           
    UIColor* color = [UIColor colorWithRed:255/255.0 green:245/255.0 blue:238/255.0 alpha:1.0];
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

【问题讨论】:

  • 我对你想要完成的事情有点困惑。颜色与大小有什么关系?您是否尝试过在 axml 布局文件中使用 Image 并明确指定宽度和高度?
  • 我有一个包含图像的列表视图。当我不设置 src 时,图像视图首先具有宽度 0(因此使行折叠),当设置图像时,它的高度会改变..

标签: android image colors


【解决方案1】:

这是等效的Android代码:

// CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
Rect rect = new Rect(0, 0, 1, 1);

//UIGraphicsBeginImageContext(rect.size);
//CGContextRef context = UIGraphicsGetCurrentContext();
Bitmap image = Bitmap.createBitmap(rect.width(), rect.height(), Config.ARGB_8888);
Canvas canvas = new Canvas(image);

//UIColor* color = [UIColor colorWithRed:255/255.0 green:245/255.0 blue:238/255.0 alpha:1.0];
int color = Color.argb(255, 255, 245, 238);

//CGContextSetFillColorWithColor(context, [color CGColor]);
Paint paint = new Paint();
paint.setColor(color);

//CGContextFillRect(context, rect);
canvas.drawRect(rect, paint);

//image = UIGraphicsGetImageFromCurrentImageContext();
//UIGraphicsEndImageContext();
/** nothing to do here, we already have our image **/
/** and the canvas will be released by the GC     **/

现在,如果您想在 XML 中执行此操作,则要容易得多:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <size android:width="1px" android:height="1dp"/>
    <solid android:color="#FFFFF5EE/>
</shape>

虽然这不会给你一个Bitmap,而是一个Drawable。如果你打算把它画在某个地方,那很好。如果你真的需要Bitmap,那么你必须使用上面的代码从Bitmap创建一个Canvas,然后把你的Drawable画进去。

【讨论】:

  • ios和android的对比!
【解决方案2】:

这有助于您创建具有特定颜色的位图图像。 首先创建一个名为 sampleBitmap 的位图,如下所示

Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap sampleBitmap = Bitmap.createBitmap(300, 300, conf); // this creates a MUTABLE bitmap

接下来使用以下代码获取创建的位图的每个像素

// int[] 像素 = new int[sampleBitmap.getHeight()*sampleBitmap.getWidth()];

for (int i=0; i < sampleBitmap.getWidth(); i++)
{
for (int j=0; j < sampleBitmap.getHeight(); i++)
 {
    sampleBitmap.setPixel(i, j, Color.rgb(someColor1, someColor2, someColor3));
 }
}

使用这个你可以将位图设置为列表视图项,这样列表项就不会折叠

【讨论】:

  • 我只是提出了一种可行且我知道的方法。
  • 当然,只是指出从可能的方式来看,这是最糟糕的。一个类似但更好的方法是将图像像素实际设置到您声明但未使用的 pixels 数组中,然后对 setPixels 进行 single 调用。
  • downvoters.. 正如我已经评论过的,这是我知道的一种方式,是的,有更好的答案。我不确定在说这是我知道的一种方式之后投反对票是否是一件好事。
猜你喜欢
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 2018-03-05
  • 2020-03-08
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
  • 1970-01-01
相关资源
最近更新 更多