Android实现旋转动画

 更新时间:2022年01月06日 11:12:54   作者:吃橘子的季节呢  
这篇文章主要为大家详细介绍了Android实现旋转动画,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

旋转动画(可加速、减速)

1、准备工作

首先需要有一个用于旋转的图片

需要考虑如何开始、结束、加速、减速

2、加速减速原理

本次的动画采用RotateAnimation,初始化需要的参数如下

public RotateAnimation(float fromDegrees,float toDegrees,int pivotXType,float pivotXValue,int pivotYType, float pivotYValue) {
    mFromDegrees = fromDegrees;//开始角度
    mToDegrees = toDegrees;//结束角度
    mPivotXValue = pivotXValue;//确定x轴坐标的类型
    mPivotXType = pivotXType;//x轴的值,0.5f表明是以自身这个控件的一半长度为x轴
    mPivotYValue = pivotYValue;//确定y轴坐标的类型
    mPivotYType = pivotYType;//y轴的值,0.5f表明是以自身这个控件的一半长度为y轴
    initializePivotPoint();
}

所谓旋转动画,在本质上就是在如上的对象初始化之后,规定在一定的周期内旋转

所谓加速,本质上就是在设定好的周期内变换旋转角度

或者修改周期,在预设周期内旋转一定角度

总之,角度和周期一定会变化一个,就可以决定动画的快慢。

如: 从 2秒内旋转360度1秒内旋转360度 就是一种加速,从 2秒内旋转360度 到 2秒内旋转720度 也是一种加速。

​ 反之就是减速。

3、初始化

RotateAnimation rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setInterpolator(new LinearInterpolator());
        rotate.setDuration(2000);//设置动画持续周期
        rotate.setRepeatCount(-1);//设置重复次数
//        rotate.setFillAfter(true);//动画执行完后是否停留在执行完的状态
        rotate.setStartOffset(10);//执行前的等待时间

4、开始

start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fan.startAnimation(rotate);
            }
        });

5、加速

首先需要创建全局变量

private int duration=2000;

加速样例

accelerate.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                if (duration>10){
                    duration/=2;    //周期除2角度不变加速(需要考虑极端,所以加一个判断)
                }
                rotate.setDuration(duration);    //设置周期
                fan.startAnimation(rotate);        //开始旋转
            }
        });

6、减速

decelerate.setOnClickListener(new View.OnClickListener() {
    @Override

    public void onClick(View v) {
        if (duration<10000){
            duration*=2;    //周期乘2角度不变减速(需要考虑极端,所以加一个判断)
        }
        rotate.setDuration(duration);    //设置周期
        fan.startAnimation(rotate);        //开始旋转
    }
});

7、停止

stop.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        fan.clearAnimation();    //停止
    }
});

8、项目源码

Layout部分

<?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="#DEECFA"
    tools:context=".MainActivity">


    <RelativeLayout
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:id="@+id/relativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/border"
        tools:layout_editor_absoluteX="566dp"
        tools:layout_editor_absoluteY="132dp">
        <ImageView
            android:id="@+id/fan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/fan"
            tools:layout_editor_absoluteX="552dp"
            tools:layout_editor_absoluteY="122dp" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="150dp">
        <Button
            android:id="@+id/start"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@mipmap/border"
            android:text="开始"
            tools:layout_editor_absoluteX="525dp"
            tools:layout_editor_absoluteY="596dp" />
        <Button
            android:id="@+id/accelerate"
            android:layout_marginLeft="100dp"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@mipmap/border"
            android:text="加速"
            tools:layout_editor_absoluteX="650dp"
            tools:layout_editor_absoluteY="596dp" />

        <Button
            android:layout_marginLeft="100dp"
            android:id="@+id/decelerate"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@mipmap/border"
            android:text="减速"
            tools:layout_editor_absoluteX="795dp"
            tools:layout_editor_absoluteY="596dp" />

        <Button
            android:id="@+id/stop"
            android:layout_marginLeft="100dp"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:background="@mipmap/border"
            android:text="结束"
            tools:layout_editor_absoluteX="950dp"
            tools:layout_editor_absoluteY="596dp" />


    </LinearLayout>

    <ImageView
        android:layout_centerVertical="true"
        android:layout_marginLeft="90dp"
        android:id="@+id/imageView"
        android:layout_width="261dp"
        android:layout_height="527dp"
        app:srcCompat="@mipmap/title"
        tools:layout_editor_absoluteX="141dp"
        tools:layout_editor_absoluteY="132dp" />


</RelativeLayout>

MainActivity部分

package com.suk.rotate;

import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RotateDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
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.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.suk.rotate.R;

public class MainActivity extends AppCompatActivity {

