图片围绕中心无限旋转动画

                                            图片无限旋转动画

      平时喜欢拿着手机去听歌,看到qq音乐中有一个图片绕着中心无限旋转 的动画,于是自己动手写了个,给大家分享下。

     1 xml布局

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.rotateanimate.MainActivity" >

    <Button
        android:id="@+id/btn_start"
        android:text="动画开始"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <ImageView
        android:id="@+id/iv_sharp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
         android:scaleType="center"
        android:src="@drawable/shape" />

</RelativeLayout>

 2 动画的配置

 

<?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="5000"  
        android:repeatCount="-1"  
        android:pivotX="50%"  
        android:pivotY="50%" />  
</set>  

 

 其中:repeatCount = -1  表示无限循环的意思

            privotX="50%"      表示旋转中心的X轴为相对于自身的50%,50%p 表示相对于屏幕X轴的50%

            privotY="50%"      表示旋转中心的Y轴相对于自身的50%,50%p 表示相对于屏幕Y轴的50%

            duration="5000"   表示动画的时间为5000毫秒

 

3 activity中的代码

package com.example.rotateanimate;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends ActionBarActivity {
	private ImageView ivShape;
	private Button btnBegin;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ivShape = (ImageView)findViewById(R.id.iv_sharp);
		btnBegin = (Button)findViewById(R.id.btn_start);
		final Animation animation = AnimationUtils.loadAnimation(this, R.anim.image_rotate_animate); 
		animation.setInterpolator(new LinearInterpolator());
		btnBegin.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				 ivShape.startAnimation(animation);
			}
		});
		
	}
}

 

    需要注意的是:

    在xml配置中不能设置旋转的速度,一定要在代码中加上

animation.setInterpolator(new LinearInterpolator());

   这段代码才能实现匀速旋转,否则会出现旋转一周后,停顿的情况。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值