2014-11-27 4 views
4

모션 뷰에서 캔버스에 텍스트를 그릴 수 있습니다. 문제는 그 때 텍스트를 그릴 때입니다. & 같은 캔버스에서 다음 그리기로 이동합니다. 그리기 텍스트가 사라집니다. 평균 화면 무효로 인해 다시 그리는거야 내 이전 무승부를 유지하고 동일한 캔버스에 새로운 무승부를 어떻게해야합니까?캔버스를 그리는 대신 캔버스 그리기 캔버스를 업데이트하는 방법

@Override 
    protected void onDraw(Canvas canvas) { 
    Paint hint = new Paint(); 
    path = new Path(); 
    mTextPaths = new ArrayList<Path>(); 
    Log.v("getting mtextpaths", mTextPaths.toString()); 

    int m; 
    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); 

      // canvas.drawText(characterToString, x, y, hint); 
      // canvas.drawText(characterToString, m 
      // * width + x, m * height + y, foreground); //its working in 
      // cross 
      // canvas.drawText(characterToString, x, m * height + y, 
      // foreground); //its working for vertical 
      // canvas.drawText(characterToString, m 
      // * width + x, y, foreground); //its working in horizontal 
      // setSelectedTile(tile); 
      if (getorientation.equalsIgnoreCase("Horizontal")) { 
       canvas.drawText(characterToString, m * width + positionX, 
         positionY, foreground); // for motion event 
       hint.setColor(Color.BLACK); 
       hint.setTextSize(45); 
       foreground.getTextPath(characterToString, 0, 
         characterToString.length(), positionX * 2/3, 
         positionY - 4, path); 

      } else { 
       canvas.drawText(characterToString, positionX, m * height 
         + positionY, foreground); 
       hint.setColor(Color.BLACK); 
       hint.setTextSize(45); 
       foreground.getTextPath(characterToString, 0, 
         characterToString.length(), positionX * 2/3, 
         positionY - 4, path); 


      } 


     } 

    } 

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


} 
+0

가능한 중복 (HTTP : // 유래 아래

코드 싹둑입니다 .com/questions/3733298/off-screen-drawing-in-android) – Torben

답변

-2
나는 모든 관련이없는 이벤트 핸들링이 지나치게 길고 끔찍 형식의 코드를 읽을려고하고 있지 않다

포함 ...하지만 일반적으로 당신이 그리는 사이에 캔버스 상태를 저장하려는 경우, 당신은 명시 적으로 할 필요가 . 비트 맵에

off screen drawing in android

그리기

구글 "화면 그리기의 안드로이드". 비트 맵을 캔버스에 그립니다. 반복하십시오.

+0

비트 맵을 사용할 수 있도록 이미지가 아닌 텍스트를 그려야하나요? @ Torben – Kirti

+0

오랜 코드에 대해 유감스럽게 생각합니다. 코드 @Torben을 업데이트했습니다. – Kirti

1
  1. invalidate() 메소드를 호출 할 때마다 캔버스가 생성됩니다. 그래서 저는 모든 텍스트와 위치를 유지하고 있습니다. 캔버스를 클릭 할 때마다 캔버스를 그리고 그 자리에 을 놓으십시오.
  2. 문제를 해결하는 방법 중 하나입니다. 캔버스는 비트 맵 입니다. 당신이 그리는 것은 무엇이든 영구적입니다. 을 처리하는 방법이 있지만 다시 대부분의 구현은 모든 패스에 전체 캔버스를 다시 그립니다.

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

모델 클래스는 다음과 같습니다 :

public class Kirti { 
float positionX; 
float positionY; 
String strgettile; 
String getorientation; 
float width; 
float height; 
public Kirti(float positionX, float positionY, String strgettile, 
     String getorientation, float width, float height) { 
    super(); 
    this.positionX = positionX; 
    this.positionY = positionY; 
    this.strgettile = strgettile; 
    this.getorientation = getorientation; 
    this.width = width; 
    this.height = height; 
} 
public float getPositionX() { 
    return positionX; 
} 
public void setPositionX(float positionX) { 
    this.positionX = positionX; 
} 
public float getPositionY() { 
    return positionY; 
} 
public void setPositionY(float positionY) { 
    this.positionY = positionY; 
} 
public String getStrgettile() { 
    return strgettile; 
} 
public void setStrgettile(String strgettile) { 
    this.strgettile = strgettile; 
} 
public String getGetorientation() { 
    return getorientation; 
} 
public void setGetorientation(String getorientation) { 
    this.getorientation = getorientation; 
} 
public float getWidth() { 
    return width; 
} 
public void setWidth(float width) { 
    this.width = width; 
} 
public float getHeight() { 
    return height; 
} 
public void setHeight(float height) { 
    this.height = height; 
}} 
[안드로이드에서 화면 오프 그리기]의
관련 문제