Android旋转动画

简介: Android旋转动画核心方法public void startAnimation(Animation animation)执行动画,参数可以是各种动画的对象,Animation的多态,也可以是组合动画,后面会有。

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度
P


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

            }
        });
相关文章
|
1月前
|
Java Android开发 开发者
Android10 修改开发者选项中动画缩放默认值
Android10 修改开发者选项中动画缩放默认值
29 0
|
1月前
|
XML Java Android开发
android的三种动画
android的三种动画
20 0
|
5天前
|
Android开发 UED
Android Item平移动画
【6月更文挑战第18天】
|
1月前
|
数据库 Android开发
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
Android数据库框架-GreenDao入门,2024年最新flutter 页面跳转动画
|
1月前
|
Java Android开发
Android开发之使用OpenGL实现翻书动画
本文讲述了如何使用OpenGL实现更平滑、逼真的电子书翻页动画,以解决传统贝塞尔曲线方法存在的卡顿和阴影问题。作者分享了一个改造后的外国代码示例,提供了从前往后和从后往前的翻页效果动图。文章附带了`GlTurnActivity`的Java代码片段,展示如何加载和显示书籍图片。完整工程代码可在作者的GitHub找到:https://github.com/aqi00/note/tree/master/ExmOpenGL。
55 1
Android开发之使用OpenGL实现翻书动画
|
1月前
|
Java Android开发
Android 开机动画的启动
Android 开机动画的启动
30 0
|
1月前
|
Java Android开发
Android Mediatek 延迟停止启动动画和通知SurfaceFlinger(Android正在启动)
Android Mediatek 延迟停止启动动画和通知SurfaceFlinger(Android正在启动)
37 0
|
1月前
|
XML 开发工具 Android开发
Android动画效果-更新中
Android动画效果-更新中
69 1
|
1月前
|
API Android开发 开发者
【Android App】Vulkan实现宇宙中旋转雷达动画效果(附源码和原始视频 超详细必看)
【Android App】Vulkan实现宇宙中旋转雷达动画效果(附源码和原始视频 超详细必看)
116 1
|
1月前
|
XML Java Android开发
Android App开发动画特效之利用滚动器实现平滑翻页(附源码和演示 简单易懂)
Android App开发动画特效之利用滚动器实现平滑翻页(附源码和演示 简单易懂)
56 0
Android App开发动画特效之利用滚动器实现平滑翻页(附源码和演示 简单易懂)