Android View动画整理

这篇具有很好参考价值的文章主要介绍了Android View动画整理。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

View 动画相关内容可参考官网 动画资源

此前也有写 View 动画相关的内容,但都只是记录代码,没有特别分析。以此篇作为汇总、整理、分析。

Android View 动画有4中,分别是

  • 平移动画 TranslateAnimation
  • 缩放动画 ScaleAnimation
  • 旋转动画 RotateAnimation
  • 透明度动画 AlphaAnimation

View 动画可以单独使用,也可以一起使用,一起使用就叫复合动画。

实现 View 动画有 2 种方式,java 方式和 xml 方式。

Demo 图说明,动画作用在图片上。图片显示在左上角,图片右侧和下方的线是为了方便看出 View 动画前后的位置、大小对比,无实际作用。
Android View动画整理,Android,Animation,android

平移动画 TranslateAnimation

对 View 做水平或者竖直方向上的移动。

java 方式

import android.view.animation.TranslateAnimation;

TranslateAnimation translateAnimation = new TranslateAnimation(0,imageView.getWidth(),0,0);
translateAnimation.setDuration(3000);//动画时长
translateAnimation.setFillAfter(true);//true :view停留在动画结束的地方。false :动画结束后 view 返回原位
imageView.startAnimation(translateAnimation);

xml 方式

用 AS 可以很方便地创建 anim 目录和动画文件。
Android View动画整理,Android,Animation,android

在 res/anim/view_anim_translate.xml 创建文件,写入

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <!--
    android:fromXDelta
    Float or percentage. 移动起始点的x坐标. 表示形式有三种:
    1 相对于自己的左边界的距离,单位像素值。(例如 "5")
    2 相对于自己的左边界的距离与自身宽度的百分比。(例如  "5%")
    3 相对于父View的左边界的距离与父View宽度的百分比。(例如 "5%p")

    android:toXDelta
    Float or percentage. 移动结束点的x坐标. 表现形式同上

    android:fromYDelta
    Float or percentage. 移动起始点的y坐标. 表示形式有三种:
    1 相对于自己的上边界的距离,单位像素值。(例如 "5")
    2 相对于自己的上边界的距离与自身高度的百分比。(例如  "5%")
    3 相对于父View的上边界的距离与父View高度的百分比。(例如 "5%p")

    android:toYDelta
    Float or percentage. 移动结束点的y坐标. 表现形式同上
    -->

    <translate
        android:duration="3000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="800"
        android:toYDelta="0"/>
</set>

然后通过 AnimationUtils 实现,

Animation animTran = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_translate);
animTran.setFillAfter(true);
imageView.startAnimation(animTran);

实现效果:View 向右平移自身宽度的距离。
Android View动画整理,Android,Animation,android

缩放动画 ScaleAnimation

对 View 进行缩放操作。

java 方式

放大为 1.5 倍用 1.5f ,缩小为 0.5 倍用 0.5f ,很好理解。

import android.view.animation.AccelerateInterpolator;
import android.view.animation.ScaleAnimation;

ScaleAnimation scaleAnimation = new ScaleAnimation(1 ,1.5f ,1 ,1.5f);// X轴 Y轴都放大 1.5 倍
scaleAnimation.setDuration(3000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setInterpolator(new AccelerateInterpolator());// 设置动画插值器,用来控制动画速度,这是加速插值器,动画速度会越来越快
imageView.startAnimation(scaleAnimation);

方法 public ScaleAnimation(float fromX, float toX, float fromY, float toY) {} 默认缩放总中心是 (0,0) ,即 View 的左上角。
方法 public ScaleAnimation(float fromX, float toX, float fromY, float toY,float pivotX, float pivotY) {} 最后两个参数指定缩放中心点。

缩放中心点不同,缩放效果是不同的。
如,
效果1 ,new ScaleAnimation(1 ,0.5f ,1 , 0.5f);
Android View动画整理,Android,Animation,android

效果2,new ScaleAnimation(1 ,0.5f ,1 ,0.5f, (float) 200, (float) 100);
Android View动画整理,Android,Animation,android
效果3,new ScaleAnimation(1 ,0.5f ,1 ,0.5f, (float) imageView.getWidth(), (float) imageView.getHeight());
Android View动画整理,Android,Animation,android

xml 方式

创建 res/anim/view_anim_translate.xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <!--
        android:fromXScale
        Float. 水平方向缩放比例的初始值,1.0是没有变化。

        android:toXScale
        Float. 水平方向缩放比例的结束值,1.0是没有变化。

        android:fromYScale
        Float. 竖直方向缩放比例的初始值,1.0是没有变化。

        android:toYScale
        Float. 竖直方向缩放比例的结束值,1.0是没有变化。

        android:pivotX
        Float. 缩放中心点的x坐标

        android:pivotY
        Float. 缩放中心点的y坐标
    -->

    <scale
        android:duration="3000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5" />
</set>

然后通过 AnimationUtils 实现,

Animation animScale = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_scale);
animScale.setFillAfter(true);
imageView.startAnimation(animScale);

