2011-07-25 62 views
1

我想實現android的ios中的畫布功能。 ios畫布有一個名爲drawChildren的屬性,它基本上是一個管理圖形對象的數組,它可以由畫布控制器添加/刪除。實現帆布android

有沒有一種方法可以幫助我實現這一壯舉?我知道應該使用Canvas類,但是我不知道如何將它排列成數組。

+0

有沒有人有想法? – user788511

+0

您是否得到了解決方案? – Aiapaec

回答

0

我不知道這是否會爲你工作,或者如果你解決了這個,但我做了這個辦法:

Activity.class

//Create an activity to hold everything 
public class GameActivity extends Activity { 
protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game); 
     Rel_main_game = (RelativeLayout) findViewById(R.id.rlt_main); 
     DisplayMetrics dm = new DisplayMetrics(); 
     this.getWindowManager().getDefaultDisplay().getMetrics(dm); 
     final int heightS = dm.heightPixels; 
     final int widthS = dm.widthPixels; 
//this is the SurfaceView that will do the magic 
     game_panel = new GamePanel(getApplicationContext(),this,widthS, heightS); 
     Rel_main_game.addView(game_panel); 

} 

SurfaceView

public class GamePanel extends SurfaceView implements SurfaceHolder.Callback { 

    public MainThread thread;  
    public int ScreenWidth; 
    public int Screenheigt; 
    public GameActivity game; 



    public GamePanel(Context context, GameActivity game, int width , int height) { 
     super(context); 
     getHolder().addCallback(this); 
     this.game = game; 
     this.ScreenWidth=width; 
     this.Screenheigt=height; 
     thread = new MainThread(getHolder(),this);     
     ship = new Ship(BitmapFactory.decodeResource(getResources(), R.drawable.player), 100, 0, ScreenWidth, Screenheigt); 

    } 


void Draw(Canvas canvas){ 
     if (canvas!=null) 
     { 
     ship.draw(canvas); 
     } 
    } 

    void Update(float dt){ 
     ship.udpdate(dt); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {  

    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
     thread.setRunning(true); 
     thread.start(); 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
     boolean retry = true; 
     while (retry) 
     { 
      try 
      { 
       thread.join(); 
       retry=false; 
      } 
      catch (InterruptedException e) 
      { 
      } 
     } 
    } 
} 

並且您添加了繪製和更新方法的子類:

public class Ship { 

    public Bitmap bitmap; 
    public int x; 
    private int y;   
    private int Speed; 
    private int inc; 
    private int ScreenWidth; 
    private int ScreenHeight; 
    public int direction=0; 
    boolean death; 
    float animTime=0; 
    float totalAnimTime = 1; 
    float numFrames; 

    public Ship(Bitmap decodeResource, int x, int y, int screenWidth, int screenheight) { 
     this.bitmap = decodeResource; 
     this.x =screenWidth/2 - bitmap.getWidth()/2; 
     this.y = screenheight - bitmap.getHeight(); 
     Speed=1; 
     inc=0; 
     death=false; 
     ScreenWidth =screenWidth; 
     ScreenHeight =screenheight; 
    } 

    public void draw(Canvas canvas){ 
     if (!death) 
     { 
      canvas.drawBitmap(bitmap, x, y - bitmap.getHeight()/2, null); 
     }      
    } 

    public void update(float dt){ 
     if (death) 
     { 
      animTime += dt; 
     } 
     else 
     { 
      float movement =ScreenWidth/2*dt; 
      if (x - (bitmap.getWidth()/ 2)<ScreenWidth || x > - bitmap.getWidth()/ 2) 
      switch(direction) 
      { 
       case(1): 
       { 
        x+=8;//movement*dt; 
        break; 
       } 
       case(2): 
       { 
        x-=8;//movement*dt; 
        break; 
       } 
      } 
      } 
    } 

等等。