Android图片匀速旋转

本文是在我的博客android图片处理,让图片变成圆形 的基础上继续写的,可以去看看,直接看也没关系,也能看懂


1,首先在res文件夹下创建一个名字为anim的文件夹,名字不要写错

2,在anim里面创建一个xlm文件:img_animation.xml,这个名字随便写都可以,注意不要大写,里面的代码如下:


  1. <?xml version=“1.0” encoding=“utf-8”?>  
  2. <set xmlns:android=“http://schemas.android.com/apk/res/android” >  
  3.   
  4.     <rotate  
  5.         android:duration=“5000”  
  6.         android:fromDegrees=“0”  
  7.         android:pivotX=“50%”  
  8.         android:pivotY=“50%”  
  9.         android:repeatCount=“-1”  
  10.         android:repeatMode=“restart”  
  11.         android:toDegrees=“360” />  
  12.   
  13. </set>  
<?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:repeatMode="restart"
        android:toDegrees="360" />

</set>

具体含义是:

  1. duration:时间</span>  
duration:时间</span>
  1. fromDegrees=“0”:  从几度开始转</span>  
fromDegrees="0":  从几度开始转</span>
  1. toDegrees=“360” : 旋转多少度</span>  
toDegrees="360" : 旋转多少度</span>
  1. pivotX=”50%        旋转中心距离view的左顶点为50%距离,  
pivotX="50%        旋转中心距离view的左顶点为50%距离,
  1. pivotY=”50%       距离view的上边缘为50%距离  
pivotY="50%       距离view的上边缘为50%距离
  1. repeatCount=“-1”:重复次数,-1为一直重复  
repeatCount="-1":重复次数,-1为一直重复
  1. repeatMode=“restart”:重复模式,restart从头开始重复  
repeatMode="restart":重复模式,restart从头开始重复

 
 
 布局文件代码没变,依旧是:放一个控件就行了 
 
 
 
  1. </  
  2.     xmlns:toolsxmlns:tools=“http://schemas.android.com/tools”  
  3.     android:layout_width=“match_parent”  
  4.     android:layout_height=“match_parent”  
  5.     android:background=“#ff00ff”  
  6.      >  
  7.   
  8. <com.example.circleimageview.CircleImageView   
  9.     android:id=“@+id/imageview”  
  10.     android:layout_width=“100dp”  
  11.     android:layout_height=“100dp”  
  12.     android:layout_centerInParent=“true”  
  13.     android:src=“@drawable/control_image”  
  14.     />  
  15. </RelativeLayout>  
</
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff00ff"
     >

<com.example.circleimageview.CircleImageView 
    android:id="@+id/imageview"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_centerInParent="true"
    android:src="@drawable/control_image"
    />
</RelativeLayout>
你也可以写成一个普通的控件都可以实现旋转
 
 
  1. <span style=“font-family: Arial, Helvetica, sans-serif;”>package com.example.circleimageview;</span>  
<span style="font-family: Arial, Helvetica, sans-serif;">package com.example.circleimageview;</span>
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.animation.Animation;  
  4. import android.view.animation.AnimationUtils;  
  5. import android.view.animation.LinearInterpolator;  
  6. import android.widget.ImageView;  
  7.   
  8. public class MainActivity extends Activity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.           
  15.         ImageView imageView = (ImageView) findViewById(R.id.imageview);  
  16.          //动画  
  17.          Animation animation = AnimationUtils.loadAnimation(this, R.anim.img_animation);  
  18.         LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动  
  19.         animation.setInterpolator(lin);  
  20.         imageView.startAnimation(animation);  
  21.     }  
  22.   
  23. }  
import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;

public class MainActivity extends Activity {

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

        ImageView imageView = (ImageView) findViewById(R.id.imageview);
         //动画
         Animation animation = AnimationUtils.loadAnimation(this, R.anim.img_animation);
        LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
        animation.setInterpolator(lin);
        imageView.startAnimation(animation);
    }

}
  1.   

是不是很简单,运行效果如下:录制的有点问题,实际上是匀速地

 
 
 
 



demo地址:http://download.csdn.net/detail/dl10210950/9600216


转自段炼Android的博客:http://blog.csdn.net/dl10210950/article/details/52175873

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值