I am new to Android development so have been picking things up over the past couple of months.
The app that I am writing requires 2 images to be displayed, one of top of the other. The image on top has some transparent areas where I want the image below to appear.
The problem is that I am unsure what the correct method is to do this.
The code I originally wrote looks something like this:
// 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 );
}
Although this works, I have since read ab开发者_JAVA技巧out how the same thing can be achieved using an ImageView positioned on top of the view associated with the app in the main activites layout xml file:
<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>
So my question is really which is the correct method? Is one faster than the other?
Any thoughts would be appreciated.
:-)
Many thanks, Wayne.
I'd say both do what they are supposed to be. Which one you choose really depends on your use-case. Do you need pixel-perfect placement? Do you have to redraw/alter the picture rather often at runtime? Use Canvas. Otherwise, the FrameLayout is more versatile and does not require any custom code to draw just two images.
Im not sure speedwise, I'd say measure and see what you get. See this tutorial here, if you are new to Android you should read this. It's useful to know in general.
精彩评论