Evan
Evan

Reputation: 167

Drawing to Android screen custom x,y

I'm having a problem drawing an image to the screen of android. Right now I'm trying to do a port of "Zombie Archer" for android, and I'm not sure how to go about drawing the turtle and all of the zombies on the screen. Any seasoned android game programmers around that want to help me? I feel like it has to be easier than adding a relativelayout and adding an ImageView to that relativelayout... just to display a head. My code doesn't even draw the head. All I see is the background defined in the XML file.

EDIT: I have a new method at going about this, using Canvas' after looking at LunarLander's source code. What am I doing wrong now?

MAIN CLASS:

package com.wickeyware.zombiearcher;

import android.app.Activity;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class GameActivity extends Activity
{
    private Turtle turtle;
    private Canvas canvas = new Canvas();
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        final Window win = getWindow();
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.game);
        turtle = new Turtle(this);
    }
    @Override
    public void onResume()
    {
        turtle.Draw(canvas);
    }
}

TURTLE CLASS:

package com.wickeyware.zombiearcher;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;

public class Turtle
{
    // Surface Holder?
    //private SurfaceHolder sh;
    // Background image
    //private Bitmap bmp;
    // All the parts of the turtle
    private Drawable head;
    private Drawable body;
    private Drawable rleg;
    private Drawable lleg;
    private Drawable rarm;
    private Drawable larm;
    private Drawable eye;
    private Drawable mouth;
    private Drawable bow;
    private Drawable tstring;
    private Drawable bstring;
    // Fill the array
    private Drawable[] turtle = {head, body, rleg, lleg, rarm, larm, eye, mouth, bow, tstring, bstring};
    // X,Y coords of turtle
    //private int tX = 0;
    //private int tY = 0;
    // Constructor
    public Turtle(Context context)
    {
        //bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.default_board);
        // Load up turtle parts with drawables
        turtle[0] = context.getResources().getDrawable(R.drawable.head);
        turtle[1] = context.getResources().getDrawable(R.drawable.body);
        turtle[2] = context.getResources().getDrawable(R.drawable.leg);
        turtle[3] = context.getResources().getDrawable(R.drawable.leg);
        turtle[4] = context.getResources().getDrawable(R.drawable.arm);
        turtle[5] = context.getResources().getDrawable(R.drawable.forearm);
        turtle[6] = context.getResources().getDrawable(R.drawable.eye);
        turtle[7] = context.getResources().getDrawable(R.drawable.mouth);
        turtle[8] = context.getResources().getDrawable(R.drawable.bow);
        turtle[9] = context.getResources().getDrawable(R.drawable.halfbowstring);
        turtle[10] = context.getResources().getDrawable(R.drawable.halfbowstring);
    }
    public void Draw(Canvas canvas)
    {
        for(Drawable part : turtle)
        {
            part.setBounds(0,0,0,0);
            part.draw(canvas);
        }
    }
}

Upvotes: 0

Views: 615

Answers (2)

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

You were using the same view to add your view but didnot set the added view to contentview. I have modified your code. Use the following coe.

package com.wickeyware.zombiearcher;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class GameActivity extends Activity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        final Window win = getWindow();
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        RelativeLayout rl = createTurtle();
        setContentView(rl);
    }
    public RelativeLayout createTurtle()
    {
        RelativeLayout rl = (RelativeLayout) View.inflate(this, R.layout.game, null);
        //Turtle turtle = new Turtle(getBaseContext());
        ImageView head = new ImageView(getBaseContext());
        head.setImageResource(R.drawable.head);
        head.setAdjustViewBounds(true);
        //head.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(head.getWidth(), head.getHeight());
        params.leftMargin = 50;
        params.topMargin = 60;
        head.bringToFront();
        rl.addView(head, params);
        return rl;
    }
}

Thanks Deepak

Upvotes: 1

JDx
JDx

Reputation: 2655

Try putting setContentView(R.layout.game) before createTurtle().

Upvotes: 1

Related Questions