objectAnimation与ValueAnimation的使用
在给控件做动画的时候,我一直都是使用animation的基本动画来给控件界面添加动画,能简单肯定不会用复杂的,我记得在我刚开始的时候要做一个效果,就是要按钮移动然后结束以后固定到结束的点,我记得当时的我是通过给动画添加监听器来判断动画结束的时候,然后让动态设置控件的位置,但是随着对android接触的越来越多,我渐渐的觉得自己当时的方法是相当的笨。
下来就说下objectAnimation跟 ValueAnimation吧:
objectAnimation:
这个子类ValueAnimator支持动画的目标对象上的属性。这类带参数的构造函数定义的目标对象动画以及属性的名称,将动画。然后确定适当的设置/获取函数内部和动画将调用这些函数作为必要的动画属性。
也就是这个动画可以修改view的属性,还能固定他的显示位置,那么就很简单了
ValueAnimation
这个类提供了一个简单的计时引擎运行动画动画计算值和设置在目标对象。
有一个所有动画使用的定时脉冲。它运行在一个自定义处理程序以确保属性更改在UI线程上发生。
默认情况下,ValueAnimator使用非线性插值,通过AccelerateDecelerateInterpolator类,它加速,减慢的动画。这种行为可以改变callingsetInterpolator(TimeInterpolator)。
这个是说动画的所有属性都可以让我们控制,使用更加灵活。
其实学习这个是我在想要画一个抛物线的时候,当我画完了以后我就想抛物线的动画挺好看,然后就研究了下,总结了四种抛物线的动画实现。
(1)
public static int mCount;
/**
* 整个抛物线
* @param imageView
*/
public static void startAnimation(final View view) {
mCount = 300;
Keyframe[] keyframes = new Keyframe[mCount];
final float keyStep = 1f / (float) mCount;
float key = keyStep;
for (int i = 0; i < mCount; ++i) {
keyframes[i] = Keyframe.ofFloat(key, i+ 1);
key += keyStep;
}
PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe("translationX", keyframes);
key = keyStep;
for (int i = 0; i < mCount; ++i) {
keyframes[i] = Keyframe.ofFloat(key, -getY(i + 1));
key += keyStep;
}
PropertyValuesHolder pvhY = PropertyValuesHolder.ofKeyframe("translationY", keyframes);
ObjectAnimator yxBouncer = ObjectAnimator.ofPropertyValuesHolder(view, pvhY, pvhX).setDuration(3000);
yxBouncer.setInterpolator(new BounceInterpolator());
yxBouncer.start();
}
<pre name="code" class="java"> public static final float a = -1f / 75f;
/**
* 这里是根据三个坐标点{(0,0),(300,0),(150,300)}计算出来的抛物线方程
*
* @param x
* @return
*/
public static float getY(float x) {
if (x>300) {
x= 300;
}
return a * x * x + 4 * x;
}
(2)
/**
* 半个抛物线,从上向下
* @param imageView
*/
public static void starthalfAnimation(final View view) {
mCount = 150 ;
Keyframe[] keyframes = new Keyframe[mCount];
final float keyStep = 1f / (float) mCount;
float key = keyStep;
for (int i = 0; i < mCount; ++i) {
keyframes[i] = Keyframe.ofFloat(key, i+ 150);
key += keyStep;
}
PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe("translationX", keyframes);
key = keyStep;
for (int i = 0; i < mCount; ++i) {
keyframes[i] = Keyframe.ofFloat(key, -getY(i + 150));
key += keyStep;
}
PropertyValuesHolder pvhY = PropertyValuesHolder.ofKeyframe("translationY", keyframes);
ObjectAnimator yxBouncer = ObjectAnimator.ofPropertyValuesHolder(view, pvhY, pvhX).setDuration(3000);
yxBouncer.setInterpolator(new BounceInterpolator());
yxBouncer.start();
}
(3)//也是抛物线
public static void parabolaAnimation(View view){
PropertyValuesHolder holderY = PropertyValuesHolder.ofFloat("translationY", 0.0f,180f);
PropertyValuesHolder holderX = PropertyValuesHolder.ofFloat("translationX", 0.0f,180f);
ObjectAnimator.ofPropertyValuesHolder(view,holderX,holderY).setDuration(2000).start();
}
(4)
public static void ParabolaValueAnimation(final View view){
ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.addListener(new AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
/*ViewGroup group = (ViewGroup) view.getParent();
if (group != null) {
group.removeView(view);
}*/
}
@Override
public void onAnimationCancel(Animator animation) {
}
});
valueAnimator.setDuration(3000);
valueAnimator.setObjectValues(new PointF(0, 0));
valueAnimator.setInterpolator(new BounceInterpolator());
valueAnimator.setEvaluator(new TypeEvaluator<PointF>() {
/**
*
* @param fraction 从0。0 - 1.0
* @param startValue
* @param endValue
* @return
*/
@Override
public PointF evaluate(float fraction, PointF startValue,
PointF endValue) {
PointF pointF= new PointF();
Log.e("point", fraction+"");
pointF.x = 200 * fraction * 3/2;
pointF.y = 0.5f * 200 * (fraction * 3) * (fraction * 3)/2;
return pointF;
}
});
valueAnimator.start();
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
PointF pointF = (PointF)animation.getAnimatedValue();
view.setTranslationX(pointF.x);
view.setTranslationY(pointF.y);
}
});
}
我实验了多种动画,可以看看。下一篇: TV端开发遇坑随记
推荐阅读
-
windows下ipython的安装与使用详解
-
Java 关键字 volatile 的理解与正确使用
-
PHP中的empty、isset、isnull的区别与使用实例
-
Android 中自定义ContentProvider与ContentObserver的使用简单实例
-
解析C#自定义控件的制作与使用实例的详解
-
Android编程使用GestureDetector实现简单手势监听与处理的方法
-
iOS9与XCode7中不能使用http连接的快速解决办法
-
详解Swift中对C语言接口缓存的使用以及数组与字符串转为指针类型的方法
-
.NET Framework中定时器timer的单线程与多线程使用讲解
-
使用C#获取远程图片 Form用户名与密码Authorization认证的实现