2014-01-08 66 views
1

我想沿SurfaceView中的連續座標移動位圖圖像。我在繪製的位圖myBallSurfaceView座標(x1, y1)如下(部分代碼)(如何將位圖從一個座標移動到另一個座標 - Android開發

public class MainSurfaceView extends SurfaceView implements Runnable {... 
     ... 
     @Override 
     public void run() { 
      while (isRunning) { 
       if (!myHolder.getSurface().isValid()) 
        continue; 
       Canvas canvas;// Define canvas to paint on it 
       canvas = myHolder.lockCanvas(); 
       //Draw full screen rectangle to hold the floor map. 
       Rect dest = new Rect(0, 0, getWidth(), getHeight()); 
       Paint paint = new Paint(); 
       paint.setFilterBitmap(true); 
       canvas.drawBitmap(bgImage, null, dest, paint); 
       //This is the ball I want to move 
       canvas.drawBitmap(myBall, x1, y1, null); 

       myHolder.unlockCanvasAndPost(canvas); 
      } 

    } 

現在我想將它移動到(x2, y2)然後(x3, y3)和......多達必要和一前一後。我試圖使用TranslateAnimation,但無法做到這一點

+0

只是增加或減少(x1,y1)例如x1 = x1 + 5,y1 = y1 + 2 –

+0

對不起Pradeep我沒有明白你的意思。如果我改變x1和y1的值,圖像將以不同的座標繪製。我想要的是從一個點到另一個點將它製作成動畫。謝謝! – Ermi

回答

1

我已經想出瞭如何使用座標進行動畫製作。我將解釋什麼,我沒跳,這將是幫助他人: 首先,我保存的座標點對象

List<Point> point_list = new ArrayList<Point>(); 
point_list.add(new Point(x_value, y_value));//Add the x and y coordinates to the Point\ 

休假實現Runnable和使用的onDraw()方法,而不是run()方法如下:

public class MainSurfaceView extends SurfaceView {... 
    .... 
@Override 
protected void onDraw(Canvas canvas) { 
// Draw full screen rectangle to hold the floor map. 
Rect fArea = new Rect(0, 0, getWidth(), getHeight()); 
    Paint paint = new Paint(); 
paint.setFilterBitmap(true); 
// draw the paint on the rectangle with the floor map 
canvas.drawBitmap(bgImage, null, fArea, paint); 

// Get the coordinates of x & y as Point object 
List<Point> myPoints = point_list; 
// Start printing myBall on the floor (view) 
try { 
    if (index < myPoints.size()) { 
    // Increment the value of index and use it as index for the point_list 
    index++; 
} 
// Print myBall in each coordinates of x & y using the index 
canvas.drawBitmap(myBall, myPoints.get(index).x, myPoints.get(index).y, null); 
} catch (IndexOutOfBoundsException e) { 
     // TODO: handle exception 
} 
} 

我用try和catch來避免IndexOutOfBoundryExeption 乾杯!

0

我認爲this可能是你在做什麼,你想要做的是點綴位圖,有幾種方法可以做到這一點。還有ViewPropertyAnimator,它可以對整個視圖進行動畫處理。

相關問題