Android实现简单旋转动画

 更新时间:2022年07月20日 10:02:45   作者:Android-kongqw  
这篇文章主要为大家详细介绍了Android实现简单旋转动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现简单旋转动画的具体代码,供大家参考,具体内容如下

核心方法

public void startAnimation(Animation animation)

执行动画,参数可以是各种动画的对象,Animation的多态,也可以是组合动画,后面会有。

2个参数的构造方法

/**
 * Constructor to use when building a RotateAnimation from code.
 * Default pivotX/pivotY point is (0,0).
 * 
 * @param fromDegrees Rotation offset to apply at the start of the animation.
 * @param toDegrees Rotation offset to apply at the end of the animation.
 */
public RotateAnimation(float fromDegrees, float toDegrees) {
    mFromDegrees = fromDegrees;
    mToDegrees = toDegrees;
    mPivotX = 0.0f;
    mPivotY = 0.0f;
}
  • 第一个参数是图片旋转的起始度数
  • 第二个参数是图片旋转结束的度数

用法

RotateAnimation ta = new RotateAnimation(0, 360);
// 设置动画播放的时间
ta.setDuration(1000);
// 开始播放动画
iv.startAnimation(ta);

效果

以图片左上角为旋转中心,顺时针旋转360度

4个参数的构造方法

/**
     * Constructor to use when building a RotateAnimation from code
     * 
     * @param fromDegrees Rotation offset to apply at the start of the animation.
     * @param toDegrees Rotation offset to apply at the end of the animation.
     * @param pivotX The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge.
     * @param pivotY The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge.
     */
    public RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) {
        mFromDegrees = fromDegrees;
        mToDegrees = toDegrees;

        mPivotXType = ABSOLUTE;
        mPivotYType = ABSOLUTE;
        mPivotXValue = pivotX;
        mPivotYValue = pivotY;
        initializePivotPoint();
    }
  • 头两个参数和上面两个参数的构造方法一样,是开始和结束的角度
  • 后两个参数是设置图片的旋转中心

用法

RotateAnimation ta = new RotateAnimation(0, 360, iv.getWidth() / 2, iv.getHeight() / 2);
// 设置动画播放的时间
ta.setDuration(1000);
// 开始播放动画
iv.startAnimation(ta);

效果

以图片中心为旋转中心,顺时针旋转360度

6个参数的构造方法

/**
* Constructor to use when building a RotateAnimation from code
* 
* @param fromDegrees Rotation offset to apply at the start of the animation.
* @param toDegrees Rotation offset to apply at the end of the animation.
* @param pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
* @param pivotXValue The X coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the left edge. This value can either be an absolute number if pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
* @param pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.
* @param pivotYValue The Y coordinate of the point about which the object is being rotated, specified as an absolute number where 0 is the top edge. This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
*/
public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {
   mFromDegrees = fromDegrees;
   mToDegrees = toDegrees;

   mPivotXValue = pivotXValue;
   mPivotXType = pivotXType;
   mPivotYValue = pivotYValue;
   mPivotYType = pivotYType;
   initializePivotPoint();
}

比4个参数的构造方法多了第三个和第五个参数,其他用法一样,第三个和四五个参数分别设置第四个和第六个参数的类型,四个参数的构造没有设置,是默认设置了Animation.ABSOLUTE类型

用法

// 创建旋转的动画对象
RotateAnimation ta = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
// 设置动画播放的时间
ta.setDuration(1000);
// 开始播放动画
iv.startAnimation(ta);

效果

和上面一样,以图片中心为旋转中心,顺时针旋转360度。

设置动画重复播放的次数的方法

/**
 * Sets how many times the animation should be repeated. If the repeat
 * count is 0, the animation is never repeated. If the repeat count is
 * greater than 0 or {@link #INFINITE}, the repeat mode will be taken
 * into account. The repeat count is 0 by default.
 *
 * @param repeatCount the number of times the animation should be repeated
 * @attr ref android.R.styleable#Animation_repeatCount
 */
public void setRepeatCount(int repeatCount) {
    if (repeatCount < 0) {
        repeatCount = INFINITE;
    }
    mRepeatCount = repeatCount;
}

使用

sa.setRepeatCount(2);

一直重复

sa.setRepeatCount(Animation.INFINITE);

设置动画重复播放的模式的方法

/**
 * Defines what this animation should do when it reaches the end. This
 * setting is applied only when the repeat count is either greater than
 * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. 
 *
 * @param repeatMode {@link #RESTART} or {@link #REVERSE}
 * @attr ref android.R.styleable#Animation_repeatMode
 */
public void setRepeatMode(int repeatMode) {
    mRepeatMode = repeatMode;
}

使用

sa.setRepeatMode(ScaleAnimation.REVERSE);

动画的监听

rotateAnimation.setAnimationListener(new AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                // TODO Auto-generated method stub

            }
 });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android中PathMeasure仿支付宝支付动画

    Android中PathMeasure仿支付宝支付动画

    这篇文章主要为大家详细介绍了Android中PathMeasure仿支付宝支付动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • RecyclerView自定义分割线

    RecyclerView自定义分割线

    这篇文章主要为大家详细介绍了RecyclerView自定义分割线的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • Android getViewById和getLayoutInflater().inflate()的详解及比较

    Android getViewById和getLayoutInflater().inflate()的详解及比较

    这篇文章主要介绍了Android getViewById和getLayoutInflater().inflate()的详解及比较的相关资料,这里对这两种方法进行了详细的对比,对于开始学习Android的朋友使用这两种方法是个很好的资料,需要的朋友可以参考下
    2016-11-11
  • Android中Root权限获取的简单代码

    Android中Root权限获取的简单代码

    那么我们在Android开发中如何获取Android的Root权限呢?下面是主要的简单代码。
    2013-06-06
  • Kotlin如何直接使用控件ID原理详析

    Kotlin如何直接使用控件ID原理详析

    这篇文章主要给大家介绍了关于Kotlin如何直接使用控件ID原理的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-12-12
  • 详解Android App卸载后跳转到指定的反馈页面的方法

    详解Android App卸载后跳转到指定的反馈页面的方法

    这篇文章主要介绍了Android App卸载后跳转到指定的反馈页面的方法,关键点是相关线程要判断在目录被消除以前作出响应,需要的朋友可以参考下
    2016-04-04
  • Android控件PullRefreshViewGroup实现下拉刷新和上拉加载

    Android控件PullRefreshViewGroup实现下拉刷新和上拉加载

    这篇文章主要为大家详细介绍了Android控件PullRefreshViewGroup实现下拉刷新和上拉加载效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • 获取Android设备电池电量状态

    获取Android设备电池电量状态

    本文介绍了在Android系统中获取设备电池电量状态的方法,包括使用BatteryManager类获取电量百分比、电池状态和健康状况,以及通过注册广播接收器实时获取电量状态变化。了解这些方法可以帮助用户更好地管理设备的使用,避免因电量不足而影响使用体验。
    2023-03-03
  • android自定义组件实现仪表计数盘

    android自定义组件实现仪表计数盘

    这篇文章主要为大家详细介绍了android自定义组件实现仪表计数盘,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • Android通讯录开发之删除功能的实现方法

    Android通讯录开发之删除功能的实现方法

    这篇文章主要介绍了Android通讯录开发之删除功能的实现方法,有需要的朋友可以参考一下
    2014-01-01

最新评论