动画之Evaluator
Evaluator就是通过监听器拿到当前动画对对应的具体数值,作用在于从插值器返回的数值进行转换成对应的数值.简单来说就是转换器 Evaluator返回值的类型更加动画中值决定的,所以在使用的时候注意数据类型
tv = (TextView)findViewById(R.id.tv); findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ValueAnimator animator = ValueAnimator.ofInt(0,300); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
int curValue = (Integer)animation.getAnimatedValue();
tv.layout(tv.getLeft(),curValue,tv.getRight(),curValue+tv.getHeight());
}
});
animator.setDuration(1000);
animator.setEvaluator(new ReverseEvaluator());
animator.start();
}
});
import android.animation.TypeEvaluator; public class ReverseEvaluator implements TypeEvaluator<Integer> {
public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
int startInt = startValue;
return (int)(endValue - fraction * (endValue - startInt));
}
}
tv = (TextView)findViewById(R.id.tv);
findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ValueAnimator animator = ValueAnimator.ofInt(0xffffff00,0xff0000ff);
animator.setEvaluator(new ArgbEvaluator());//颜色值过滤转换
animator.setDuration(3000); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
int curValue = (Integer) animation.getAnimatedValue();
tv.setBackgroundColor(curValue);
}
}); animator.start();
}
tv = (TextView)findViewById(R.id.tv); findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ValueAnimator animator = ValueAnimator.ofObject(new CharEvaluator(), new Character('A'), new Character('Z'));
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
char text = (Character) animation.getAnimatedValue();
tv.setText(String.valueOf(text));
}
});
animator.setDuration(10000);
animator.setInterpolator(new AccelerateInterpolator());
animator.start();
}
});
抛物动画
ballImg = (ImageView) findViewById(R.id.ball_img); findViewById(R.id.start_anim).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth();
int height = wm.getDefaultDisplay().getHeight(); ValueAnimator animator = ValueAnimator.ofObject(new FallingBallEvaluator(),new Point(0,0),new Point(width*2/3,height*2/3));
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
mCurPoint = (Point) animation.getAnimatedValue();
ballImg.layout(mCurPoint.x, mCurPoint.y, mCurPoint.x + ballImg.getWidth(), mCurPoint.y + ballImg.getHeight());
}
});
animator.setDuration(2000);
animator.start();
}
});
public class FallingBallEvaluator implements TypeEvaluator<Point> {
private Point point = new Point();
public Point evaluate(float fraction, Point startValue, Point endValue) {
point.x = (int)(startValue.x + fraction *(endValue.x - startValue.x)); if (fraction*2<=1){
point.y = (int)(startValue.y + fraction*2*(endValue.y - startValue.y));
}else {
point.y = endValue.y;
}
return point;
}
}
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#ff0000"/>
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
tools:context="com.loaderman.customviewdemo.MainActivity"> <ImageView
android:id="@+id/ball_img"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/cicle"/> <Button
android:id="@+id/start_anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始动画"/> </LinearLayout>
动画之Evaluator的更多相关文章
- android.animation(2) - ValueAnimator的 Interpolator 和 Evaluator
一.插值器 插值器,也叫加速器:有关插值器的知识,我在<Animation动画详解(二)——Interpolator插值器>中专门讲过,大家可以先看看这篇文章中各个加速器的效果.这里再讲一 ...
- android.animation(5) - PropertyValuesHolder与Keyframe(转)
前几篇给大家讲了ValueAnimator.ObjectAnimator的知识,讲解了它们ofInt(),ofFloat(),ofObject()函数的用法.细心的同学可能会注意到,ValueAnim ...
- [转]Android自定义控件三部曲系列完全解析(动画, 绘图, 自定义View)
来源:http://blog.csdn.net/harvic880925/article/details/50995268 一.自定义控件三部曲之动画篇 1.<自定义控件三部曲之动画篇(一)—— ...
- Android属性动画源代码解析(超详细)
本文假定你已经对属性动画有了一定的了解,至少使用过属性动画.下面我们就从属性动画最简单的使用开始. ObjectAnimator .ofInt(target,propName,values[]) .s ...
- Android Animation(动画)
前言 Android 平台提供实现动画的解决方案(三种) 一.3.0以前,android支持两种动画: (1)Frame Animation:顺序播放事先做好的图像,与gif图片原理类似,是一种逐帧动 ...
- Android之动画的学习(转载)
Android动画学习笔记-Android Animation 3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...
- Android Property Animation动画
3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中又引入了一个新的动画系统:property animation,这三 ...
- Android动画学习笔记-Android Animation
Android动画学习笔记-Android Animation 3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...
- 安卓开发_浅谈Android动画(四)
Property动画 概念:属性动画,即通过改变对象属性的动画. 特点:属性动画真正改变了一个UI控件,包括其事件触发焦点的位置 一.重要的动画类及属性值: 1. ValueAnimator 基本属 ...
随机推荐
- C++——virtual function
无论是pure virtual还是impure virtual,都允许子类override他.但是真两种方式还是有一点差别,如果是pure virtual,那么父类是十分强烈希望子类override他 ...
- 为0LTP选择RDMBS时,你都需要考虑哪些?
我们经常需要为自己的OLTP(事务/运营)数据库选择适合的RDBMS(关系型数据库管理系统).虽然通过编写可移植的SQL可以暂时避免进行这样的选择,但迟早要做出这样的选择,至少需要进行这样的尝试(比如 ...
- vue-cli2 和vue-cli3
vue-cli2 和vue-cli3 https://www.cnblogs.com/zhanvo/p/10963776.html <!DOCTYPE html> <html lan ...
- P1273 有线电视网[分组背包+树形dp]
题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节点. 从转播站到转播站以及从 ...
- 《The one!》团队作业五:团队项目需求改进与系统设计
项目 内容 作业所属课程 所属课程 作业要求 作业要求 团队名称 < The One !> 作业学习目标 (1)掌握面向对象需求分析方法:(2)学习软件系统总体结构和数据库逻辑结构设计,学 ...
- Maximum Average Subarray II
Description Given an array with positive and negative numbers, find the maximum average subarray whi ...
- 007——转载——C#将字符串转换为整型的三种方法的总结
(一)转载——C#将字符串转换为整型的三种方法的总结 在C#中,要将一个字符串或浮点数转换为整数,基本上有三种方法: (1)使用强制类型转换:(int)浮点数 (2)使用Convert.ToInt32 ...
- 使用Keras训练神经网络备忘录
小书匠深度学习 文章太长,放个目录: 1.优化函数的选择 2.损失函数的选择 2.2常用的损失函数 2.2自定义函数 2.1实践 2.2将损失函数自定义为网络层 3.模型的保存 3.1同时保持结构和权 ...
- 【概率论】4-7:条件期望(Conditional Expectation)
title: [概率论]4-7:条件期望(Conditional Expectation) categories: - Mathematic - Probability keywords: - Exp ...
- js new call apply bind 的 原理
new new 做了什么事?1. 以 Object.protoype 为原型创建一个新对象 2. 以新对象为 this,执行函数的 [[call]] 3. 如果 [[call]] 的返回值是对象,那么 ...