实现效果:View 在竖直和水平方向上都放大 1.5 倍。
Android View动画整理,Android,Animation,android

旋转动画 RotateAnimation

对 View 进行旋转。

默认以 View 的左上角为中心点旋转,也可以指定旋转中心的坐标。旋转中心的坐标不同,旋转效果也是不一样的。

java 方式

import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.ScaleAnimation;

RotateAnimation rotateAnimation = new RotateAnimation(0,180, imageView.getWidth(), imageView.getHeight());
rotateAnimation.setFillAfter(true);
rotateAnimation.setDuration(3000);
rotateAnimation.setRepeatCount(1);//重复1次
rotateAnimation.setInterpolator(new AccelerateDecelerateInterpolator());// 设置动画插值起,用来控制动画速度,这是加速减速插值器,动画速度先快后慢
imageView.startAnimation(rotateAnimation);

xml 方式

创建 res/anim/view_anim_rotate.xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <!--
    android:fromDegrees
    Float. 旋转初始的角度。
    android:toDegrees
    Float. 旋转结束的角度。
    android:pivotX
    Float or percentage. 旋转中心点x坐标,表示形式有三种:
    1 相对于自己的左边界的距离,单位像素值。(例如 "5")
    2 相对于自己的左边界的距离与自身宽度的百分比。(例如 "5%")
    3 相对于父View的左边界的距离与父View宽度的百分比。(例如 "5%p")
    android:pivotY
    Float or percentage. 旋转中心点y坐标,表示形式有三种:
    1 相对于自己的上边界的距离,单位像素值。(例如 "5")
    2 相对于自己的上边界的距离与自身宽度的百分比。(例如 "5%")
    3 相对于父View的上边界的距离与父View高度的百分比。(例如 "5%p")
    -->

    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="100%"
        android:pivotY="100%"
        android:toDegrees="+180"
        android:repeatCount="1" />
</set>

然后通过 AnimationUtils 实现,

Animation animRotate = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_rotate);
animRotate.setFillAfter(false);
imageView.startAnimation(animRotate);

实现效果:绕着 View 的右下角旋转 180° ,重复1次。
Android View动画整理,Android,Animation,android

透明度动画 AlphaAnimation

对 View 进行透明度操作。

java 方式

import android.view.animation.AlphaAnimation;

AlphaAnimation alphaAnim = new AlphaAnimation(1f, 0);
alphaAnim.setDuration(2000);
alphaAnim.setFillAfter(true);
imageView.startAnimation(alphaAnim);

xml 方式

创建 res/anim/view_anim_alpha.xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--
        android:fromAlpha
        Float. 设置透明度的初始值,其中0.0是透明,1.0是不透明的。

        android:toAlpha
        Float. 设置透明度的结束值,其中0.0是透明,1.0是不透明的。
    -->

    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

然后通过 AnimationUtils 实现,

Animation alphaAnimation = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_alpha);
alphaAnimation.setFillAfter(true);
imageView.startAnimation(alphaAnimation);

实现效果:View 变为不透明。
Android View动画整理,Android,Animation,android

复合动画

复合动画就是把 View 动画结合使用。也是通过 java 方式和 xml 方式实现。

平移+透明度

import android.view.animation.AnimationSet;

