2011-06-17 27 views
0

我在將圖像繪製到android屏幕時出現問題。現在我正在嘗試爲Android做一個「殭屍射手」的端口,我不知道如何去畫屏幕上的烏龜和所有的殭屍。任何經驗豐富的安卓遊戲程序員都想幫助我?我覺得它比添加一個relativelayout並將一個ImageView添加到該相關佈局...僅僅是爲了顯示一個頭部要容易。我的代碼甚至沒有畫出頭像。我看到的只是在XML文件中定義的背景。繪製到Android屏幕自定義x,y

編輯:我有一個新的方法去看這個,使用Canvas'看LunarLander的源代碼後。我現在做錯了什麼?

主類:

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); 
    } 
} 

烏龜類:

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

發表您的佈局如果可能的話 – 2011-06-17 14:36:39

回答

1

嘗試把的setContentView(R.layout.game)createTurtle前()。

+0

我做到了。沒有工作。 – Evan 2011-06-17 14:12:46

+0

好的,你可以發佈你的佈局文件嗎? – JDx 2011-06-17 14:18:51

1

您使用相同的視圖來添加您的視圖,但沒有將添加的視圖設置爲contentview。 我修改了你的代碼。 使用以下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; 
    } 
} 

感謝 迪帕克