    private ImageView fan;
    private Button start;
    private Button stop;
    private Button accelerate;
    private RotateAnimation rotate;
    private Button decelerate;
    private int duration=2000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fan = findViewById(R.id.fan);
        start = findViewById(R.id.start);
        stop = findViewById(R.id.stop);
        accelerate = findViewById(R.id.accelerate);
        decelerate = findViewById(R.id.decelerate);


    }

    @Override
    protected void onStart() {
        super.onStart();

        rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setInterpolator(new LinearInterpolator());
//        rotate.setInterpolator(lin);
        rotate.setDuration(2000);//设置动画持续周期
        rotate.setRepeatCount(-1);//设置重复次数
//        rotate.setFillAfter(true);//动画执行完后是否停留在执行完的状态
        rotate.setStartOffset(10);//执行前的等待时间

        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fan.startAnimation(rotate);
            }
        });
        accelerate.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                if (duration>10){
                    duration/=2;}
                rotate.setDuration(duration);
                fan.startAnimation(rotate);
            }
        });

        decelerate.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                if (duration<10000){
                    duration*=2;}
                rotate.setDuration(duration);
                fan.startAnimation(rotate);
            }
        });
        /*
        fan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
                dialog.setTitle("点我没用");
                dialog.setIcon(R.drawable.fan);
                dialog.setPositiveButton("OK",null);
                dialog.setMessage("这是普通对话框");
                View view=View.inflate(MainActivity.this,R.layout. activity_main1, null);
                dialog.setView(view);
                dialog.create();
                dialog.show();

            }
        });
        */
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fan.clearAnimation();
            }
        });

    }
}

需要有三个图片:

​ fan.png 风扇扇叶
​ border.png 风扇边框
​ title.png 贴图
(随便找一个能看就行)

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

相关文章

  • android开发基础教程—三种方式实现xml文件解析

    android开发基础教程—三种方式实现xml文件解析

    本文将介绍三种方式:sax方式/dom方式/pull方式实现xml文件解析,感兴趣的朋友可以了解下
    2013-01-01
  • Android实现让图片在屏幕上任意移动的方法(拖拽功能)

    Android实现让图片在屏幕上任意移动的方法(拖拽功能)

    这篇文章主要介绍了Android实现让图片在屏幕上任意移动的方法,实例分析了Android拖拽功能的相关实现技巧,需要的朋友可以参考下
    2016-08-08
  • Android自定义酒店日期选择器

    Android自定义酒店日期选择器

    这篇文章主要为大家详细介绍了Android自定义酒店日期选择器、旅游日期区间选择器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • Android开发之DialogFragment用法实例总结

    Android开发之DialogFragment用法实例总结

    这篇文章主要介绍了Android开发之DialogFragment用法,结合实例形式总结分析了Android使用DialogFragment代替Dialog功能的相关使用技巧与注意事项,需要的朋友可以参考下
    2017-11-11
  • Android Toast的用法总结(五种用法)

    Android Toast的用法总结(五种用法)

    本篇文章主要介绍了Android Toast的用法总结(五种用法),android toast几种使用方法 toast经常会用到,今天做个总结,特别是自定义toast的布局,值得一看。
    2017-01-01
  • Android实现短信、微信、微博分享功能

    Android实现短信、微信、微博分享功能

    微信、微博分享功能大家都体验过吧,非常方便我们的生活,下面通过本文给大家介绍Android实现短信、微信、微博分享功能,需要的朋友参考下吧
    2017-12-12
  • Kotlin自定义View系列教程之标尺控件(选择身高、体重等)的实现

    Kotlin自定义View系列教程之标尺控件(选择身高、体重等)的实现

    这篇文章主要给大家介绍了关于Kotlin自定义View系列教程之标尺控件(选择身高、体重等)实现的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
    2018-07-07
  • Android控件之SeekBar的用法总结

    Android控件之SeekBar的用法总结

    SeekBar是进度条。本篇文章介绍了Android控件之SeekBar的使用,SeekBar的应用非常广,比如用来显示音量条、播放进度条,有水平显示也有垂直显示,有兴趣的可以了解一下。
    2017-01-01
  • flutter 常见圆角处理示例详解

    flutter 常见圆角处理示例详解

    这篇文章主要介绍了flutter 常见圆角处理示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Github简单易用的 Android ViewModel Retrofit框架

    Github简单易用的 Android ViewModel Retrofit框架

    这篇文章主要介绍了Github简单易用的Android ViewModel Retrofit框架,RequestViewMode有自动对LiveData进行缓存管理,每个retrofit api接口复用一个livedata的优势。下文具体详情,感兴趣的小伙伴可以参考一下
    2022-06-06

最新评论