安卓:我如何可以覆盖的背景这个水平? [英] Android: How can I overlay this level on the background?

查看:126
本文介绍了安卓:我如何可以覆盖的背景这个水平?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制背景之上的水平,但我不确定如何做到这一点。我能够单独绘制每件的,但我就怎么画都在屏幕上完全消失。我相信我可能要绘制不同的方法的背景下,但我不知道这样的方法。

I am trying to draw a level on top of a background, but I am unsure how to do it. I am able to draw each of the pieces separately, but am completely lost on how to draw both on the screen. I believe I may have to draw the background with a different method, but I don't know of such a method.

下面是我的code迄今:

Here is my code thus far:

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中

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>

通过注释1的setContentView而不是其他我能画成一片,但我无法弄清楚如何吸取两者。我听说过一种叫ViewFlipper,但我不确定我是否应该使用与否;我不知道它是如何工作的。

By commenting one setContentView and not the other I can draw one piece, but I can't figure out how to draw both. I've heard of something called ViewFlipper, but am unsure if I should use that or not; I don't know how it works.

非常感谢你对任何人可以帮助我!

Thank you very much to anyone that can help me!

推荐答案

您应该添加自定义视图(TileBackground)添加到您activity_main.xml中布局文件。这应该允许它被正确绘制当你在使用activity_main的setContentView。你可能会想它作为一个RelativeLayout的父母下的第一个元素,这样它就总是在后台绘制。

You should add your custom view (TileBackground) to your activity_main.xml layout file. That should allow it to be drawn properly when you use setContentView on activity_main. You'll probably want it as the first element under a RelativeLayout parent so that it gets drawn in the background.

在你的情况,你应该有作为你的RelativeLayout的第一个元素:

In your case, you should have as your first element of the RelativeLayout:

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

这篇关于安卓:我如何可以覆盖的背景这个水平?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