Android绘制旋转动画方法详解

目录
  • 1、准备工作
  • 2、加速减速原理
  • 3、初始化
  • 4、开始
  • 5、加速
  • 6、减速
  • 7、停止
  • 8、项目源码
    • Layout部分
    • MainActivity部分

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绘制旋转动画方法详解的详细内容,更多关于Android旋转动画的资料请关注我们其它相关文章!

(0)

相关推荐

  • Android酷炫动画效果之3D星体旋转效果

    在Android中,如果想要实现3D动画效果一般有两种选择:一是使用Open GL ES,二是使用Camera.Open GL ES使用起来太过复杂,一般是用于比较高级的3D特效或游戏,并且这个也不是开源的,像比较简单的一些3D效果,使用Camera就足够了. 一些熟知的Android 3D动画如对某个View进行旋转或翻转的 Rotate3dAnimation类,还有使用Gallery( Gallery目前已过时,现在都推荐使用 HorizontalScrollView或 RecyclerVi

  • Android编程实现RotateAnimation设置中心点旋转动画效果

    本文实例讲述了Android编程实现RotateAnimation设置中心点旋转动画效果.分享给大家供大家参考,具体如下: 在xml设置: <?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="800" // 设置动画

  • Android自定义动画根据控件Y轴旋转动画(仿红包)

    这里给正在学安卓的小白们分享一个动画吧,用处挺多,代码也不多,还望各位大佬不要打击. 进入正题,先看看效果 效果很炫酷很美好 好了 来看代码吧    该说的都在代码注释里面  这个不用多说 代码极其简单 //自定义一个类继承Animation(android.view.animation.Animation)抽象类 public class MyAnimation extends Animation { // 先定义成员变量 //X轴的中心坐标 int center_X; //Y轴的中心坐标 i

  • Android使用Rotate3dAnimation实现3D旋转动画效果的实例代码

    利用Android的ApiDemos的Rotate3dAnimation实现了个图片3D旋转的动画,围绕Y轴进行旋转,还可以实现Z轴的缩放.点击开始按钮开始旋转,点击结束按钮停止旋转. 代码如下:: Rotate3dAnimation.java public class Rotate3dAnimation extends Animation { private final float mFromDegrees; private final float mToDegrees; private fi

  • Android旋转、平移、缩放和透明度渐变的补间动画

    android实现旋转.平移.缩放和透明度渐变的补间动画,具体实现如下: 1.在新建项目的res目录中,创建一个名为anim的目录,并在该目录中创建实现旋转.平移.缩放和透明度渐变的动画资源文件. 透明度渐变的动画资源文件anim_alpha.xml(完全不透明->完全透明->完全不透明) <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://sch

  • Android补间动画基本使用(位移、缩放、旋转、透明)

    本文讲述了Android补间动画基本使用(位移.缩放.旋转.透明).分享给大家供大家参考,具体如下: 补间动画 原形态变成新形态时为了过渡变形过程,生成的动画就叫补间动画 位移.旋转.缩放.透明 位移: 参数10指的是X的起点坐标,但不是指屏幕x坐标为10的位置,而是imageview的 真实X + 10 参数150指的是X的终点坐标,它的值是imageview的 真实X + 150 //创建为位移动画对象,设置动画的初始位置和结束位置 TranslateAnimation ta = new T

  • Android实现旋转动画的两种方式案例详解

    目录 练习案例 效果展示 前期准备 自定义 View java代码编写 方法一 方法二 易错点总结: 练习案例 视差动画 - 雅虎新闻摘要加载 效果展示 前期准备 第一步:准备好颜色数组 res => values => colors.xml <color name="orange">#FF9600</color> <color name="aqua">#02D1AC</color> <color n

  • Android 3D旋转动画效果实现分解

    这篇文章主要介绍一下如何实现View的3D旋转效果,实现的主要原理就是围绕Y轴旋转,同时在Z轴方面上有一个深入的缩放. 演示的demo主要有以下几个重点: 1,自定义旋转动画 2,动画做完后,重置ImageView 先看一下程序的运行效果:  1,自定义动画类 这里实现了一个Rotate3dAnimation的类,它扩展了Animation类,重写applyTransformation()方法,提供指定时间的矩阵变换,我们在这个方法里,就可以利用Camera类得得到一个围绕Y轴旋转的matrix

  • Android绘制旋转动画方法详解

    目录 1.准备工作 2.加速减速原理 3.初始化 4.开始 5.加速 6.减速 7.停止 8.项目源码 Layout部分 MainActivity部分 1.准备工作 首先需要有一个用于旋转的图片 需要考虑如何开始.结束.加速.减速 2.加速减速原理 本次的动画采用RotateAnimation,初始化需要的参数如下 public RotateAnimation(float fromDegrees,float toDegrees,int pivotXType,float pivotXValue,i

  • Android中SQLite 使用方法详解

    Android中SQLite 使用方法详解 现在的主流移动设备像android.iPhone等都使用SQLite作为复杂数据的存储引擎,在我们为移动设备开发应用程序时,也许就要使用到SQLite来存储我们大量的数据,所以我们就需要掌握移动设备上的SQLite开发技巧.对于Android平台来说,系统内置了丰富的API来供开发人员操作SQLite,我们可以轻松的完成对数据的存取. 下面就向大家介绍一下SQLite常用的操作方法,为了方便,我将代码写在了Activity的onCreate中: @Ov

  • Android 中 onSaveInstanceState()使用方法详解

    Android 中 onSaveInstanceState()使用方法详解 覆盖onSaveInstanceState方法,并在onCreate中检测savedInstanceState和获取保存的值 @Override protected void onSaveInstanceState(Bundle outState) { outState.putInt("currentposition", videoView.getCurrentPosition()); super.onSave

  • Android Parcelable接口使用方法详解

     Android Parcelable接口使用方法详解 1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel. Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementin

  • Android StringBuffer的使用方法详解

    今天,讲讲StringBuffer的使用. StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBuffer在进行字符串处理时,不生成新的对象,在内存使用上要优于String类. 所以在实际使用时,如果经常需要对一个字符串进行修改,例如插入.删除等操作,使用StringBuffer要更加适合一些. 在StringBuffer类中存在很多和String类一样的方法,这些方法在功能上和String类中的功能

  • Android自定义View实现绘制虚线的方法详解

    前言 说实话当第一次看到这个需求的时候,第一反应就是Canvas只有drawLine方法,并没有drawDashLine方法啊!这咋整啊,难道要我自己做个遍历不断的drawLine?不到1秒,我就放弃这个想法了,因为太恶心了.方法肯定是有的,只不过我不知道而已. 绘制方法 最简单的方法是利用ShapeDrawable,比如说你想用虚线要隔开两个控件,就可以在这两个控件中加个View,然后给它个虚线背景. 嗯,理论上就是这样子的,实现上也很简单. <!-- drawable 文件 --> <

  • Android onActivityResult和setResult方法详解及使用

    Android onActivityResult和setResult方法 最近做公司项目遇到Android onActivityResult和setResult ,在应用过程中进程报错,这里有必要记录下,以免再次使用出错. 如果你想在Activity中得到新打开Activity关闭后返回的数据,你需要使用系统提供的startActivityForResult(Intent intent,int requestCode)方法打开新的Activity,新的Activity关闭后会向前面的Activi

  • Android编程解析XML方法详解(SAX,DOM与PULL)

    本文实例讲述了Android编程解析XML方法.分享给大家供大家参考,具体如下: XML在各种开发中都广泛应用,Android也不例外.作为承载数据的一个重要角色,如何读写XML成为Android开发中一项重要的技能.今天就由我向大家介绍一下在Android平台下几种常见的XML解析和创建的方法. 在Android中,常见的XML解析器分别为SAX解析器.DOM解析器和PULL解析器,下面,我将一一向大家详细介绍. SAX解析器: SAX(Simple API for XML)解析器是一种基于事

  • Android编程闹钟设置方法详解

    本文实例讲述了Android编程闹钟设置方法.分享给大家供大家参考,具体如下: 闹钟在生活中最常见了,在Android中可以通过AlarmManager来实现闹钟,AlarmManager类专门用来设置在某个指定的时间去完成指定的时间.AlarmManager就会通过onReceive()方法去执行这些事件,就算系统处于待机状态,同样不会影响运行.可以通过Context.getSystemService方法来获得该服务.AlarmManager中的方法不少,如下: 方法 说明 Cancel 取消

  • Android结束进程的方法详解

    本文实例讲述了Android结束进程的方法.分享给大家供大家参考,具体如下: 最近在做一个类似与任务管理器的东西,里面有个功能,可以通过这个管理器结束掉其他的进程. 在Android平台下,结束进程的方法还是比较多的.首先指明,此处的"结束进程",包含了结束自身进程和结束其他进程两个方面.通过查阅SDK文档和网上的一些资料,自己找到一些结束进程的方法.在这里做一些归纳和总结,文章的部分信息有可能来自网上已有的文章和帖子,由于过了比较长时间,所以若发现本文与其他文章雷同,请谅解. 一.结

随机推荐