Android:onDraw(Canvas canvas)未被调用

时间:2015-07-11 23:31:23

标签: android canvas ondraw

好像在这里没有调用onDraw方法,我在屏幕上没有日志消息或矩形,我似乎无法找到问题  重要课程:     主要活动类:         包com.binf.riley.paintwall;

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 个答案:

答案 0 :(得分:1)

似乎你甚至没有读过关于android的东西。所以显然没有任何工作。

  • 您不能自己调用​​onResume之类的生命周期方法。系统将调用它;
  • onDraw由系统调用,而不是由您的代码调用。
  • 您不应该创建新线程来呈现视图。系统已经为您创建了它;

你必须开始阅读至少从这里开始的渲染:https://developer.android.com/guide/topics/graphics/opengl.html