If we don’t have the guts to try anything, what’s the meaning of life.
——如果我们任何事情都没有勇气去尝试,人生还有什么意义。
1. 自定义View动画
这里给出一个模仿QQ客户端的抖一抖特效:
这里窗口抖动的特效代码为:
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