AnimationSet set1 = new AnimationSet(true);
TranslateAnimation tAnimation1 = new TranslateAnimation(0,imageView.getWidth(),0,0);
AlphaAnimation aAnim1 = new AlphaAnimation(1f, 0.2f);

set1.addAnimation(tAnimation1);
set1.addAnimation(aAnim1);
set1.setDuration(2500);
set1.setFillAfter(true);
imageView.startAnimation(set1);

实现效果:View 向右平移自身宽度的距离,透明度逐渐变为 0.2f 。
Android View动画整理,Android,Animation,android

平移+缩放

AnimationSet set2 = new AnimationSet(true);
TranslateAnimation tAnimation2 = new TranslateAnimation(0,(float) imageView.getWidth()/4,0,(float) imageView.getHeight()/8);

ScaleAnimation sAnimation2 = new ScaleAnimation(1 ,1.5f ,1 ,1.5f);

set2.addAnimation(tAnimation2);
set2.addAnimation(sAnimation2);
set2.setDuration(2500);
set2.setFillAfter(true);
imageView.startAnimation(set2);

实现效果:View 向右平移自身宽度 1/4 的距离,向下平移自身高度 1/8 的距离,放大 1.5 倍 。
Android View动画整理,Android,Animation,android

平移+旋转

AnimationSet set3 = new AnimationSet(true);
TranslateAnimation tAnimation3 = new TranslateAnimation(0,imageView.getWidth(),0,imageView.getHeight());
RotateAnimation rAnimation3 = new RotateAnimation(0,360, imageView.getWidth(), imageView.getHeight());

set3.addAnimation(tAnimation3);
set3.addAnimation(rAnimation3);
set3.setDuration(2500);
set3.setFillAfter(true);
imageView.startAnimation(set3);

实现效果:View 向右平移自身宽度的距离,向下平移自身高度的距离,绕着右下角旋转 360°
Android View动画整理,Android,Animation,android

平移+旋转+透明度

java 方式

AnimationSet set4 = new AnimationSet(false);
RotateAnimation rAnimation4 = new RotateAnimation(0,720, (float) imageView.getWidth()/2, (float) imageView.getHeight()/2);
rAnimation4.setInterpolator(new AccelerateDecelerateInterpolator());
rAnimation4.setDuration(3500);

AlphaAnimation aAnim4 = new AlphaAnimation(1f, 0.5f);
aAnim4.setInterpolator(new AccelerateInterpolator());
aAnim4.setDuration(3000);

ScaleAnimation sAnimation4 = new ScaleAnimation(1 ,0.5f ,1 ,0.5f, (float) imageView.getWidth()/2, (float) imageView.getHeight()/2);
sAnimation4.setInterpolator(new LinearInterpolator());
sAnimation4.setDuration(2500);

set4.addAnimation(rAnimation4);
set4.addAnimation(aAnim4);
set4.addAnimation(sAnimation4);
set4.setFillAfter(true);
set4.setStartOffset(500);
mageView.startAnimation(set4);

xml 方式

创建 res/anim/view_anim_set4.xml 文件,

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">

    <rotate
        android:duration="3500"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="720" />

    <alpha
        android:duration="3000"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.5" />

    <scale
        android:duration="2500"
        android:fromXScale="1"
        android:fromYScale="1"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.5"
        android:toYScale="0.5" />
</set>

然后通过 AnimationUtils 实现,

Animation anim4 = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_set4);
anim4.setFillAfter(true);
imageView.startAnimation(anim4);

实现效果:以 View 的中心旋转 720° ,缩小为 0.5 倍 ,透明度逐渐变为 0.5f 。
Android View动画整理,Android,Animation,android

监听动画过程

使用 Animation.setAnimationListener(AnimationListener listener) 方法即可监听动画的开始、结束、重复等过程。

Animation anim4 = AnimationUtils.loadAnimation(ViewAnimationActivity.this, R.anim.view_anim_set4);
                anim4.setFillAfter(true);
                anim4.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                imageView.startAnimation(anim4);

对点击事件的影响

View 动画对点击事件无影响。

即 View 的大小、位置变化后,点击 View 的原始位置,点击事件还是有的。

