Android View动画——自定义View动画

本文介绍了如何在Android中创建自定义View动画,特别讲解了模仿QQ客户端的抖动特效以及实现类似电视关机画面的3D旋转动画。通过继承抽象类并重写关键方法来控制动画过程,利用Matrix矩阵修改动画数值以达到预期效果。
摘要由CSDN通过智能技术生成

If we don’t have the guts to try anything, what’s the meaning of life.
——如果我们任何事情都没有勇气去尝试,人生还有什么意义。

1. 自定义View动画

这里给出一个模仿QQ客户端的抖一抖特效:
https://i-blog.csdnimg.cn/blog_migrate/e1a3315340c89ac6b0f9d2cd5395eb80.gif
这里窗口抖动的特效代码为:

package com.wondertwo.qqTremble;

import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
 * QQ抖一抖特效的自定义View动画实现
 * Created by wondertwo on 2016/3/17.
 */
public class QQTrembleAni extends Animation {
   
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
   
        t.getMatrix().setTranslate(
                (float) Math.sin(interpolatedTime * 50) * 8,
                (float) Math.sin(interpolatedTime * 50) * 8
                );// 50越大频率越高,8越小振幅越小
        super.applyTransformation(interpolatedTime, t);
    }
}

首先,所有的自定义View动画都要继承android.view.animation.Animation 抽象类,重写initialize()applyTransformation() 这两个方法。

  • initialize() :对一些变量进行初始化
  • applyTransformation() : 通过矩阵修改动画数值,从而控制动画实现过程

applyTransformation(float interpolatedTime, Transformation t) 在动画执行过程中不断调用,interpolatedTime 表示当前动画进行的时间与动画总时间的比值。Transformation t 传递当前动画对象,一般可以通过代码 android.graphics.Matrix matrix = t.getMatrix() 获得 Matrix 矩阵对象,再设置 Matrix 对象,一般要用到 interpolatedTime 参数,以此达到控制动画实现的结果

以下为QQ抖一抖动画的测试类Activity,:


import android.app.Activity;
import android.os
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值