? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 图片无限旋转动画
? ? ? 平时喜欢拿着手机去听歌,看到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配置中不能设置旋转的速度,一定要在代码中加上红色的打断代码这段代码才能实现匀速旋转,否则会出现旋转一周后,停顿的情况。
?
原文:http://1029457926.iteye.com/blog/2193330