android drawPoint()没有绘制任何东西

时间:2012-04-13 20:04:12

标签: java android graphics2d ondraw

我是一名业余程序员,一直在玩android开发。我正在尝试生成分形高度图。为了测试地图是否正确生成,我需要能够将地图绘制到屏幕上。这是我遇到麻烦的地方。

这是我的MainActivity,DrawMap类(我的onDraw()所在的代码)和我的CanvasThread()类的代码。

主要活动:

package com.psstudios.HMG;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.OnClickListener;
import android.util.Log;

public class MainActivity extends Activity
{
/** Called when the activity is first created. */
Button Startgen = null;
Boolean run=true;

private void log(String text) {
    Log.d("heightmap",text);
    AppendLog app = new AppendLog(text);
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    Startgen = (Button) findViewById(R.id.startgen);

    log("loop");
    Startgen.setOnClickListener(new  View.OnClickListener(){
        public void onClick(View v){
            log("Starting world generation");
            WorldGen world=new WorldGen(200,200);
            log("World generation complete, drawing heightmap");
            setContentView(new DrawMap(MainActivity.this, world));
            log("Went too far");
        }
    });
    run=false;

}   

}

DrawMap:

package com.psstudios.HMG;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Bitmap.*;
import android.graphics.Paint;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import android.util.Log;
import android.util.*;


public class DrawMap extends SurfaceView implements SurfaceHolder.Callback
{
int drawtype=1;             //variable for which type of map to draw 
Bitmap drawbitmap=null;
CanvasThread canvasThread=null;
Boolean run= true;

int Worldx=200;
int Worldy=200;

WorldGen _world=null;

public void log(String text){
    Log.d("Heightmap", text);
    AppendLog app = new AppendLog(text);    
}

public void init(){
    log("init();");
    canvasThread=new CanvasThread(getHolder(), this);
    setFocusable(true);
    log("Post-CanvasThread");
}

public DrawMap(Context context){
    super(context);
    log("DrawMap(Context context)");
    getHolder().addCallback(this);
    init();
}

public DrawMap(Context context, AttributeSet attrs) {
    super(context, attrs);
    log("DrawMap(Context context, AttributeSet attrs)");
    getHolder().addCallback(this);
    init();

}

public DrawMap(Context context,WorldGen world){
    super(context); 
    log("DrawMap(Context context, WorldGen world)");
    getHolder().addCallback(this);
    _world=world;
    init();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format,int width, int height){
    log("surfaceChanged()");
}
@Override
public void surfaceCreated(SurfaceHolder holder){
    log("Surface Created");
    canvasThread.setRunning(true);
    canvasThread.run();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
    log("Surface Destroyed");
    Boolean retry = true;
    canvasThread.setRunning(false);
    while(retry){
        try {
            canvasThread.join();
            retry=false;
        } catch (InterruptedException e) {
            //we will try again and again
        }
    }
}

@Override
public void onDraw(Canvas canvas){
    log("onDraw()");
    Paint paint = new Paint();
    Paint paint2 = new Paint();
    //canvas.drawColor(Color.BLACK);

    paint.setColor(Color.WHITE);
    paint.setStrokeWidth(10);
    paint2.setColor(Color.BLUE);


    for(int x=0; x<Worldx-1; x++){
        for(int y=0; y<Worldy-1; y++){
            //log(x + " : " + y);
            canvas.drawPoint(x+20, y+20, paint);
            canvas.drawPoint(x+220, y+220, paint2);
        }
    }
}
}

CanvasThread:

package com.psstudios.HMG;

import android.graphics.Canvas;
import android.view.*;
import android.util.Log;

public class CanvasThread extends Thread
{
    private SurfaceHolder _surfaceHolder;
    private DrawMap _drawMap;
    private Boolean _run = false;

    private void log(String text){
        Log.d("HeightmapGen",text);
        AppendLog app = new AppendLog(text);
    }

    public CanvasThread(SurfaceHolder surfaceHolder, DrawMap drawMap){
        _surfaceHolder=surfaceHolder;
        _drawMap=drawMap;
        log("CanvasThread()");
    }

    public void setRunning(Boolean run){
        log("setRunning()");
        _run=run;
    }

    @Override
    public void run() {
        log("run()");
        Canvas c;
        while(_run){
            c=null;
            try{
                c=_surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    _drawMap.onDraw(c);
                }
            } finally {
                if(c != null){
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}

程序进入onDraw();功能,并通过for循环运行,但屏幕上没有任何显示。我想我错过了一些非常愚蠢的东西,但我似乎无法弄清楚出了什么问题。

提前感谢你们给我的任何帮助。

0 个答案:

没有答案