叠加图像的最佳/正确方法是什么?

时间:2011-07-15 11:30:13

标签: android

我是Android开发的新手,所以过去几个月一直都在努力。

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

问题在于我不确定正确的方法是什么。

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

  // 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 );
  }

虽然这有效,但我已经阅读了如何使用与主要活动布局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 个答案:

答案 0 :(得分:1)

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

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