0

我的应用程序中有一个图像...当我单击开始按钮时,图像需要旋转..当我单击停止按钮时,图像设置为原始位置...相反,我希望图像准确地停止位置..这是我的代码

public class MainActivity extends Activity {

    ImageView my_image;
    AnimationListener listener;
@Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    listener = new AnimationListener() {
        @Override public void onAnimationStart(Animation animation) {}
        @Override public void onAnimationRepeat(Animation animation) {}
        @Override
        public void onAnimationEnd(Animation animation) {
            System.out.println("End Animation!");
            //load_animations();
        }
    };

    my_image=(ImageView)findViewById(R.id.imageView1);
    load_animations();
    Button start=(Button)findViewById(R.id.button1);
    Button stop=(Button)findViewById(R.id.button2);


}
void load_animations()
{
    Button start=(Button)findViewById(R.id.button1);
    Button stop=(Button)findViewById(R.id.button2);

    new AnimationUtils();
    final Animation rotation = AnimationUtils.loadAnimation(this, R.anim.animation);
    rotation.setAnimationListener(listener);
    start.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
             my_image.startAnimation(rotation);
        }
    });
    stop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        my_image.clearAnimation();  

        }
    });

}

}
4

2 回答 2

0

当您需要将图像停止在确切位置时。改用 ObjectAnimator...这支持 API 11

ObjectAnimator animator = ObjectAnimator.ofFloat(my_image,"rotationY", 360f);
animator.setDuration(1000);
animator.setRepeatCount(ObjectAnimator.INFINITE);

在开始按钮中单击..

animator.start();

在停止按钮中单击..

animator.cancel();
于 2013-10-01T05:50:32.807 回答
0

在 load_animations() 方法中使用以下代码:

final Animation rotation = AnimationUtils.loadAnimation(this, R.anim.animation);
rotation.setDuration(10000000);
rotation.setAnimationListener(listener);
于 2013-10-01T05:52:44.803 回答