2014-09-05 77 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,但对于游戏而言可能太慢。