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);
			}
		});
    }
  我实验了多种动画,可以看看。

Demo:http://download.csdn.net/detail/u012808234/9095419

相关文章
|
机器学习/深度学习 编解码 算法
DL之DeepLabv3:DeepLab v3和DeepLab v3+算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略
DL之DeepLabv3:DeepLab v3和DeepLab v3+算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略
DL之DeepLabv3:DeepLab v3和DeepLab v3+算法的简介(论文介绍)、架构详解、案例应用等配图集合之详细攻略
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16874 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
7天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1228 2
|
13天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2324 7
|
18天前
|
弹性计算 人工智能 API
Codex超详细入门指南(下载·安装·登录·上手,全版本覆盖,2026最新版)
建议收藏本教程,2026 全年各版本更新随时回来查阅。如果你在搭建过程中遇到任何问题,欢迎结合 阿里云工单系统 提交技术支持请求,或在 阿里云开发者社区 搜索 Codex 相关话题交流。 祝你编码愉快,效率翻倍!
3795 22