5

I need to initialize View and Canvas for the project I'm working on, but after an hour or so or searching, I can't figure out what to make them equal to.

Here is the code I have so far:

 public class DisplayMap extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            displayMap();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SlickException e) {
            e.printStackTrace();
        }

    }

    public void displayMap() throws IOException, SlickException {
        loadWorld("assets/World.tmx");
    }

    public void loadWorld(String path) throws IOException {
        View view = ?????;
        Canvas canvas = ?????;

        //World loading goes here
    }
    }

So, can anybody please suggest me how to initialize View and Canvas? Or am I going about this in the completely wrong way?

Rookie Programmer Aravind
  • 11,952
  • 23
  • 81
  • 114
Sean Heiss
  • 780
  • 2
  • 9
  • 25

1 Answers1

1

You need to write a custom view that loads the map, and then use the custom view in your activity.

In TMXView.java:

public class TMXView extends View {
  public TMXView(Context context) {
    super(context);
    // Load map
  }

  public void onDraw(Canvas canvas) {
     // Draw the map on the canvas
  }
}

In onCreate of your activity:

 View view = new TMXView(this);
 setContentView(view);

For more information, refer to my talk on custom components: http://www.sqisland.com/talks/android-custom-components/

chiuki
  • 14,580
  • 4
  • 40
  • 38
  • Thank you very much! Do you think you could look at this other question of mine? We are having trouble figuring it out. http://stackoverflow.com/questions/14105504/android-how-can-i-overlay-this-level-on-the-background – Sean Heiss Jan 01 '13 at 00:16