Android动画-RotateAnimation

本文详细介绍了Android中的RotateAnimation,包括不同构造器的使用方法及常见属性的设置技巧。通过具体的Java代码和XML示例,展示了如何实现以不同点为中心的旋转动画效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、构造器说明

1、RotateAnimation(fromDegrees, toDegrees) [默认以View左上角顶点为旋转点]。
X轴顺时针转动到fromDegrees为旋转的起始点,
X轴顺时针转动到toDegrees为旋转的起始点。
如fromDegrees=0,toDegrees=90;为左上角顶点为旋转点。0度为起始点,90度为终点。进行旋转,旋转了90度
如fromDegrees=60,toDegrees=90;为左上角顶点为旋转点。60度为起始点,90度为终点。进行旋转,旋转了90-60=30度

2、RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
(pivotX,pivotY)为旋转点。pivotX为距离左侧的偏移量,pivotY为距离顶部的偏移量。即为相对于View左上角(0,0)的坐标点。
如View width=100px,height=100px
RotateAnimation(0,10,100,100);则以右下角顶点为旋转点,从原始位置顺时针旋转10度
RotateAnimation(0,90,50,50);则以View的中心点为旋转点,旋转90度

3、RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)
pivotXType, pivotXValue, pivotYType, pivotYValue 旋转点类型及其值。
Animation.ABSOLUTE为绝对值 其他为百分比。
如RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 按中心点旋转90度
效果和2例中的RotateAnimation(0,90,50,50);则以View的中心点为旋转点,旋转90度 。

二、常用属性说明

1、android:interpolator:这个属性是用来设置转动速率的。
LinearInterpolator为匀速效果,Accelerateinterpolator为加速效果、DecelerateInterpolator为减速效果

2、android:repeatCount 重复的次数,默认为0,必须是int,可以为-1表示不停止

3、android:duration属性表示从android:fromDegrees转动到android:toDegrees所花费的时间,单位为毫秒。可以用来计算速度。

4、android:startOffset 在调用start函数之后等待开始运行的时间,单位为毫秒,若为10,表示10ms后开始运行

5、android:repeatMode 重复的模式,默认为restart,即重头开始重新运行,可以为reverse即从结束开始向前重新运行。
在android:repeatCount大于0或为infinite时生效

6、android:detachWallpaper 表示是否在壁纸上运行

7、android:zAdjustment 表示被animated的内容在运行时在z轴上的位置,默认为normal。normal保持内容当前的z轴顺序top运行时在最顶层显示bottom运行时在最底层显示

8、rotateAnimation.setFillAfter(true);
ture表示动画结束后停留在动画的最后位置,false表示动画结束后回到初始位置,默认为false。

使用示例—代码中设置动画

Java代码

public class ParameterSettingActivity extends AppCompatActivity {

    private RotateAnimation rotateAnimation;
    private ImageView ivParamsetOneManual;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parameter_setting);

        initAnim();
        initView();
    }

    private void initAnim() {
        rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setInterpolator(new LinearInterpolator());//如果不添加这句,那么动画每个周期结束时会停顿一下
        rotateAnimation.setDuration(1000);
        rotateAnimation.setFillAfter(true);
        rotateAnimation.setRepeatCount(Animation.INFINITE);
        rotateAnimation.setRepeatMode(Animation.RESTART);
    }

    private void initView() {
        ivParamsetOneManual = (ImageView)findViewById(R.id.iv_paramset_one_manual);
        ivParamsetOneManual.setAnimation(rotateAnimation);
        rotateAnimation.start();

        //animation.cancel(); //结束动画

    }
}

xml代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/background_dark"
    tools:context="com.sdkj.heaterbluetooth.activity.ParameterSettingActivity">
    <android.support.percent.PercentRelativeLayout
        android:layout_below="@+id/header"
        android:id="@+id/rl_paramset_one_manual"
        android:layout_width="wrap_content"
        android:layout_height="72dp"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/iv_paramset_one_manual"
            style="@style/style_paramset_imageview2_background"/>
        <ImageView
            style="@style/style_paramset_imageview_background"
            android:text="一档"/>
    </android.support.percent.PercentRelativeLayout>

</RelativeLayout>

style.xml

<resources>
    <style name="style_paramset_imageview_background">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">0dp</item>
        <item name="layout_heightPercent">100%</item>
        <item name="layout_aspectRatio">100%</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textSize">10sp</item>
        <item name="android:background">@mipmap/bg_paramset_manual_text</item>
    </style>

    <style name="style_paramset_imageview2_background">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">0dp</item>
        <item name="layout_heightPercent">100%</item>
        <item name="layout_aspectRatio">100%</item>
        <item name="android:background">@mipmap/bg_paramset</item>
    </style>

</resources>

使用示例—xml中设置动画

java代码

public class ParameterSettingActivity extends AppCompatActivity {

    private RotateAnimation rotateAnimation;
    private ImageView ivParamsetOneManual;
    Animation rotate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_parameter_setting);

        initTitleBar();
        initAnim();
        initView();
    }

    private void initAnim() {
        rotate = AnimationUtils.loadAnimation(this, R.anim.anim_paramset_manual_rotate);
        LinearInterpolator lin = new LinearInterpolator();
        rotate.setInterpolator(lin);
    }

    private void initView() {
        ivParamsetOneManual = (ImageView)findViewById(R.id.iv_paramset_one_manual);
        ivParamsetOneManual.setAnimation(rotate);
        ivParamsetOneManual.startAnimation(rotate);

        //ivParamsetOneManual.clearAnimation();动画停止

    }
}

xml和style.xml文件不变

anim_paramset_manual_rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android" >
    <rotate
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360"
        android:duration = "1000"
        android:repeatCount = "-1">
    </rotate>
</rotate>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值