我正在android studios上编写一个寻路代码,我正在使用一个名为drawsurface的视图类。这个类是在activity\u main.xml中使用的。我被困在如何生成细胞,我也有麻烦与ontouch方法。我有两个图像,一个是起点,另一个是终点。有了这个,我必须能够把起点放在任何单元格里,然后把终点放在任何单元格里。一旦发生这种情况,a*代码将生成一条最便宜的路径。然后可以接触任何其他不同的细胞,这将清除细胞除了新的起点,然后程序重新开始。下面是我的drawsurfce类,它包含所有loadnewmap,代码用于创建单元格集、启动单元格集阻止程序、计算屏幕上的单元格数和连接单元格。也不确定我的ondraw是否正确。现在如果我运行的程序,它是一个白色的屏幕,底部有一个信用按钮,我可以触摸屏幕,但它只把开始点放在屏幕上。
深色表面
package edu.fullsail.mgems.cse.pather.allisonhix;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import java.util.ArrayList;
public class DrawSurface extends SurfaceView implements SurfaceHolder.Callback, View.OnTouchListener {
protected Bitmap mBMPStart = BitmapFactory.decodeResource(getResources(), R.drawable.start);
protected Bitmap mBMPEnd = BitmapFactory.decodeResource(getResources(), R.id.end);
protected ArrayList<PointF> mCellStart;
protected ArrayList<PointF> mCellEnd;
protected ArrayList<NavCell> mNavCell;
private int mCellCols;
private int mCellRows;
Rect mScreenDim = new Rect();
public NavCell[][] mCells;
static float CELL_SIZE = 64;
private SurfaceHolder mHolder;
private Object Canvas;
private Object PointF;
private Canvas canvas;
public DrawSurface(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void init() {
getHolder().addCallback(this);
setOnTouchListener(this);
mBMPEnd = BitmapFactory.decodeResource(getResources(), R.drawable.end);
mBMPStart = BitmapFactory.decodeResource(getResources(), R.drawable.start);
setWillNotDraw(false);
mCellStart = new ArrayList<>();
mCellEnd = new ArrayList<>();
View viewById = findViewById(R.id.dsField);
}
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
mHolder = surfaceHolder;
Canvas c = mHolder.lockCanvas();
if (c != null) mScreenDim.set(0, 0, c.getWidth(), c.getHeight());
mHolder.unlockCanvasAndPost(c);
loadNewMap();
invalidate();
}
public void loadNewMap() {
//calculate number of cells based on screen
for (int i = 0; i < mCellCols; i++) {
for (int j = 0; j < mCellRows; j++) {
mCellCols = (int) Math.ceil((float) mScreenDim.width() / (float) CELL_SIZE);
mCellRows = (int) Math.ceil((float) mScreenDim.height() / (float) CELL_SIZE);
}
}
// create cells
mCells = new NavCell[mCellRows][mCellCols];
for (int j = 0; j < mCellRows; j++) {
for (int i = 0; i < mCellCols; i++) {
mCells[j][i] = new NavCell(i, j);
mCells[j][i].setBounds(
i * CELL_SIZE,
j * CELL_SIZE,
(i * CELL_SIZE) + CELL_SIZE,
(j * CELL_SIZE) + CELL_SIZE);
}
}
// set start cell
mCellStart = mCells[mCellRows / 4][mCellCols / 2];
mCellEnd = null;
//set blockers
int midRow = mCellRows / 2;
{
for (int j = 0; j < mCellRows; j++) {
for (int i = 0; i < mCellCols; i++) {
if (j == midRow && i > 0 && i < mCellCols - 1)
mCells[j][i].setImpassable();
}
}
}
// connect cells
for (int j = 0; j < mCellRows; j++) {
for (int i = 0; i < mCellCols; i++) {
if (i > 0 && mCells[j][i - 1].isPassable())
mCells[j][i].setNeighbor(0, mCells[j][i - 1]);
if (j > 0 && mCells[j - 1][i].isPassable())
mCells[j][i].setNeighbor(1, mCells[j - 1][i]);
if (i < mCellCols - 1 && mCells[j][i + 1].isPassable())
mCells[j][i].setNeighbor(2, mCells[j][i + 1]);
if (j < mCellRows - 1 && mCells[j + 1][i].isPassable())
mCells[j][i].setNeighbor(3, mCells[j + 1][i]);
}
}
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mCellEnd != null) {
for (PointF end : mCellEnd) {
canvas.drawBitmap(mBMPEnd, new Rect(0, 0, mBMPEnd.getWidth(), mBMPEnd.getHeight()),
new RectF(end.x - mBMPEnd.getWidth() / 2, end.y - mBMPEnd.getHeight() / 2,
end.x + mBMPEnd.getWidth() / 2, end.y + mBMPEnd.getHeight() / 2), new Paint());
}
if (mBMPStart != null) {
for (PointF start : mCellStart) {
canvas.drawBitmap(mBMPStart, new Rect(0, 0, mBMPStart.getWidth(), mBMPStart.getHeight()),
new RectF(start.x - mBMPStart.getWidth() / 2, start.y - mBMPStart.getHeight() / 2,
start.x + mBMPStart.getWidth() / 2, start.y + mBMPStart.getHeight() / 2), new Paint());
}
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
public boolean onTouch(android.view.View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
PointF start = new PointF(event.getX(), event.getY());
mCellStart.add(start);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
PointF end = new PointF(event.getX(), event.getY());
mCellEnd.add(end);
}
invalidate();
}
return true;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!