Android:如何在后台覆盖此级别?

时间:2012-12-31 19:44:24

标签: android canvas

我试图在背景上绘制一个关卡,但我不确定该怎么做。我可以分别绘制每个部分,但我完全迷失了如何在屏幕上绘制两个部分。我相信我可能不得不用不同的方法绘制背景,但我不知道这种方法。

到目前为止,这是我的代码:

public class DisplayMap extends Activity {

ImageView mapView;
String FILENAME = "World.tmx";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);
    setContentView(new TileBackground(this));
}

public void loadWorld(String path) {
    ImageView mapView;
    // Start the parser, get back TMX data object
    TileMapData t = TMXLoader.readTMX(FILENAME, this);

    mapView = (ImageView) findViewById(R.id.MapImage);

    // Create a Bitmap from the tilemap data
    Bitmap mapImage = TMXLoader.createBitmap(t, this, 0, t.layers.size());

    // Set the imageview to show the map, if we have one
    if (mapImage != null) {
        mapView.setImageBitmap(mapImage);
    }
    // Map loading problem, inform the user.
    else {
        Toast errorMessage = Toast.makeText(getApplicationContext(),
                "Map could not be loaded", Toast.LENGTH_LONG);
        errorMessage.show();
    }
}

class TileBackground extends View {
    Bitmap bg;

    public TileBackground(Context context) {
        super(context);

        try {
            AssetManager assetManager = context.getAssets();
            InputStream inputStream;
            inputStream = assetManager.open("Background.png");

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_4444;

            bg = BitmapFactory.decodeStream(inputStream, null, options);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDraw(Canvas canvas) {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        int iWidth = bg.getWidth();
        int iHeight = bg.getHeight();

        for (int x = 0; x < width; x += iWidth) {
            for (int y = 0; y < height; y += iHeight)
                canvas.drawBitmap(bg, x, y, null);
        }
        loadWorld(FILENAME);
        invalidate();
    }
}

activity_main.xml中

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <com.seanheiss.shovelshovel.DisplayMap.TileBackground
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/TileBackground" />

    <ImageView
        android:id="@+id/MapImage"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>

通过评论一个setContentView而不是另一个我可以绘制一个,但我无法弄清楚如何绘制它们。我听说过一种名为ViewFlipper的东西,但不确定我是否应该使用它;我不知道它是如何运作的。

非常感谢能帮助我的人!

1 个答案:

答案 0 :(得分:0)

您应该将自定义视图(TileBackground)添加到activity_main.xml布局文件中。这应该允许在activity_main上使用setContentView时正确绘制它。您可能希望它作为RelativeLayout父级下的第一个元素,以便它在后台绘制。

在您的情况下,您应该拥有RelativeLayout的第一个元素:

<com.YOUR_PACKAGE.TileBackground
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/TileBackground" />
相关问题