0

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.

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

    <?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>

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!

Sean Heiss
  • 760
  • 1
  • 8
  • 22

1 Answers1

0

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.

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" />
qzikl
  • 631
  • 3
  • 7
  • Hmm, I did what you suggested, but I'm not sure if I did it correctly. I haven't worked much with the Android UI yet.... I've updated my post with the new activity_main.xml file. – Sean Heiss Dec 31 '12 at 20:10
  • Additionally, `OnDraw()` is never called. I'm not sure what's up there. – Sean Heiss Dec 31 '12 at 20:13
  • I've updated my answer to indicate how a custom view should be added to XML. Also, try looking at this S.O. answer for more details on custom views: http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml – qzikl Dec 31 '12 at 20:18
  • OK. I read those questions, and understand it better now. But, I tried to fix my XML file, and now I'm getting an InflateException. I edited my post with my current XML. Thank you very much for your support! – Sean Heiss Dec 31 '12 at 20:36
  • My guess is that your class for your view needs to not be an inner class of DisplayMap – qzikl Dec 31 '12 at 21:30
  • You need a constructor with this signature `public TileBackground(Context context, AttributeSet attrs)` to use it in xml. With that you should be able to see both view by `setContentView(R.layout.activity_main);` – chiuki Jan 01 '13 at 22:46