如果动画使 View 超出了原始位置,超出的部分,是无法响应点击事件的。

插值器

插值器用来控制动画的执行速度。

通过 Animation.setInterpolator(Interpolator i) 方法可以设置动画的插值器,如

ScaleAnimation scaleAnimation = new ScaleAnimation(1 ,1.5f ,1 ,1.5f);
scaleAnimation.setDuration(3000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setInterpolator(new AccelerateInterpolator());// 设置动画插值器
imageView.startAnimation(scaleAnimation);

也可以直接在动画 xml 文件中指定 android:interpolator

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="3000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.5"
        android:toYScale="1.5" />
</set>

系统预设的插值器有:

插值器 说明
AccelerateDecelerateInterpolator 开始和结束速度慢,中间加速
AccelerateInterpolator 开始慢,逐渐加速
AnticipateInterpolator 先反方向执行动画再正方向执行
AnticipateOvershootInterpolator AnticipateInterpolator 结合 OvershootInterpolator ,先反方向执行动画再正方向执行,到动画终点后继续前进一段距离然后回到动画终点
BounceInterpolator 自由落体,到终点后回弹几下再回到终点,类似乒乓球落地效果
CycleInterpolator 正弦曲线,先正方向执行动画再反方向执行动画
DecelerateInterpolator 开始快,结束慢
LinearInterpolator 线性、匀速
OvershootInterpolator 动画终点后继续前进一段距离然后回到动画终点
PathInterpolator 通过它的构造函数实现贝塞尔曲线
LinearOutSlowInInterpolator androidx里的,速度曲线用的贝塞尔曲线,开始速度快,逐渐减速,类似 DecelerateInterpolator 但速度更快
FastOutLinearInInterpolator androidx里的,,速度曲线用的贝塞尔曲线,逐渐加速,类似 AccelerateInterpolator 但速度更快
FastOutSlowInInterpolator androidx里的,速度曲线用的贝塞尔曲线,先加速再减速,类似 AccelerateDecelerateInterpolator 但速度更快

这些插值器基本满足了日常需求。

效果对比:
Android View动画整理,Android,Animation,android
除了 CycleInterpolator ,绿色线是所有动画的终点。

效果对比2:
Android View动画整理,Android,Animation,android

自定义插值器

自定义插值器,

继承 BaseInterpolator ,重写 getInterpolation(float input) 方法,

相关继承关系为 BaseInterpolator >> Interpolator >> TimeInterpolator ,

package android.animation;

/**
 * A time interpolator defines the rate of change of an animation. This allows animations
 * to have non-linear motion, such as acceleration and deceleration.
 */
public interface TimeInterpolator {

    /**
     * Maps a value representing the elapsed fraction of an animation to a value that represents
     * the interpolated fraction. This interpolated value is then multiplied by the change in
     * value of an animation to derive the animated value at the current elapsed animation time.
     *
     * @param input A value between 0 and 1.0 indicating our current point
     *        in the animation where 0 represents the start and 1.0 represents
     *        the end
     * @return The interpolation value. This value can be more than 1.0 for
     *         interpolators which overshoot their targets, or less than 0 for
     *         interpolators that undershoot their targets.
     */
    float getInterpolation(float input);
}

input 取值是从 0.0 到 1.0 ,分别表示动画的开始和结束。

看系统插值器的实现,都涉及到数学公式~~

本例的效果和 LinearInterpolator 是一样的,因为抄的 LinearInterpolator 的实现。 😄

import android.view.animation.BaseInterpolator;

public class MyInterpolator extends BaseInterpolator {

    @Override
    public float getInterpolation(float input) {
        return input;
    }
}

测试 return input/2return input*1.1f 的结果如下,
Android View动画整理,Android,Animation,android
Android View动画整理,Android,Animation,android
虽然最终效果是不适宜的,但说明自定义插值器有效果。
Android View动画整理,Android,Animation,android文章来源地址https://www.toymoban.com/news/detail-671479.html

到了这里,关于Android View动画整理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • android开发之Android 自定义滑动解锁View

    自定义滑动解锁View 需求如下: 近期需要做一个类似屏幕滑动解锁的功能,右划开始,左划暂停。 需求效果图如下 实现效果展示 自定义view如下 /** Desc 自定义滑动解锁View Author ZY Mail sunnyfor98@gmail.com Date 2021/5/17 11:52 */ @SuppressLint(“ClickableViewAccessibility”) class SlideSwitchButton :

    2024年02月13日
    浏览(25)
  • 【Android】之【View绘制】

    简单的可以说,如 measure,layout,draw 分别对应测量,布局,绘制三个过程。 ① measure:测量。系统会先根据xml布局文件和代码中对控件属性的设置,来获取或者计算出每个View和ViewGrop的尺寸,并将这些尺寸保存下来。 ② layout:布局。根据测量出的结果以及对应的参数,来确

    2024年02月08日
    浏览(31)
  • android 自定义圆角View

    public class CustomView extends View {     private float cornerRadius;     public CustomView(Context context) {         super(context);         init();     }     public CustomView(Context context, AttributeSet attrs) {         super(context, attrs);         TypedArray typedArray = context.obt

    2024年02月11日
    浏览(36)
  • Android自定义View流程

    在开发中,View视图具有非常重要的作用,它是直接呈现给使用者的,因此向用户展示精美高效的View视图很有意义。Android系统提供了丰富的视图组件,如TextView、ImageView、Button等,还提供了RelativeLayout、LinearLayout、FrameLayout等组合组件,使用这些组件搭配能实现良好的视图效果

    2024年04月11日
    浏览(35)
  • Android View滑动处理大法

    原文链接 Android View滑动处理大法 对于触控式操作来说,滑动是一个特别重要的手势操作,如何做到让应用程序的页面滑动起来如丝般顺滑,让用户感觉到手起刀落的流畅感,是开发人猿需要重点解决的问题,这对提升用户体验是最为重要的事情。本文就将探讨一下,Android中

    2024年02月13日
    浏览(28)
  • 微信小程序animation动画,微信小程序animation动画无限循环播放

    需求是酱紫的: 页面顶部的喇叭通知,内容不固定,宽度不固定,就是做走马灯(轮播)效果,从左到右的走马灯(轮播),每播放一遍暂停 1500ms ~ 2000ms 刚开始想的是 css 的 position: relative + animation,如果宽度固定还好说,宽度不固定,用百分比的话,想象很美好,现实很

    2024年02月13日
    浏览(43)
  • Android 动态控制view的大小

    这样就实现了控件尺寸随着我们手指滑动变化了。 OnTouchListener中有event.getX(),event.getY(),和event.getRawX(),event.getRawY(),他们究竟有什么区别了? getX和getY代表相当于控件本身的位置,getRawX和getRawY相对屏幕的位置

    2023年04月08日
    浏览(92)
  • Android 遍历界面所有的View

    关于作者:CSDN内容合伙人、技术专家, 从零开始做日活千万级APP。 专注于分享各领域原创系列文章 ,擅长java后端、移动开发、商业变现、人工智能等,希望大家多多支持。 我们继续总结学习,温故知新。 本文讲述如何遍历获取页面中所有的view,并输出对应的id,textview文

    2024年02月08日
    浏览(28)
  • Android View实现滑动的方式

    实现View的滑动有三种方式 通过View本身提供的scrollTo/scrollBy方法实现滑动 通过动画给View施加平移效果来实现滑动 通过改变View LayoutParams使得View重新布局从而实现滑动 scrollTo:通过传递的参数实现绝对滑动 scrollBy:通过传递的参数实现相对滑动 scrollTo和scrollBy只能改变View内容

    2024年02月16日
    浏览(37)
  • Android自定义View之游戏摇杆键盘实现(一),快手android面试经验

    public class RemoteViewBg { private Bitmap bitmapBg; public RemoteViewBg(Bitmap bitmap) { bitmapBg = bitmap; } //背景的绘图函数 public void draw(Canvas canvas, Paint paint, Rect src0 ,Rect dst0 ) { canvas.drawBitmap(bitmapBg, src0, dst0, paint); } } 重写系统的触摸时间,判断触摸点在背景范围内还是背景范围外 @Override public b

    2024年04月12日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包