我正在创建一个迷宫,想将图像插入其中一个单元中。我可以使用绘画来插入和更改单元格的颜色,但不能插入图像甚至文本。似乎细胞只涂油漆。我想在单元格中插入图像或文本以显示主页或结束位置。迷宫是随机的,因此末端单元可以改变。
这是我正在使用的Java文件。
package com.example.mazegame;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Random;
import java.util.Stack;
public class GameView extends View{
private Cell [] [] cells;
private Cell player, exit;
private static final int COLS=15, ROWS=15;
private static final float WALL_THICKNESS =4;
private float cellSize, hMargin, vMargin;
private Paint wallPaint, startPaint, endPaint;
public Bitmap canvasBitmap;
private Canvas canvas;
private Random random;
public GameView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
wallPaint = new Paint();
wallPaint.setColor(Color.BLACK);
wallPaint.setStrokeWidth(WALL_THICKNESS);
startPaint = new Paint();
startPaint.setColor(Color.BLACK);
endPaint = new Paint();
endPaint.setColor(Color.BLACK);
random = new Random();
canvasBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.wh);
createMaze();
}
private Cell getNeighbor (Cell cell) {
ArrayList <Cell> neighbors =new ArrayList<>();
//left neighbor
if (cell.col >0)
if(!cells[cell.col-1][cell.row].visited)
neighbors.add(cells[cell.col-1][cell.row]);
//right neighbor
if (cell.col < COLS-1)
if(!cells[cell.col+1][cell.row].visited)
neighbors.add(cells[cell.col+1][cell.row]);
//top neighbor
if (cell.row >0)
if(!cells[cell.col][cell.row-1].visited)
neighbors.add(cells[cell.col][cell.row-1]);
//bottom neighbor
if (cell.row <ROWS-1)
if(!cells[cell.col][cell.row+1].visited)
neighbors.add(cells[cell.col][cell.row+1]);
if(neighbors.size()>0 ){
int index = random.nextInt(neighbors.size());
return neighbors.get(index);
}
return null;
}
private void removeWall(Cell current, Cell next){
if (current.col == next.col && current.row == next.row+1){
current.topWall=false;
next.bottomWall = false;
}
if (current.col == next.col && current.row == next.row-1){
current.bottomWall=false;
next.topWall = false;
}
if (current.col == next.col+1 && current.row == next.row){
current.leftWall=false;
next.rightWall = false;
}
if (current.col == next.col-1 && current.row == next.row){
current.rightWall=false;
next.leftWall = false;
}
}
void createMaze() {
Stack<Cell>stack = new Stack<>();
Cell current, next;
cells = new Cell [COLS] [ROWS];
for (int x=0;x<COLS; x++) {
for (int y=0;y<ROWS; y++){
cells [x][y]=new Cell (x,y);
}
}
player =cells [0][0];
exit = cells [COLS-1][ROWS-1];
current = cells [0][0];
current.visited = true;
do {
next=getNeighbor (current);
if (next !=null) {
removeWall(current, next);
stack.push(current);
current = next;
current.visited = true;
}
else
current=stack.pop();
}while (!stack.empty());
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
int width=getWidth();
int height = getHeight();
if (width/height < COLS/ROWS)
cellSize = width/(COLS +1);
else
cellSize=height/(ROWS+1);
hMargin=(width-COLS*cellSize)/2;
vMargin=(height-ROWS*cellSize)/2;
canvas.translate(hMargin, vMargin);
for (int x=0;x<COLS; x++) {
for (int y = 0; y < ROWS; y++) {
if (cells[x][y].topWall)
canvas.drawLine(
x * cellSize,
y * cellSize,
(x + 1) * cellSize,
y * cellSize,
wallPaint);
if (cells[x][y].leftWall)
canvas.drawLine(
x * cellSize,
y * cellSize,
x * cellSize,
(y + 1) * cellSize,
wallPaint);
if (cells[x][y].rightWall)
canvas.drawLine(
(x + 1) * cellSize,
y * cellSize,
(x + 1) * cellSize,
(y + 1) * cellSize,
wallPaint);
if (cells[x][y].bottomWall)
canvas.drawLine(
x * cellSize,
(y + 1) * cellSize,
(x + 1) * cellSize,
(y + 1) * cellSize,
wallPaint);
}
}
float margin =cellSize/10;
canvas.drawRect(
player.col*cellSize+margin,
player.row*cellSize+margin,
(player.col+1)*cellSize-margin,
(player.row+1)*cellSize-margin,
startPaint);
canvas.drawRect(
exit.col*cellSize+margin,
exit.row*cellSize+margin,
(exit.col+1)*cellSize-margin,
(exit.row+1)*cellSize-margin,
endPaint);
}
private class Cell {
boolean
topWall = true,
leftWall = true,
bottomWall = true,
rightWall = true,
visited = false;
int col, row;
public Cell (int col, int row){
this.col=col;
this.row=row;
}
}
}