关于Android中的Rotate旋转动画应用

引用:http://rainbowsu.iteye.com/blog/766608

开始Android开发工作不久,工作所需,项目需要做一个单帧动画旋转的功能。于是上网找了很多资料都不是我所需要的,要么就是乱转,要么就是我能力问题无法掌握的Matrix方法。

 

我所需要的动画是:单帧图片,以其中心为旋转点,循环匀速旋转,并且作为ImageView显示于某Activity中。

 

刚开始的时候我想到的办法是能不能参考Android自身的ProgressBar里的圆圈旋转方式来写,于是找到了frameworks/base/core/res/res/drawable中的progress_large.xml,发现里面有这么一段代码:

Xml代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--   
  3. /*   
  4. **   
  5. ** Copyright 2009, The Android Open Source Project   
  6. **   
  7. ** Licensed under the Apache License, Version 2.0 (the "License");    
  8. ** you may not use this file except in compliance with the License.    
  9. ** You may obtain a copy of the License at    
  10. **   
  11. **     http://www.apache.org/licenses/LICENSE-2.0    
  12. **   
  13. ** Unless required by applicable law or agreed to in writing, software    
  14. ** distributed under the License is distributed on an "AS IS" BASIS,    
  15. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.    
  16. ** See the License for the specific language governing permissions and    
  17. ** limitations under the License.   
  18. */   
  19. -->  
  20. <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"  
  21.     android:drawable="@drawable/spinner_black_76"  
  22.     android:pivotX="50%"  
  23.     android:pivotY="50%"  
  24.     android:framesCount="12"  
  25.     android:frameDuration="100" />  

 

当我模仿着编写的时候,却发现android:framesCount与android:frameDuratiion是frameworks内部的属性,不对外使用。于是我只能继续寻找其他办法。

 

我接下来想到的办法就是能不能通过修改ProgressBar的图片资源来直接显示ProgressBar从而实现我所需要的功能,这里可以参考这个资料:

http://phenom.iteye.com/blog/728892

 

开始的时候没注意到有setProgress与setSecondProgress的设置,也就是我没有注意到这是进度条类型的ProgressBar而不是我所需要的那个自动转圈的ProgressBar,于是又只能放弃。

 

接下来我想到了使用Android中的Matrix类,来实现旋转功能,但是怪我自己数学没学好,没能理解这个矩阵的意思。无论我怎么设置,图片都只会从一个很怪的角度去旋转。

 

 

所幸的是,最后我还是想回到了Android自身的Animation动画功能,于是继续搜索学习。这个时候就发现了可以直接通过写anim中的动画来实现我所说的功能,这里参考了资料:

http://www.iteye.com/topic/483737

http://wenku.baidu.com/view/a4e16d66f5335a8102d220cb.html

 

Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">   
  3. <rotate   
  4.     android:fromDegrees="0"      
  5.     android:toDegrees="+360"     
  6.     android:duration="1500"    
  7.     android:pivotX="50%"    
  8.     android:pivotY="50%"    
  9.     />   
  10. </set>  
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
	android:fromDegrees="0"   
	android:toDegrees="+360"  
	android:duration="1500" 
	android:pivotX="50%" 
	android:pivotY="50%" 
	/>
</set>

 

(这是我自己参照资料写下的动画XML,以图片的中点为旋转点,顺时针旋转360度,转着360度需要1.5秒时间)

 

可是问题又来了,这样写出来的动画图片只会转1圈就自己停了,于是我傻傻地将度数与时间都翻了几翻也就是将android:toDegrees与android:duration的数值都调大,这时的XML代码为:

anim/my_anim.xml

Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">   
  3. <rotate   
  4.     android:fromDegrees="0"      
  5.     android:toDegrees="+3600"     
  6.     android:duration="15000"    
  7.     android:pivotX="50%"    
  8.     android:pivotY="50%"    
  9.     />   
  10. </set>  
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
	android:fromDegrees="0"   
	android:toDegrees="+3600"  
	android:duration="15000" 
	android:pivotX="50%" 
	android:pivotY="50%" 
	/>
</set>

 
这样的话,图片的确是会自动旋转10次了,可是问题又来了:开始旋转的时候,图片处于加速状态,然后越转越快,最后到快要结束的时候又开始变满……我暗想这Android的动画设置也太TMD“人性化”了!于是我想起了刚才参考的资料里面有一个关于“插入动画”的属性android:interpolator,这个属性可以设置动画的插入方式为“加速、减速、先加后减”,可就是没有匀速。

 

这个时候我查到了Android中有这么一个类LinearInerpolator:这个类是Inerpolator的子类,是实现匀速动画的关键。但是不能通过XML中的那个android:interpolator属性去设置,必须在java代码中去实现:

Java代码 复制代码  收藏代码
  1. import android.app.Activity;        
  2. import android.os.Bundle;         
  3. import android.view.animation.Animation;        
  4. import android.view.animation.AnimationUtils;           
  5. public class RotateAnimation extends Activity{        
  6.     public void onCreate(Bundle savedInstanceState) {        
  7.         super.onCreate(savedInstanceState);        
  8.          setContentView(R.layout.main);        
  9.          Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate);        
  10.          <SPAN style="COLOR: #ff0000">LinearInterpolator lir = new LinearInterpolator();   
  11.     anim.setInterpolator(lir);</SPAN>   
  12.           findViewById(R.id.ImageViewTest).startAnimation(anim);       
  13.      }        
  14.  }     
import android.app.Activity;     
import android.os.Bundle;      
import android.view.animation.Animation;     
import android.view.animation.AnimationUtils;        
public class RotateAnimation extends Activity{     
    public void onCreate(Bundle savedInstanceState) {     
        super.onCreate(savedInstanceState);     
         setContentView(R.layout.main);     
         Animation anim = AnimationUtils.loadAnimation(this, R.anim.my_rotate);     
         LinearInterpolator lir = new LinearInterpolator();
	anim.setInterpolator(lir);
          findViewById(R.id.ImageViewTest).startAnimation(anim);    
     }     
 }   

 这样,图片就可以匀速旋转了

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值