如何在物体绕其自身轴旋转的同时沿着路径移动它?

3

如何在沿着路径移动对象的同时绕其自身轴旋转,就像以下图像中所示:

enter image description here

虽然这就是我实际得到的:

enter image description here

这是我的代码:
AnimationSet as1 = new AnimationSet(true);
as1.setFillAfter(true);

float dist = 0.5f;
// The following is too slow just to inspect the animation
int duration = 5000; // 5 seconds
// Tried the following: RELATIVE_TO_SELF and RELATIVE_TO_PARENT but no difference
int ttype = Animation.RELATIVE_TO_SELF; // Type of translation
// Move to X: distance , Y: distance
TranslateAnimation ta1 = new TranslateAnimation( ttype,0,ttype,dist,ttype,0, ttype,dist); 
ta1.setDuration(duration);
// Add Translation to the set
as1.addAnimation(ta1);

// Rotate around its center
int rtype = Animation.RELATIVE_TO_SELF;
float rotation = 90;
RotateAnimation ra1 = new RotateAnimation(0, rotation,rtype,0.5f , rtype,0.5f );
ra1.setDuration(duration);
as1.addAnimation(ra1);

Object1.startAnimation(as1); // in my app Object1 is an ImageButton
2个回答

5
改变翻译和旋转动画的顺序解决了这个问题。

0
使用以下代码在旋转时移动对象。
AnimationSet set = new AnimationSet(true); set.setFillAfter(true);
    RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation.setInterpolator(new AccelerateInterpolator());
    rotateAnimation.setDuration(1000);

    TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, .85f,
            Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
    translateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());
    translateAnimation.setDuration(1000);

    set.addAnimation(rotateAnimation);
    set.addAnimation(translateAnimation);
    btnRoll.startAnimation(set);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接