本文主要介绍Android中如何使用rotate实现图片不停旋转的效果。Android 平台提供了两类动画,一类是 Tween 动画,即通过对场景里的对象不断做图像变换(平移、缩放、旋转)产生动画效果;第二类是 Frame 动画,即顺序播放事先做好的图像,跟电影类似。本文分析 Tween动画的rotate实现旋转效果。



在新浪微博客户端中各个操作进行中时activity的右上角都会有个不停旋转的图标,类似刷新的效果,给用户以操作中的提示。这种非模态的提示方式推荐使用,那么下面就分享下如何实现这种效果吧

1、定义一个ImageView
定义一个ImageView是为了装载图片,其中的图片将被rotate用来进行旋转,其他View亦可。

资源文件为
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#fff"
tools:context=".TestDemoActivity" >

<ImageView
android:id="@+id/iv_ratate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/rotate"
/>

</LinearLayout>

2、定义rotate旋转效果

在res/anim文件夹下新建tip.xml文件,内容如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

<rotate
android:duration="5000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:toDegrees="359" />

</set>
含义表示从0到359度开始循环旋转,0-359(若设置成360在停止时会出现停顿现象)度旋转所用时间为500ms,旋转中心距离view的左顶点为50%距离,距离view的上边缘为50%距离,即正中心,具体每个含义见下面的具体属性介绍。

3.java代码
public class RotateActivity extends Activity {
private ImageView infoOperatingIV;
private Animation operatingAnim;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rotate);
infoOperatingIV = (ImageView) findViewById(R.id.iv_ratate);
operatingAnim = AnimationUtils.loadAnimation(this, R.anim.tip);
LinearInterpolator lin = new LinearInterpolator();
if (operatingAnim != null) {
infoOperatingIV.startAnimation(operatingAnim);
}
operatingAnim.setInterpolator(lin);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (operatingAnim != null && infoOperatingIV != null
&& operatingAnim.hasStarted()) {
infoOperatingIV.clearAnimation();
infoOperatingIV.startAnimation(operatingAnim);
}
}
}



关于其中的属性意义如下(红色部分加以注意):

android:fromDegrees 起始的角度度数

android:toDegrees 结束的角度度数,负数表示逆时针,正数表示顺时针。如10圈则比android:fromDegrees大3600即可

android:pivotX 旋转中心的X坐标

浮点数或是百分比。浮点数表示相对于Object的左边缘,如5; 百分比表示相对于Object的左边缘,如5%; 另一种百分比表示相对于父容器的左边缘,如5%p; 一般设置为50%表示在Object中心

android:pivotY 旋转中心的Y坐标

浮点数或是百分比。浮点数表示相对于Object的上边缘,如5; 百分比表示相对于Object的上边缘,如5%; 另一种百分比表示相对于父容器的上边缘,如5%p; 一般设置为50%表示在Object中心

android:duration 表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。

android:interpolator表示变化率,但不是运行速度。一个插补属性,可以将动画效果设置为加速,减速,反复,反弹等。默认为开始和结束慢中间快,

android:startOffset 在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行

android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止

android:repeatMode 重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行。在android:repeatCount大于0或为infinite时生效

android:detachWallpaper 表示是否在壁纸上运行

android:zAdjustment 表示被animated的内容在运行时在z轴上的位置,默认为normal。

normal保持内容当前的z轴顺序

top运行时在最顶层显示

bottom运行时在最底层显示
LinearInterpolator为匀速效果,Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果

更多相关文章

  1. Android UI控件之ListView实现圆角效果
  2. Android百分比下载进度条
  3. 《android 的四中动画效果》
  4. Android 自定义view的简单应用(2) 毛玻璃效果
  5. 一起学android之给图片添加水印效果(33)
  6. 【Android】实现打开关闭效果
  7. Android 仿订单出票效果 (附DEMO)
  8. android动画效果演示

随机推荐

  1. c语言二进制怎么转换十进制
  2. c语言中不等于怎么表示?
  3. c语言中long是什么意思?
  4. c语言中=和==的区别是什么?
  5. c语言关键字是什么
  6. c语言源程序文件的后缀是什么?
  7. i++和++i的区别及举例说明
  8. C#中DataSet的用法
  9. c语言注释符号是什么?
  10. c语言字符串定义的方法是什么