这是解决您问题的方法之一。 Canvas 只是一个位图。无论你在上面画什么,它都是永久的。有办法处理它,但大多数实现只是重新绘制整个 Canvas 在每一遍上。
下面是代码 fragment :
public class PuzzleView extends View {
private static final String TAG = "Sudoku";
private float width; // width of one tile
private float height; // height of one tile
private int selX; // X index of selection
private int selY; // Y index of selection
private final Rect selRect = new Rect();
private final Game game;
float positionX = 5;
float positionY = 15;
ArrayList<Kirti> data = new ArrayList<Kirti>();
String strgettile = null;
Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
String getorientation;
String characterToString;
Path path;
List<Path> mTextPaths = new ArrayList<Path>();
public PuzzleView(Context context) {
super(context);
this.game = (Game) context;
setFocusable(true);
setFocusableInTouchMode(true);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
width = w / 9f;
height = h / 9f;
// getRect(selX, selY, selRect);
Log.d(TAG, "onSizeChanged: width " + width + ", height " + height);
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
// canvas.save();
// canvas.restore();
// Draw the background...
Paint background = new Paint();
background.setColor(getResources().getColor(R.color.puzzle_background));
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
// Draw the board...
// Define colors for the grid lines
Paint dark = new Paint();
dark.setColor(getResources().getColor(R.color.puzzle_dark));
Paint hilite = new Paint();
hilite.setColor(getResources().getColor(R.color.puzzle_hilite));
Paint light = new Paint();
light.setColor(getResources().getColor(R.color.puzzle_light));
// Draw the minor grid lines
for (int i = 0; i < 9; i++) {
canvas.drawLine(0, i * height, getWidth(), i * height, light);
canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(), light);
canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
hilite);
}
// Draw the major grid lines
for (int i = 0; i < 9; i++) {
if (i % 3 != 0)
continue;
canvas.drawLine(0, i * height, getWidth(), i * height, dark);
canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
hilite);
canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
hilite);
}
Paint hint = new Paint();
path = new Path();
int m;
for (int i = 0; i < data.size(); i++) {
String strgettile = data.get(i).getSt();
if (strgettile != null) {
for (m = 0; m < strgettile.length(); m++) {
System.out.println(strgettile.charAt(m));
char convertst = strgettile.charAt(m);
characterToString = Character.toString(convertst);
if (data.get(i).getorientation
.equalsIgnoreCase("Horizontal")) {
canvas.drawText(characterToString, m
* data.get(i).getWidth()
+ data.get(i).getXposition(), data.get(i)
.getYposition(), foreground); // for motion
// event
hint.setColor(Color.BLACK);
hint.setTextSize(45);
} else {
// mTextPaths.add(strgettile);
// for (int i = 0; i < mTextPaths.size(); i++) {
canvas.drawText(characterToString, data.get(i)
.getXposition(), m * data.get(i).getHeight()
+ data.get(i).getYposition(), foreground);
// }
hint.setColor(Color.BLACK);
hint.setTextSize(45);
}
}
try {
if (foreground != null) {
foreground.getTextPath(characterToString, 0,
characterToString.length(), data.get(i)
.getXposition() * 2 / 3, data.get(i)
.getYposition() - 4, path);
}
} catch (Exception e) {
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() != MotionEvent.ACTION_DOWN)
game.showKeypadOrError(selX, selY);
foreground.setColor(getResources().getColor(R.color.puzzle_foreground));
foreground.setStyle(Style.FILL);
foreground.setTextSize(height * 0.75f);
foreground.setTextScaleX(width / height);
foreground.setTextAlign(Paint.Align.CENTER);
FontMetrics fm = foreground.getFontMetrics();
positionX = (int) event.getX();
positionY = (int) event.getY() - (fm.ascent + fm.descent) / 2;
return true;
}
public void setSelectedTile(String tile, String strorientations) {
// TODO Auto-generated method stub
Log.v("getting string in puzzle view ", tile);
if (game.setTileIfValid(selX, selY, tile)) {
strgettile = tile;
getorientation = strorientations;
mTextPaths.add(path);
data.add(new Kirti(positionX, positionY, strgettile,
getorientation, width, height));
invalidate();
Log.v("getting mtextpaths", mTextPaths.toString());
}
}}
我是一名优秀的程序员,十分优秀!