本篇内容主要讲解“Android如何实现旋转动画”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android如何实现旋转动画”吧!1、准备工作首先需要有一个用于旋转的图片需要考虑如何开始、结束、
本篇内容主要讲解“Android如何实现旋转动画”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android如何实现旋转动画”吧!
首先需要有一个用于旋转的图片
需要考虑如何开始、结束、加速、减速
本次的动画采用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度 也是一种加速。
反之就是减速。
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);//执行前的等待时间
start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fan.startAnimation(rotate); } });
首先需要创建全局变量
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); //开始旋转 } });
decelerate.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (duration<10000){ duration*=2; //周期乘2角度不变减速(需要考虑极端,所以加一个判断) } rotate.setDuration(duration); //设置周期 fan.startAnimation(rotate); //开始旋转 }});
stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fan.clearAnimation(); //停止 }});
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); } }); stop.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fan.clearAnimation(); } }); }}
需要有三个图片:
fan.png 风扇扇叶
border.png 风扇边框
title.png 贴图
(随便找一个能看就行)
到此,相信大家对“Android如何实现旋转动画”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
--结束END--
本文标题: Android如何实现旋转动画
本文链接: https://www.lsjlt.com/news/306490.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0