1

我是一名业余程序员,一直在玩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;

}   

}

绘制地图:

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);
        }
    }
}
}

画布线程:

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 循环运行,但屏幕上没有显示任何内容。我想我错过了一些非常愚蠢的东西,但我似乎无法弄清楚出了什么问题。

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

4

0 回答 0