如何在同一画布上保留以前的画面?

时间:2014-11-25 07:11:26

标签: android android-canvas

我能够在动画视图上的画布上绘制文本,现在问题是当我绘制文本时在同一个画布上进行下一次绘制我的绘制文本正在消失我的意思是屏幕正在重绘因为无效我想保留我以前的绘制并在同一画布上进行新绘制我将如何做到这一点?

public class PuzzleView extends View {

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;
String strgettile = null;
float x, y;
Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
String getorientation;

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();
    // 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);
    }
    // // Draw the numbers...


    Paint hint = new Paint();

    int m;
    if (strgettile != null) {
        for (m = 0; m < strgettile.length(); m++) {
            System.out.println(strgettile.charAt(m));

            char convertst = strgettile.charAt(m);
            String characterToString = Character.toString(convertst);

            if (getorientation.equalsIgnoreCase("Horizontal")) {
                canvas.drawText(characterToString, m * width + positionX,
                        positionY, foreground); // for motion event
                hint.setColor(Color.BLACK);
                hint.setTextSize(45);


            } else {
                canvas.drawText(characterToString, positionX, m * height
                        + positionY, foreground);
                hint.setColor(Color.BLACK);
                hint.setTextSize(45);

            }

        }
        //invalidate();
    }

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() != MotionEvent.ACTION_DOWN)
        return super.onTouchEvent(event);
    // select((int) (event.getX() / width), (int) (event.getY() /
    // height));
    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);
    // // Draw the number in the center of the tile
    FontMetrics fm = foreground.getFontMetrics();
    // // Centering in X: use alignment (and X at midpoint)

    // positionX = width / 2;
    // // Centering in Y: measure ascent/descent first
    // positionY = height / 2 - (fm.ascent + fm.descent) / 2;
    positionX = (int) event.getX();
    positionY = (int) event.getY() - (fm.ascent + fm.descent) / 2;

    // Draw the numbers...
    // Define color and style for numbers

    // invalidate();
    Log.d(TAG, "onTouchEvent: x " + selX + ", y " + selY);
    return true;
}

public void setSelectedTile(String tile, String strorientations) {
    // TODO Auto-generated method stub
    Log.v("getting string in puzzle view ", tile);
    strgettile = tile;
    getorientation = strorientations;
    invalidate();
   }

}

1 个答案:

答案 0 :(得分:-1)

在onDraw结束时调用invalidate()。 此函数可以再次调用onDraw。