2016-07-25 13 views
0

나는 안드로이드 개발에서 꽤 새로운데 다음과 같은 문제가있다.이런 식으로 새 비트 맵을 만들 때 왜 검정색 배경을 얻나요? 어떻게 투명 배경을 얻을 수 있습니까?

나는 (이 5 개 아이콘을 서로 옆에 하나를 그릴) 캔버스를 사용하여 비트 맵을 그리는 코드를 구현, 그래서이 내 코드입니다 :

@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)); 

} 

그래서 잘 작동하고 이미지 이미지가 올바르게 표시되고 서로 옆에 하나씩 표시됩니다.

유일한 문제는 star.png 이미지 뒤에있는 새 이미지의 배경이 검정색이라는 것입니다. star.png 이미지의 배경이 흰색입니다.

는 나는이 선으로 따라 생각 : 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); 

Bitmap.Config.RGB_565에 의해.

정확히 무엇을 의미합니까?

투명 배경을 얻으려면 어떻게이 값을 변경할 수 있습니까? createBitmap에 대한 안드로이드 문서에서

+1

'Bitmap.config.ARGB_8888'로 변경하십시오. 투명도는 알파 채널에 의해 정의되기 때문에 (ARGB의 "A"). "RGB"를 사용하는 경우 알파 채널이 없음 => 투명도가 없음 – wanpanman

+0

또한 'png'자체에 투명한 배경이 있는지 확인하십시오. '흰색'은 투명하지 않습니다. –

+0

RGB_565는 빨간색으로 5 비트, 초록색으로 6 개, 파란색은 5, 알파는 0입니다. 바이트 단위로 비트 맵을 작게하지만 색상 깊이는 더 적어 투명성이 없습니다. 투명도에서는 일반적으로 ARGB_8888- 8을 각 색상에 사용하고 8을 투명도로 사용합니다. –

답변

0

(예 : 흰색 배경을 얻기 위해 또는 적어도, 색상을 변경) 당신은 그것을 발견 할 것이다 : (마지막 인수) Android Doc for createBitmap

config가 픽셀 별 알파 (예 : RGB_565)를 지원하지 않으면 색상 []의 알파 바이트는 무시됩니다 (FF로 가정).

따라서 마지막 인수로 Bitmap.Config.ARGB_8888을 사용하십시오.

이 구성에서 각 픽셀은 4 바이트에 저장됩니다.