Android旋转动画
16lz
2021-12-18
第一种:
代码继承Animation实现
第二种:
在xml中配置
代码继承Animation实现
package com.iaiai.an;import android.graphics.Matrix;import android.view.animation.Animation;import android.view.animation.LinearInterpolator;import android.view.animation.Transformation;/** * * <br/> * Title: WindowAnimation.java<br/> * E-Mail: 176291935@qq.com<br/> * QQ: 176291935<br/> * Http: iaiai.iteye.com<br/> * Create time: 2013-8-27 上午10:22:22<br/> * <br/> * * @author 丸子 * @version 0.0.1 */public class WindowAnimation extends Animation {private int halfWidth;private int halfHeight;private int duration;public WindowAnimation(int duration) {this.duration = duration;}@Overrideprotected void applyTransformation(float interpolatedTime, Transformation t) {super.applyTransformation(interpolatedTime, t);Matrix matrix = t.getMatrix();//matrix.preScale(interpolatedTime, interpolatedTime);matrix.preRotate(interpolatedTime * 360); // 进行旋转matrix.preTranslate(-halfWidth, -halfHeight); // 改变动画的起始位置,把扩散点和起始点移到中间matrix.postTranslate(halfWidth, halfHeight);}@Overridepublic void initialize(int width, int height, int parentWidth, int parentHeight) {super.initialize(width, height, parentWidth, parentHeight);this.setDuration(duration);this.setFillAfter(true);this.halfHeight = height / 2;this.halfWidth = width / 2;this.setInterpolator(new LinearInterpolator());}}
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);final Animation animation = new WindowAnimation(1000);animation.setRepeatCount(-1);//执行多少次findViewById(R.id.btn).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {findViewById(R.id.imageView1).setAnimation(animation);animation.start();}});}
第二种:
在xml中配置
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" > <rotate android:duration="1000" android:fromDegrees="0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:repeatCount="-1" android:shareInterpolator="true" android:toDegrees="+360" /> <!--rotate 旋转动画效果 repeatCount 旋转次数===========================interpolator 指定一个动画的插入器,用来控制动画的速度变化 AccelerateInterpolator:动画开始时比较慢,然后逐渐加速。DecelerateInterpolator:动画开始时比较快,然后逐渐减速。AccelerateDecelerateInterpolator:动画开始时和结束时比较慢,中间过程加速。LinearInterpolator:动画匀速进行。CycleInterpolator:动画循环播放指定次数,速率沿着正弦曲线改变。)=============================fromDegrees 属性为动画起始时物件的角度 toDegrees 属性为动画结束时物件旋转的角度,+代表顺时针 duration 属性为动画持续时间,以毫秒为单位pivotX/pivotY 属性为动画旋转中心位置shareInterpolator --></set>
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);final Animation animation = (AnimationSet) AnimationUtils.loadAnimation(this, R.anim.xz);findViewById(R.id.btn).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {findViewById(R.id.imageView1).setAnimation(animation);animation.start();}});}
更多相关文章
- Android的Spinner组件的XML属性(备查)
- Android(安卓)五大布局之(一) 线性布局和相对布局
- Android(安卓)解析XML
- Android(安卓)播放Gif 动画
- Android遍历API (1) 动画篇——克隆动画AnimationCloning
- Android(安卓)12上全新的应用启动画面,还不适配一下?
- android 动态 布局
- Android属性动画(ObjectAnimation)
- Android沉浸式状态栏,看完这篇就够了!