Android 补间动画之旋转动画RotateAnimation

Android动画系列

博客导航:

         

1.介绍:

在接下来的不断地学习Android动画期间,会总结相关知识点。旋转动画是Android补间动画中的其中一种,本篇博客主要总结了旋转动画的属性、实现方式、相关方法和一个小案例的实现。

2.属性

 

duration动画执行的时间
pivotX旋转中心点的X轴坐标,50%。浮点数或是百分比,浮点数和百分比都是表示距离Object的左边缘,如5、5%。还有一种百分比是相对于父容器的左边缘,如5%p。一般50%表示正中心。
pivotY旋转中心点的Y轴坐标,50%。同上。
repeatCount重复次数,无数循环,可以设置为infinite或是-1,表示无限的。
repeatMode重复运行的模式,默认为restart,每次重复从头开始。可以设置为reverse,表示结束开始之后向前重复运行。
fromDegrees开始角度位置0。
toDegrees结束角度位置,正数表示顺时针,负数表示逆时针。
detachWallpaper表示是否在壁纸上运行
zAdjustment表示被animated的内容在运行时在z轴上的位置,默认为normal。normal保持内容当前的z轴顺序、top运行时在最顶层显示、bottom运行时在最底层显示

 

 

 

 

 

 

 

 

 

 

 

 

3.实现方式

3.1 xml方式

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="1000"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:fromDegrees="0"
        android:toDegrees="359"></rotate>
</rotate>

3.2 代码方式实现

Animation rotate = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        LinearInterpolator lin = new LinearInterpolator();
        rotate.setInterpolator(lin); //设置插值器
        rotate.setDuration(1000);//设置动画持续周期
        rotate.setRepeatCount(-1);//设置重复次数
        rotate.setFillAfter(true);//动画执行完后是否停留在执行完的状态

4.动画的监听事件

animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                //动画开始回调方法
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //动画结束
                rotate_img.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //动画重复执行
            }
        });

5.方法解释

setInterpolator设置动画的旋转速率:LinearInterpolator为匀速、Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果、默认为开始结束慢中间快。
setStartOffset表示执行start函数后等待开始运行的时间,单位为ms。

6.案例实现

6.1 Activity的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/home_id"
    android:background="#fff"
    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"
    tools:context="com.menglong.animatordemo.MainActivity">

    <Button
        android:id="@+id/rotate_but"
        android:text="旋转"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/rotate_img"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher"
        android:layout_width="200px"
        android:layout_height="300px" />

</RelativeLayout>

6.2 Activity代码实现

package com.menglong.animatordemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements View.OnClickListener {

    private ImageView rotate_img;
    private Animation animation;
    private Button rotate_but;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        rotate_img = (ImageView) findViewById(R.id.rotate_img);
        rotate_but = (Button) findViewById(R.id.rotate_but);
        rotate_but.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        //xml文件实现方式
        xml();
        //代码实现方式
//        code();
    }

    private void code() {
        Animation rotate = new RotateAnimation(0f, 359f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        LinearInterpolator lin = new LinearInterpolator();
        rotate.setInterpolator(lin); //设置插值器
        rotate.setDuration(1000);//设置动画持续周期
        rotate.setRepeatCount(-1);//设置重复次数
        rotate.setFillAfter(true);//动画执行完后是否停留在执行完的状态
//        rotate.setStartOffset(1000);//执行前的等待时间  单位ms
        rotate_img.setAnimation(rotate);
        rotate_img.startAnimation(rotate);
    }

    private void xml() {
        //xml文件动画执行
        animation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
        //setInterpolator设置动画的旋转速率:LinearInterpolator为匀速
        //另:Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果、默认为开始结束慢中间快。
        LinearInterpolator lin = new LinearInterpolator();
        animation.setInterpolator(lin);
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                //动画开始回调方法
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //动画结束
                rotate_img.clearAnimation();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                //动画重复执行
            }
        });
        rotate_img.startAnimation(animation);
    }
}

6.3 rotate_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="1000"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:fromDegrees="0"
        android:toDegrees="359"></rotate>
</rotate>

 

7.案例效果展示

                                                      

8.项目地址

https://github.com/SunMengLong/AnimatorDemo

 

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值