Android Animation 实现360度旋转效果

移动开发 码拜 9年前 (2015-09-29) 984次浏览

设有如下定义的动画。相关问题列在代码里。

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_test);
		layoutImg=(FrameLayout)findViewById(R.id.layoutimg);
		animtest=AnimationUtils.loadAnimation(this, R.anim.animtest);
		playMusic=(ImageButton)findViewById(R.id.playmusic);
		playMusic.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				boolean flag=animtest.hasStarted();	//hasStarted()自从layoutImg.startAnimation后就一直返回true
				Log.d(TAG, "play button clicked:"+flag);
				if(!flag&&layoutImg!=null){
					updateBtnView(true);
					layoutImg.startAnimation(animtest);
					Log.d(TAG, "play");
				}else{
					updateBtnView(false);
					/*Question:
					 * 1、clearAnimation可以停止动画,但不是暂停,也就是说动画会回到起点,而不是停在当前旋转的角度。
					 * 2、clearAnimation后,animtest.hasStarted()仍然返回的true,请问怎样才能让它返回false?
					 */
					layoutImg.clearAnimation();		
				/*
				 * 以下两者皆无效
				 */
				//	animtest.reset();
				//	animtest.cancel();
					Log.d(TAG, "pause");
				}
				
			}
			
		});
		
	}

然后,动画定义如下。toDegrees设置过不同值。每次转完一圈后,总会有些停顿,不能做得无缝的流畅的那种效果。

   <rotate
		android:fromDegrees="0"
		android:toDegrees="355"
		android:pivotX="50%"
		android:pivotY="50%"
		android:startOffset="0"
		android:repeatCount="-1"
		android:interpolator="@android:anim/linear_interpolator"
		android:duration="4000"/>
方案推荐指数:20
ObjectAnimator rot = ObjectAnimator.ofFloat(toggleButton, "rotation", 0, 359);
rot.setInterpolator(new LinearInterpolator());
rot.setDuration(4000);
rot.setRepeatCount(-1);
rot.start()

;

方案推荐指数:20
问题1:你确定有看它的api吗?有pause方法,它停止就是这个,不需要stop。

问题2:停止当前角度,你可以去看api,找符合你的方法。是滞启动,它的api也有提供isStart方法。

问题3:没用过,所以,代码给你了,你就要去看它的api。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Android Animation 实现360度旋转效果
喜欢 (0)
[1034331897@qq.com]
分享 (0)