2014-11-25 27 views
1

我能畫上鏡頭來看畫布上的文字現在的問題是,當我繪製文本&去上相同的畫布我的畫文本越來越消失下一期我的意思是屏幕越來越重畫,因爲無效我想要保持我以前的平局,並在同一個畫布上畫新畫,我該怎麼做?以前如何在同一個畫布上繪製?

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

}

回答

0

呼叫invalidate()在的onDraw結束。 該函數讓onDraw再次被調用。

+0

我應該在哪裏保持invalidate()使用setSelectedTile方法,我應該從那裏刪除這個方法? – Kirti 2014-11-25 08:11:05

+0

@Override \t保護無效的onDraw(帆布帆布){ \t \t \t \t \t \t \t無效(); \t \t} \t \t \t}將我像這樣使用 – Kirti 2014-11-25 08:12:39

+0

我無法找到你所談論的方法,反正把無效在最上的OnDraw的最後,你畫的一切,並最終更新你的變量之後。這將創建一個onDraw調用的循環 – Cloud57 2014-11-25 08:17:10