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

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

 

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

 

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

<?xml version="1.0" encoding="utf-8"?>
<!--
/*
**
** Copyright 2009, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License"); 
** you may not use this file except in compliance with the License. 
** You may obtain a copy of the License at 
**
**     http://www.apache.org/licenses/LICENSE-2.0 
**
** Unless required by applicable law or agreed to in writing, software 
** distributed under the License is distributed on an "AS IS" BASIS, 
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
** See the License for the specific language governing permissions and 
** limitations under the License.
*/
-->
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_black_76"
    android:pivotX="50%"
    android:pivotY="50%"
    android:framesCount="12"
    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

 

<?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

<?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代码中去实现:

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);    
     }     
 }   

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

 

 

但是,重复旋转的问题依然没有解决。我使用了Animation的setRepeatCount设置为循环,还是不能循环播放。最后,我就用了最无赖的招式,将度数与播放时间设置得很大很大,因为我的项目需要的动画时间是不会超过10秒的,所以这个问题也就迎刃而解了。

 

 

当然,如果能找到能自动循环播放的方法,一定要继续研究!

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值