4

背景を設定しても、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;
}
4

2 に答える 2

9

同等の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、上記のコードを使用してCanvasからを作成し、そこBitmapに描画する必要がありますDrawable

于 2012-12-26T03:36:58.660 に答える
-3

これは、特定の色のビットマップ画像を作成するのに役立ちます。まず、以下のような名前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[]ピクセル=newint [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));
 }
}

これを使用して、リストアイテムが折りたたまれないようにビットマップをリストビューアイテムに設定できます

于 2012-12-26T03:31:49.723 に答える