Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How do I initialize View and Canvas?

Tags:

java

android

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?

like image 536
Sean Heiss Avatar asked Nov 12 '22 14:11

Sean Heiss


1 Answers

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/

like image 75
chiuki Avatar answered Nov 15 '22 05:11

chiuki