2012-03-26 103 views
0

我根本没有得到这个工作。 我看到这个网络不仅仅只是简单的代码纠正帖子。 但我认为这是为了他人的利益,因为没有任何好的教程。Android画布更新问题

所以我打电话给一个画布,我可以在ondraw()方法上画它,但是当我尝试从游戏循环中抽出时,它什么都没画。我甚至没有得到一个错误messasge。

应用:

public class App extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     draw d = new draw(this); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
           WindowManager.LayoutParams.FLAG_FULLSCREEN); 


     setContentView(d); 

    } 
    public void end() 
    { 
     App.this.finish(); 
    } 
} 

draw.java:

public class draw extends View { 
    Canvas ca; 
    View v; 
    Paint paint; 

    int width; 
    int height; 

    static final int MAX_GAME_SPEED=33; 
    static int fps; 
    boolean running=true; 



    int pw=0,ph=0; 





    public draw(Context context) { 
      super(context); 
     } 

      @Override 
     protected void onDraw(Canvas c){ 
     super.onDraw(c); 
     paint = new Paint(); //Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.FILL); 


     //get screen size 
     WindowManager wm = (WindowManager) this.getContext().getSystemService(Context.WINDOW_SERVICE); 
     Display display = wm.getDefaultDisplay(); 


     width = display.getWidth(); // deprecated 
     height = display.getHeight(); // deprecated 







      // make the entire canvas white 
      paint.setColor(Color.WHITE); 
      c.drawPaint(paint); 


      ca = c; 
      paint.setColor(Color.BLACK); 
      c.drawRect(Math.round(width/3),Math.round(height/3),Math.round((width/3)*2),Math.round((height/3)*2), paint); //position width, position height,width,height 


      paint.setStyle(Paint.Style.FILL); 
      paint.setAntiAlias(true); 
      paint.setTextSize(30); 
      paint.setColor(Color.GREEN); 
      c.drawText(String.valueOf(width)+"x"+String.valueOf(height)+","+Math.round(width/3)+"x"+Math.round(height/3), 30, 200, paint); 
      paint.setColor(Color.GREEN); 
      c.drawText(String.valueOf(width)+"x"+String.valueOf(height)+","+Math.round(width/3)+"x"+Math.round(height/3), 30, 200, paint); 


      Thread myThread = new Thread(new UpdateThread()); 
      myThread.start(); 

    } 
      public void paint(Canvas c) 
      { 

       paint.setColor(Color.GREEN); 
       c.drawRect(20, 5, 50, 100, paint); 

       pw++; 
       ph++; 



       /*if (pw >= width || ph >= height) 
       { 
        pw=0; 
        ph=0; 
       } 
       */ 

      } 
      public Handler updateHandler = new Handler(){ 
       /** Gets called on every message that is received */ 
       // @Override 
       public void handleMessage(Message msg) { 

        paint(ca); 


        super.handleMessage(msg); 
       } 
      }; 



      public class UpdateThread implements Runnable { 

       @Override 
       public void run() { 
        while(true){ 
         draw.this.updateHandler.sendEmptyMessage(0); 

        } 
       } 

      } 

    } 

回答

2

首先: 您发送的更新信息太频繁。我相信你用这个过载了你的消息队列。

其次: 您正在使用保存在onDraw方法的画布实例。我认为这是不对的。 相反,您可以将UpdateThread.run()方法更改为

@Override 
public void run() { 
    while(true){ 
     draw.this.postInvalidate(); 
    } 
} 

这将发送消息无效,并重新绘制您的视图,从而打电话给你的View.onDraw(Canvas)的方法适当帆布。

另外: 如果你想使您的浏览尽可能多的,你一定要实现下一个序列:

static final int MSG_REDRAW = 1; 

Handler handler = new Handler() { 
    public void handleMessage(Message msg) { 
     switch (msg.what) { 
     case MSG_REDRAW: 
      // This method invoked on UI thread - ok. 
      draw.this.invalidate(); 
      sendEmptyMessage(MSG_REDRAW); 
      break; 

     default: 
      super.handleMessage(msg); 
      break; 
     } 
    } 
} 

和代替调度线程(UpdateThread),你只是需要发送第一MSG_REDRAW消息。

+0

非常感谢你这对我有效 – user1293780 2012-03-26 19:40:21

0

所以,你必须在的onDraw()你正在创建一个线程,做图纸视图和?为什么?你应该在UI线程上完成所有的绘图工作,并将你游戏的其他部分养殖到其他线程。

+0

我正在建造这个和老java教程,我已经用来做一个java手机相同的gmae,但你怎么能这样做? – user1293780 2012-03-26 19:13:45

+0

如果你正在做一些类似游戏的事情,你应该使用SurfaceView,因为它可以提供更好的速度。这里有一个如何做到这一点的教程:http://www.mybringback.com/tutorial-series/3281/android-the-basics-33-review-of-surfaceview-and-thread-setup/ – CaseyB 2012-03-26 19:22:19

0

你不需要额外的线程。

只要无效,onDraw和onDraw中的最后一条语句将再次安排。

0

我有一些postInvalidate和线程的问题。 首先尝试添加setWillNotDraw(false); 我在屏幕更改方向时遇到了问题 - 由于调用此函数花费了大量的时间来重绘postInvalidate(),因为我们不知道何时会调用onDraw,但它会是。 为我的解决方案和问题关于这看起来link