2011-12-02 44 views

回答

8

你使用的是什麼Android版本?由於API級別11,您可以使用自定義Animators,這可以輕鬆實現您的曲線翻譯。

如果使用低於一個版本有AFAIK僅使用翻譯動畫和設置動畫聽衆

EDIT手動連接多個線性平移的可能性:

實施例:

View view; 
animator = ValueAnimator.ofFloat(0, 1); // values from 0 to 1 
animator.setDuration(5000); // 5 seconds duration from 0 to 1 
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() 
{ 
    @Override 
    public void onAnimationUpdate(ValueAnimator animation) { 
     float value = ((Float) (animation.getAnimatedValue())) 
        .floatValue(); 
     // Set translation of your view here. Position can be calculated 
     // out of value. This code should move the view in a half circle. 
     view.setTranslationX((float)(200.0 * Math.sin(value*Math.PI))); 
     view.setTranslationY((float)(200.0 * Math.cos(value*Math.PI))); 
    } 
}); 

我希望它能起作用。剛剛複製&粘貼(並縮短和更改)我的應用程序的代碼。

+0

請提供一些示例代碼,用於在android 3.0中準備曲線翻譯。 – user884126

+0

你在那裏。我希望它的作品 –

+0

@ js-先生,我可以實現一個彎曲的動畫api水平低於11使用九老android庫? –

-1

請考慮以下網頁鏈接。這是一個用C語言編寫的遊戲。你需要隔離projectile()函數,並試着理解在其中定義的變量。一旦你嘗試在自己的代碼中實現它。

http://www.daniweb.com/software-development/c/code/216266

+2

雖然這可能在理論上回答這個問題,但您最好(http://meta.stackexchange.com/q/8259)爲您編輯答案以包含解決方案的基本部分,並提供供參考的鏈接。 –

3

下面是我用的是動畫師:

目的:將沿路徑 「路徑」

的Android V21 +查看 「查看」:

// Animates view changing x, y along path co-ordinates 
ValueAnimator pathAnimator = ObjectAnimator.ofFloat(view, "x", "y", path) 

的Android V11 +:

// Animates a float value from 0 to 1 
ValueAnimator pathAnimator = ValueAnimator.ofFloat(0.0f, 1.0f); 

// This listener onAnimationUpdate will be called during every step in the animation 
// Gets called every millisecond in my observation 
pathAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 

float[] point = new float[2]; 

@Override 
    public void onAnimationUpdate(ValueAnimator animation) { 
     // Gets the animated float fraction 
     float val = animation.getAnimatedFraction(); 

     // Gets the point at the fractional path length 
     PathMeasure pathMeasure = new PathMeasure(path, true); 
     pathMeasure.getPosTan(pathMeasure.getLength() * val, point, null); 

     // Sets view location to the above point 
     view.setX(point[0]); 
     view.setY(point[1]); 
    } 
}); 

類似的:Android, move bitmap along a path?

+1

花更多的時間解釋你的答案是如何工作是很有幫助的,所以提問者可以更容易地遵循你的代碼。 – SuperBiasedMan

+1

@SuperBiasedMan感謝您的反饋!在評論中添加了解釋。 –

+0

我不得不這樣做:'pathMeasure.getLength()/ 2',因爲視圖一直回到它原來的位置。 – Rick