2011-07-15 114 views
0

我是Android开发的新手,所以在过去的几个月里一直在挑选事物。什么是最好的/正确的方式来覆盖图像

我正在写的应用程序需要显示两个图像,一个在另一个上面。顶部的图像有一些透明区域,我希望下面的图像出现。

问题是,我不确定什么是正确的方法来做到这一点。

我最初写的代码看起来是这样的:

// background worker thread Run() method 
    Canvas canvas = null; 
    try 
    { 
    canvas = _surfaceHolder.lockCanvas(null); 
    synchronized(_surfaceHolder) 
    { 
     canvas.drawBitmap(_backgroundBitmap, 0, 0, null); 
     // ... 
     canvas.drawBitmap(_foregroundBitmap, 0, 0, null); 
    } 
    } 
    finally 
    { 
    if(canvas != null) 
     _surfaceHolder.unlockCanvasAndPost(canvas); 
    } 

虽然这个工程,因为读我有是怎么一回事可以使用定位在与相关联的视图顶部的ImageView的实现在主要活动布局xml文件中的应用程序:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <!-- This is the surface where the application is responsible for drawing to --> 
    <com.test.MainAppView 
     android:id="@+id/mainView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     /> 

    <!-- 
    This is where a foreground graphic could be displayed which allows me 
    to remove the drawBitmap() code from the Run() method shown above. 
    --> 
    <ImageView 
     android:id="@+id/foregroundgraphic" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     /> 
</FrameLayout> 

所以我的问题是真的哪个是正确的方法?一个比另一个快吗?

任何想法将不胜感激。

:-)

非常感谢, 韦恩。

回答

1

我会说,他们都做他们应该是。你选择哪一个取决于你的用例。你需要像素完美的位置?你是否必须在运行时经常重画/改变图片?使用画布。否则,FrameLayout功能更多,不需要任何自定义代码来绘制两个图像。

我不确定速度,我会说措施,看看你得到什么。请参阅本教程here,如果您是Android新手,则应阅读本文。一般来说,这很有用。

+0

好的,谢谢你的建议。很高兴得到一些保证,我所做的是正确的。 – Wayne

相关问题