2015-07-11 32 views
0

恍如不被這裏所謂的onDraw方法,我在屏幕上得到沒有日誌消息或矩形,我似乎無法找到問題 重要的類: 主要活動類: 包com.binf.riley.paintwall;安卓的onDraw(帆布油畫)不會被調用

import com.binf.riley.paintwall.util.SystemUiHider; 

import android.annotation.TargetApi; 
import android.app.Activity; 
import android.content.Context; 
import android.content.res.Resources; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.Handler; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.RelativeLayout; 

public class GameActivity extends Activity { 

    RelativeLayout relLayout; 
    GameRoot gameRoot; 
    Renderer renderer; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_game); 
     relLayout = (RelativeLayout) findViewById(R.id.relLayout); 

     //create objects (entry point for game) 
     gameRoot = new GameRoot(); 
     renderer = new Renderer(this.getApplicationContext()); 
     onResume(); 
    } 

    @Override 
    protected void onResume() { 
     gameRoot.resume(); 
     renderer.resume(); 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     gameRoot.pause(); 
     renderer.pause(); 
     super.onPause(); 
    } 
} 

破碎類:

package com.binf.riley.paintwall; 


import android.app.Activity; 
import android.app.Fragment; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

/** 
* Created by Riley on 7/11/2015. 
*/ 
public class Renderer { 

    private CanvasRenderer mCanvasRenderer ; 

    public Renderer(Context context) { 
     mCanvasRenderer = new CanvasRenderer(context); 
      } 

    public void resume() { 
     mCanvasRenderer.resume(); 
    } 

    public void pause() { 
     mCanvasRenderer.pause(); 
    } 



    private class CanvasRenderer extends View implements Runnable { 

     Thread thread = null; 
     volatile boolean running = false; 
     volatile boolean drawNeeded = false; 

     public CanvasRenderer(Context context) { 
      super(context); 
      doSetup(); 
     } 

     private void doSetup() { 

     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      Log.i("betterinf","DRAWINGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG"); 
      //preform drawing here!!!!! 
      Paint p = new Paint(); 
      p.setColor(Color.BLACK); 
      p.setStyle(Paint.Style.FILL_AND_STROKE); 
       canvas.drawRect(50, 300, 200, 500, p); 

       Log.i("betterinf","drawing"); 


      //draw has finished, another one is now needed 
      drawNeeded = true; 

     } 

     @Override 
     public void run() { 
      while (running) { 

       if (drawNeeded) { 
        drawNeeded = false; 
        //hands off to UI thread for hardware acc. 

        postInvalidate(); 
        Log.i("betterinf", "draw was needed"); 
       } 
       try { 
        Thread.sleep(10); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 


     public void pause() { 
      Log.i("betterinf","pause"); 
      running = false; 
      while (true) { 
       try { 
        thread.join(); 
        return; 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 

      } 

     } 


     public void resume() { 
      Log.i("betterinf","resume"); 
      running = true; 
      drawNeeded = true; 
      thread = new Thread(this); 
      thread.start(); 
     } 
    } 


} 

回答

1

看來,你甚至不讀一些關於Android。所以顯然沒有任何工作。

  • 您不能自己調用​​生命週期方法,如onResume。系統會調用它;
  • onDraw由系統調用,而不是由您的代碼調用。
  • 你不應該創建新線程來渲染視圖。系統已經爲您創建它;

你必須開始瞭解,至少從這裏渲染:https://developer.android.com/guide/topics/graphics/opengl.html

+0

我知道的onDraw由系統調用,但它不是和你@PandaTurtle自相矛盾那問題 – PandaTurtle

+1

。如果您的視圖不在顯示列表中,系統如何調用'onDraw'方法? 'postInvalidate'幾乎不會做任何事情。 – eleven

+0

@Foxinsocks:他使用的是自定義視圖,而不是GLES,所以你的opengl鏈接不合適 - http://developer.android.com/training/custom-views/index.html更好。我沒有看到他明確地調用了Draw(儘管你對onResume是正確的)。 – fadden