2014-09-05 170 views
-1

在某些情況下,我必須在另一個頂部添加一個位圖。但我的代碼不起作用。當然,我真的認爲,我沒有做正確的事情,但我不知道如何去做這件事。android繪製位圖高於另一個

之後,我必須將我的位圖轉換爲圖片對象,然後將我的圖片對象添加到我的MarkerOption對象。

這裏我的代碼:

   Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.lifr); 
       Bitmap bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.manche_a_air); 
       Bitmap bmOverlay = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig()); 
       Canvas canvas = new Canvas(bmOverlay); 
       canvas.drawBitmap(bmp, 0.0f, 0.0f, null); 
       canvas.drawBitmap(bmp2, bmp.getWidth(), bmp.getHeight()/2, null); 
       markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bmOverlay)); 

       return markerOptions; 

如果任何人有一個想法?先謝謝你。

+0

爲什麼不使用BitmapDescriptorFactory.fromBitmap(bmOverlay)? – pskink 2014-09-05 11:37:02

回答

0

我會製作一個FrameLayout(子視圖繪製在一個堆棧中,最近添加的子項位於頂部),第二個佈局具有透明背景。請注意,第二個位圖也可能部分透明。如果我需要在運行時移動第二個圖像,我會以編程方式更改佈局參數。

排序:

<FrameLayout ...> 
    <LinearLayout ...> 
    <!-- the 1st bitmap goes here --> 
    </LinearLayout> 
    <LinearLayout android:background="#80000000" ...> <!-- semi-transparent --> 
    <View android:layout_width="80px" .../> <!-- just padding --> 
    <!-- the 2nd bitmap goes here --> 
    ... 
    </LinearLayout> 
</FrameLayout> 

注意,對於精確定位,可以使用水平的LinearLayout一個垂直的LinearLayout內。

以編程方式更改位置有點棘手,您可能需要攔截onGlobalLayout()。

這當然可以用於UI,但對於遊戲而言可能太慢。