0
我該如何處理throw any object(Stone)
continuously
到moving Horizontally object
? 我已經使用了一個線程,可以使用翻譯動畫連續扔石頭,但內存使用量非常大,我的設備在3-4分鐘後變慢。如何解決dis問題?android如何處理動畫線程
我該如何處理throw any object(Stone)
continuously
到moving Horizontally object
? 我已經使用了一個線程,可以使用翻譯動畫連續扔石頭,但內存使用量非常大,我的設備在3-4分鐘後變慢。如何解決dis問題?android如何處理動畫線程
我認爲你最好實施自己的SurfaceView
。在其中,您可以繪製動畫對象(使用專用線程),比使用視圖動畫更加自由。 (您當然必須重寫代碼的一部分,但從長遠來看可能是最好的)。
如果你覺得你想試試SurfaceView
,我建議你通過android的Lunar Lander例子來看看。
實施例:
public class ThrowView extends SurfaceView implements SurfaceHolder.Callback, OnGestureListener{
//The OnGestureListener can detect a fling motion.
private class DrawingThread extends Thread{
private final float timefactor = 0.0001f; //This is used to make the animation a bit slower. Play with this value if the animation seems too be to slow or too fast.
private final float damp = 0.9; //This is used to slow down the object. If it stops too fast or slow, this is the value to change.
private float stoneX = 0; //The x-coordinate of our stone object. Use this when drawing.
private float stoneY = 0; //The y-coordinate of our stone object. Use this when drawing.
@Override
public void run(){
while(running){
Canvas c = null;
try{
c = surfaceHolder.lockCanvas(null);
synchronized (surfaceHolder) {
updatePhysics();
doDraw(c); //doDraw is not in this example, but it should essentially just draw our object at stoneX, stoneY.
}
}finally{
if(c!=null) surfaceHolder.unlockCanvasAndPost(c);
}
SystemClock.sleep(40);
}
private void updatePhysics(long time){
//Calculate how much time has passed since last step:
time1 = System.currentTimeMillis();
dT = (time1 - time2)*timefactor;
time2 = time1;
//Move the stone in x depending on the time and velocity:
stoneX += vX*dT;
//Decrease the velocity:
vX -= dT*damp:
}
}
protected volatile float vX = 0; //This is the x-speed. To be able to throw in y too, just make another variable for that.
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
//This is our onFling, here we take the velocity of the users fling and set our variable to that.
//Note that this value is based on pixels.
vX = velocityX;
return true;
}
@Override
public boolean onDown(MotionEvent e) {
//The onDown should return true so onFling can be activated:
return true;
}
}
這個例子是根據月球着陸樣品易於使用的製造。許多方法(在這個例子中不需要)在這裏省略,但可以根據Lunar Lander來實現。
我已經使用了表面視圖bu nw如何使用手勢onFling()來拋出該對象。你會提供一些例子...... – Geetanjali 2011-12-22 05:05:43
我添加了一個用於投擲的方法的例子。你可以在月球着陸器中看到如何製作的缺失部分,但是當然可以稍作修改。 :) – Jave 2011-12-22 09:37:21
我們可以做到這一點:「我想通過手勢監聽器在持續移動的目標上拋出onFling()對象」,所以我們可以在單個Surface View中執行這兩個操作......我們如何做到這一點? ???? – Geetanjali 2011-12-22 10:31:14