2012-05-25 55 views
0

我試圖創造感動text.I已裝箱面視圖和線程循環它。但是它沒有給我一個移動動作,但繪製每個文本後像* * * * *無限。Android的 - 移動文本問題

但我需要的是移動,這一點** - > **明白了嗎?

這是我的代碼

package com.CurvePackage.Curve; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.widget.Toast; 

public class Origin extends SurfaceView implements SurfaceHolder.Callback { 
    Context context1; 
    private MainThread thread; 
    private int x=0; 
    private int y=0; 


    public Origin(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
     setWillNotDraw(false); 
     context1 = context; 
     getHolder().addCallback(this); 
     thread = new MainThread(getHolder(), this); 

     setFocusable(true); 

    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     super.onDraw(canvas); 
     Paint paint = new Paint(); 
     paint.setTextSize(23); 
     paint.setFakeBoldText(true); 
     paint.setColor(Color.YELLOW); 
     // int score=(10-sprites.size()*100); 
     x=x+20; 
     y=y+20; 
     canvas.drawText("ewqewqe", x, y, paint); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     // TODO Auto-generated method stub 
     return super.onTouchEvent(event); 
     // Toast.makeText(getba, "Replay clicked!", Toast.LENGTH_SHORT).show(); 
    } 

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { 
     // TODO Auto-generated method stub 

    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 
     thread.setRunning(true); 
     thread.start(); 

    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     thread.setRunning(false); 
     // TODO Auto-generated method stub 
//  boolean retry = true; 
//  while (retry) { 
//   try { 
//    thread.join(); 
//    retry = false; 
//   } catch (InterruptedException e) { 
//    // try again shutting down the thread 
//   } 
//  } 

    } 
} 

主線

package com.CurvePackage.Curve; 

import android.graphics.Canvas; 
import android.view.SurfaceHolder; 

public class MainThread extends Thread { 

    // flag to hold game state 
    private boolean running; 
    private SurfaceHolder surfaceHolder; 
    private Origin origin; 
    static final long FPS = 15; 

    public void setRunning(boolean running) { 
     this.running = running; 
    } 

    @Override 
    public void run() { 

     long ticksPS = 1000/FPS; 
     long startTime; 
     long sleepTime; 

     while (running) { 
      Canvas c = null; 
      startTime = System.currentTimeMillis(); 
      try { 
        c = origin.getHolder().lockCanvas(); 
        synchronized (origin.getHolder()) { 
         origin.onDraw(c); 
        } 
      } finally { 
        if (c != null) { 
         origin.getHolder().unlockCanvasAndPost(c); 
        } 
      } 


      sleepTime = ticksPS-(System.currentTimeMillis() - startTime); 
      try { 
        if (sleepTime > 0) 
          sleep(sleepTime); 
        else 
          sleep(10); 
      } catch (Exception e) {} 
     } 

    } 

    public MainThread(SurfaceHolder surfaceHolder, Origin origin) { 
     super(); 
     this.surfaceHolder = surfaceHolder; 
     this.origin = origin; 
    } 

} 
+0

你想做什麼你想要將你的文字作爲標題或其他東西 –

+0

我需要看到文本移動那是所有 – FriendOnline

回答

0

您需要繪製整個背景在你的onDraw方法,你畫你的文本之前。

Paint p2 = new Paint(); 
p2.setStyle(Style.FILL); 
canvas.drawRect(0, 0, screenWidth, screenHeight, p2); 
//draw text here 

這將完全覆蓋上一次繪製畫布的文本,並將刪除此拖動效果。

+0

這是完美的解決方案嗎? – FriendOnline

+0

我試過了,但它不工作..文字不移動,但仍顯示在乞討? – FriendOnline

+0

這不是關於拖動,但它應該自動移動! – FriendOnline