Android - 如何获取animatorSet的旋转度

时间:2014-07-01 22:17:47

标签: java android rotation android-animation

我有一艘太空船ImageView我正在借助以下AnimatorSet旋转:

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="sequentially" >

<objectAnimator
    android:duration="5000"
    android:propertyName="rotation"
    android:repeatCount="infinite"
    android:repeatMode="restart"
    android:valueTo="-360"
    android:valueType="floatType" 
    android:interpolator="@android:anim/linear_interpolator" />

为了开始和结束动画我正在使用这个OnTouchListener

turnShipLeft_btn.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
                spaceShip_setLeft.start();
            } else if(arg1.getAction() == MotionEvent.ACTION_UP){
                spaceShip_setLeft.cancel();
            }
            return true;
        }
    });

执行spaceShip_setLeft.cancel();后,我想做两件事:

  1. 将停止的确切旋转度存储在局部变量中。
  2. 在下一个AnimatorSet android:valueFrom=""设置我的旋转度变量,以便从spaceShip_setLeft动画结束的确切位置开始动画。
  3. 我现在已经很久了。任何帮助将非常感谢!

    PS。如果你可以包含你自己的代码片段,那就太棒了!

1 个答案:

答案 0 :(得分:0)

如果要使用动态值,请不要使用XML。

为简单起见,我在默认的Android应用程序项目(最新的具有该片段的项目)中编写了我自己的小型演示。您应该能够根据需要适应您的代码。

public static class PlaceholderFragment extends Fragment {
    Button btn_A;
    Button btn_B;
    TextView hello;
    ObjectAnimator mAnimation;
    AnimatorSet transSet;
    Float valuefrom=0f;
    Float valueto=-360f;

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        btn_A= (Button) rootView.findViewById(R.id.button1);
        btn_B= (Button) rootView.findViewById(R.id.button2);
        hello= (TextView) rootView.findViewById(R.id.textView1);

        btn_A.setOnClickListener(new OnClickListener() 
        {         
            public void onClick(View v) 
            {
                if(!transSet.isRunning()){
                    transSet.start();
                }
            }
        });

        btn_B.setOnClickListener(new OnClickListener() 
        {         
            public void onClick(View v) 
            {
                Float save = (Float) mAnimation.getAnimatedValue();
                Log.d("TAG", "Value" +save);
                valuefrom= save;
                valueto= save-360;
                transSet.cancel();
                //re-init the animation
                doObjectAnimator();
            }
        });

        //init the animation
        doObjectAnimator();
        return rootView;
    }

    public void doObjectAnimator(){
        mAnimation= ObjectAnimator.ofFloat(hello, "rotation", valuefrom, valueto);
        mAnimation.setInterpolator(new LinearInterpolator());
        mAnimation.setDuration(5000);
        mAnimation.setRepeatCount(Animation.INFINITE);
        mAnimation.setRepeatMode(Animation.RESTART);
        //define your other animations here and add them to the set like: set.play(anim1).before(anim2);
        transSet= new AnimatorSet();
        transSet.play(mAnimation);

    }
}