2016-07-25 11 views
0

私はAndroidの開発でかなり新しいです。私は次の問題があります。この方法で新しいビットマップを作成すると、なぜ黒の背景が得られますか?どのように私は透明なバックラウンドを得ることができますか?

私はキャンバスを(それが5つのアイコン同士の横の1を引く)を使用してビットマップを描き、このコードを実装しているので、これは私のコードです:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Retrieve the ImageView having id="star_container" (where to put the star.png image): 
    ImageView myImageView = (ImageView) findViewById(R.id.star_container); 

    // Create a Bitmap image startin from the star.png into the "/res/drawable/" directory: 
    Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.star); 

    // Create a new image bitmap having width to hold 5 star.png image: 
    Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth() * 5, myBitmap.getHeight(), Bitmap.Config.RGB_565); 

    /* Attach a brand new canvas to this new Bitmap. 
     The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: 
     1) a Bitmap to hold the pixels. 
     2) a Canvas to host the draw calls (writing into the bitmap). 
     3) a drawing primitive (e.g. Rect, Path, text, Bitmap). 
     4) a paint (to describe the colors and styles for the drawing). 
    */ 
    Canvas tempCanvas = new Canvas(tempBitmap); 

    // Draw the image bitmap into the cavas: 
    tempCanvas.drawBitmap(myBitmap, 0, 0, null); 
    tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth(), 0, null); 
    tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 2, 0, null); 
    tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 3, 0, null); 
    tempCanvas.drawBitmap(myBitmap, myBitmap.getWidth() * 4, 0, null); 

    myImageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap)); 

} 

だから、罰金や画像作品が正しく作成され、画像が1つずつ表示されます)。

唯一の問題は、新しい画像(star.pngの背景にある画像の背景)が黒色であることです。 star.pngイメージの背景は白です。

私はそれがこのラインで依存だと思う:Bitmap.Config.RGB_565によってparticolarで

// Create a new image bitmap having width to hold 5 star.png image: 
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth() * 5, myBitmap.getHeight(), Bitmap.Config.RGB_565); 

正確にはどういう意味ですか?

透明な背景を得るためにこの値を変更するにはどうすればよいですか? createBitmapのためにAndroidのドキュメントで

+1

'Bitmap.config.ARGB_8888'に変更してください。透過性はアルファチャンネル(ARGBの "A")によって定義されるためです。 "RGB"の場合アルファチャンネル=>透明でない場合 – wanpanman

+0

また、 'png'自体に透明な背景があることを確認してください...' white'は透明ではありません –

+0

RGB_565は、赤色に5ビット、緑色に6、青色は5、アルファは0です。これはバイト単位でビットマップを小さくしますが、色深度は少なく、透過性はありません。透過性の場合は、通常、ARGB_8888〜8を各色、透明度を8にします。 –

答えて

0

を(例では、白い背景を入手するか、少なくとも、色を変更するには)あなたはそれを見つける:(最後の引数用)Android Doc for createBitmap

configがピクセル単位のアルファ(RGB_565など)をサポートしていない場合、 colors []内のアルファバイトは無視されます(FFと仮定されます)

したがって、代わりにBitmap.Config.ARGB_8888を最後の引数として使用してください。

この構成では、各ピクセルは4バイトに格納されます。

関連する問題