我在将图像绘制到android屏幕时遇到问题。现在我正在尝试为Android安装一个“Zombie Archer”端口,我不知道如何在屏幕上绘制乌龟和所有僵尸。任何经验丰富的安卓游戏程序员都想帮助我吗?我觉得它必须比添加一个relativelayout并将ImageView添加到该relativelayout更容易...只是为了显示一个头。我的代码甚至没有画头。我所看到的只是XML文件中定义的背景。
编辑:在查看LunarLander的源代码之后,我使用Canvas进行了一个新的方法。我现在做错了什么?主要课程:
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);
}
}
}
答案 0 :(得分:1)
尝试在createTurtle()之前放置setContentView(R.layout.game)。
答案 1 :(得分:1)
您使用相同的视图添加视图,但没有将添加的视图添加到内容视图。 我修改了你的代码。 使用以下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;
}
}
由于 迪帕克