Android 动画实现 从基础到自定义

1. 基础使用

由于是继承了ValueAnimator类
所以使用的方法十分类似:XML 设置 / Java设置

1.1 Java设置

  ObjectAnimator animator = ObjectAnimator.ofFloat(Object object, String property, float ....values);  

// Object object:需要操作的对象
// String property:需要操作的对象的属性
// float …values:动画初始值 & 结束值(不固定长度)
// 若是两个参数a,b,则动画效果则是从属性的a值到b值
// 若是三个参数a,b,c,则则动画效果则是从属性的a值到b值再到c值
// 以此类推

anim.setDuration(500);
        // 设置动画运行的时长

        anim.setStartDelay(500);
        // 设置动画延迟播放时间

        anim.setRepeatCount(0);
        // 设置动画重复播放次数 = 重放次数+1
        // 动画播放次数 = infinite时,动画无限重复

        anim.setRepeatMode(ValueAnimator.RESTART);
        // 设置重复播放动画模式
        // ValueAnimator.RESTART(默认):正序重放
        // ValueAnimator.REVERSE:倒序回放

animator.start();  

// 启动动画

1.2 XML 设置

    步骤1:在路径 res/animator 的文件夹里创建动画效果.xml文件


    此处设置为res/animator/set_animation.xml


    步骤2:设置动画参数

set_animation.xml
// ObjectAnimator 采用<animator>  标签

<objectAnimator xmlns:android=“http://schemas.android.com/apk/res/android”
android:valueFrom=“1” // 初始值
android:valueTo=“0” // 结束值
android:valueType=“floatType” // 变化值类型 :floatType & intType
android:propertyName=“alpha” // 对象变化的属性名称

/>

在Java代码中启动动画
Animator animator = AnimatorInflater.loadAnimator(context, R.animator.view_animation);  

// 载入XML动画

animator.setTarget(view);
// 设置动画对象

animator.start();
// 启动动画

1.3 使用实例

此处先展示四种基本变换:平移、旋转、缩放 & 透明度
a. 透明度
mButton = (Button) findViewById(R.id.Button);
    // 创建动画作用对象:此处以Button为例

    ObjectAnimator animator = ObjectAnimator.ofFloat(mButton, "alpha", 1f, 0f, 1f);
    // 表示的是:
    // 动画作用对象是mButton
    // 动画作用的对象的属性是透明度alpha
    // 动画效果是:常规 - 全透明 - 常规
    animator.setDuration(5000);
    animator.start();

在这里插入图片描述

b. 旋转
mButton = (Button) findViewById(R.id.Button);
    // 创建动画作用对象:此处以Button为例

ObjectAnimator animator = ObjectAnimator.ofFloat(mButton, “rotation”, 0f, 360f);

    // 表示的是:
    // 动画作用对象是mButton
    // 动画作用的对象的属性是旋转alpha
    // 动画效果是:0 - 360
    animator.setDuration(5000);
    animator.start();

在这里插入图片描述

c. 平移
mButton = (Button) findViewById(R.id.Button);
    // 创建动画作用对象:此处以Button为例

float curTranslationX = mButton.getTranslationX();
// 获得当前按钮的位置
ObjectAnimator animator = ObjectAnimator.ofFloat(mButton, “translationX”, curTranslationX, 300,curTranslationX);
在这里插入图片描述

    // 表示的是:
    // 动画作用对象是mButton
    // 动画作用的对象的属性是X轴平移(在Y轴上平移同理,采用属性"translationY"
    // 动画效果是:从当前位置平移到 x=1500 再平移到初始位置
    animator.setDuration(5000);
    animator.start();



d. 缩放
mButton = (Button) findViewById(R.id.Button);
    // 创建动画作用对象:此处以Button为例

在这里插入图片描述

ObjectAnimator animator = ObjectAnimator.ofFloat(mButton, “scaleX”, 1f, 3f, 1f);
// 表示的是:
// 动画作用对象是mButton
// 动画作用的对象的属性是X轴缩放
// 动画效果是:放大到3倍,再缩小到初始大小
animator.setDuration(5000);
animator.start();

2. 通过自定义对象属性实现动画效果

    在上面的讲解,我们使用了属性动画最基本的四种动画效果:透明度、平移、旋转 & 缩放

2.1 具体使用

对于属性动画,其拓展性在于:不局限于系统限定的动画,可以自定义动画,即自定义对象的属性,并通过操作自定义的属性从而实现动画。
那么,该如何自定义属性呢?本质上,就是:

    为对象设置需要操作属性的set() & get()方法
    通过实现TypeEvaluator类从而定义属性变化的逻辑


    类似于ValueAnimator的过程

2.2 实例讲解

下面,我将用一个实例来说明如何通过自定义属性实现动画效果

    
        实现的动画效果:一个圆的颜色渐变 

在这里插入图片描述

        自定义属性的逻辑如下:(需要自定义属性为圆的背景颜色)
    


步骤1:设置对象类属性的set() & get()方法
设置对象类属性的set() & get()有两种方法:

    
        通过继承原始类,直接给类加上该属性的 get()& set(),从而实现给对象加上该属性的 get()& set()
    
    
        通过包装原始动画对象,间接给对象加上该属性的 get()& set()。即 用一个类来包装原始对象
    

此处主要使用第一种方式进行展示。

    关于第二种方式的使用,会在下一节进行详细介绍。

MyView2.java
public class MyView2 extends View {
   
// 设置需要用到的变量
public static final 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android实现水平进度条动画,可以通过自定义 View 来实现。以下是实现的步骤: 1. 创建自定义 View 类,继承自 View。 2. 在 View 中添加一个属性,表示进度值,例如 progress。 3. 在 View 的 onDraw 方法中绘制进度条背景和进度条前景。 4. 在 View 中增加一个动画,可以使用 ValueAnimator 或 ObjectAnimator 类来实现。在动画更新的时候,设置进度值,并调用 invalidate 方法来触发 View 的重绘。 5. 在使用该自定义 View 的 Activity 或 Fragment 中,调用 View 的 startAnimation 方法来开始动画。 以下是一个简单的示例代码: ``` public class HorizontalProgressBar extends View { private int progress = 0; private Paint bgPaint, fgPaint; private ValueAnimator animator; public HorizontalProgressBar(Context context) { super(context); init(); } public HorizontalProgressBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { bgPaint = new Paint(); bgPaint.setColor(Color.GRAY); fgPaint = new Paint(); fgPaint.setColor(Color.BLUE); animator = ValueAnimator.ofInt(0, 100); animator.setDuration(1000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { progress = (int) animation.getAnimatedValue(); invalidate(); } }); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int width = getWidth(); int height = getHeight(); canvas.drawRect(0, 0, width, height, bgPaint); canvas.drawRect(0, 0, width * progress / 100, height, fgPaint); } public void startAnimation() { animator.start(); } } ``` 在使用时,只需要将该自定义 View 添加到布局文件中,并在 Activity 或 Fragment 中调用 startAnimation 方法即可: ``` HorizontalProgressBar progressBar = findViewById(R.id.progress_bar); progressBar.startAnimation(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值