Android 属性动画(Property Animation) 全然解析 (上)
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38067475
1、概述
Android提供了几种动画类型:View Animation 、Drawable Animation 、Property Animation 。View Animation相当简单,只是仅仅能支持简单的缩放、平移、旋转、透明度主要的动画。且有一定的局限性。
比方:你希望View有一个颜色的切换动画。你希望能够使用3D旋转动画;你希望当动画停止时。View的位置就是当前的位置;这些View Animation都无法做到。
这就是Property Animation产生的原因。本篇博客具体介绍Property Animation的使用方法。
至于Drawable Animation。嗯,略~
2、相关API
Property Animation故名思议就是通过动画的方式改变对象的属性了,我们首先须要了解几个属性:
Duration动画的持续时间,默认300ms。
Time interpolation:时间差值,乍一看不知道是什么。可是我说LinearInterpolator、AccelerateDecelerateInterpolator,大家一定知道是干嘛的了,定义动画的变化率。
Repeat count and behavior:反复次数、以及反复模式;能够定义反复多少次;反复时从头開始。还是反向。
Animator sets: 动画集合,你能够定义一组动画。一起运行或者顺序运行。
Frame refresh delay:帧刷新延迟,对于你的动画,多久刷新一次帧;默觉得10ms,但终于依赖系统的当前状态。基本不用管。
相关的类
ObjectAnimator 动画的运行类。后面具体介绍
ValueAnimator 动画的运行类。后面具体介绍
AnimatorSet 用于控制一组动画的运行:线性,一起,每一个动画的先后运行等。
AnimatorInflater 用户载入属性动画的xml文件
TypeEvaluator 类型估值。主要用于设置动画操作属性的值。
TimeInterpolator 时间插值,上面已经介绍。
总的来说。属性动画就是。动画的运行类来设置动画操作的对象的属性、持续时间,開始和结束的属性值,时间差值等,然后系统会依据设置的參数动态的变化对象的属性。
3、ObjectAnimator实现动画
之所以选择ObjectAnimator为第一个~~是由于。这个实现最简单~~一行代码,秒秒钟实现动画。以下看个样例:
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/id_container" > <ImageView
android:id="@+id/id_ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/mv"
android:scaleType="centerCrop"
android:onClick="rotateyAnimRun"
/> </RelativeLayout>
非常easy,就一张妹子图片~
Activity代码:
package com.example.zhy_property_animation; import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View; public class ObjectAnimActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.xml_for_anim);
} public void rotateyAnimRun(View view)
{
ObjectAnimator//
.ofFloat(view, "rotationX", 0.0F, 360.0F)//
.setDuration(500)//
.start();
} }
效果:
是不是一行代码就能实现简单的动画~~
对于ObjectAnimator
1、提供了ofInt、ofFloat、ofObject。这几个方法都是设置动画作用的元素、作用的属性、动画開始、结束、以及中间的随意个属性值。
当对于属性值,仅仅设置一个的时候,会觉得当然对象该属性的值为開始(getPropName反射获取)。然后设置的值为终点。假设设置两个,则一个为開始、一个为结束~~~
动画更新的过程中,会不断调用setPropName更新元素的属性,全部使用ObjectAnimator更新某个属性,必须得有getter(设置一个属性值的时候)和setter方法~
2、假设你操作对象的该属性方法里面。比方上例的setRotationX假设内部没有调用view的重绘。则你须要自己依照以下方式手动调用。
anim.addUpdateListener(new AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
// view.postInvalidate();
// view.invalidate();
}
});
3、看了上面的样例,由于设置的操作的属性仅仅有一个。那么假设我希望一个动画能够让View既能够缩小、又能够淡出(3个属性scaleX,scaleY,alpha)。仅仅使用ObjectAnimator咋弄?
想法是不是非常不错,可能会说使用AnimatorSet啊。这一看就是一堆动画塞一起运行,可是我偏偏要用一个ObjectAnimator实例实现呢~以下看代码:
public void rotateyAnimRun(final View view)
{
ObjectAnimator anim = ObjectAnimator//
.ofFloat(view, "zhy", 1.0F, 0.0F)//
.setDuration(500);//
anim.start();
anim.addUpdateListener(new AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
float cVal = (Float) animation.getAnimatedValue();
view.setAlpha(cVal);
view.setScaleX(cVal);
view.setScaleY(cVal);
}
});
}
把设置属性的那个字符串。随便写一个该对象没有的属性,就是无论~~咱们仅仅须要它依照时间插值和持续时间计算的那个值,我们自己手动调用~
效果:
这个样例就是想说明一下,有时候换个思路不要被API所约束,利用部分API提供的功能也能实现好玩的效果~~~
比方:你想实现抛物线的效果,水平方向100px/s,垂直方向加速度200px/s*s ,咋实现呢~~能够自己用ObjectAnimator试试~
4、事实上还有更简单的方式,实现一个动画更改多个效果:使用propertyValuesHolder
public void propertyValuesHolder(View view)
{
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f,
0f, 1f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 1f,
0, 1f);
PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 1f,
0, 1f);
ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(1000).start();
}
4、ValueAnimator实现动画
和ObjectAnimator使用方法非常相似,简单看一下用view垂直移动的动画代码:
public void verticalRun(View view)
{
ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight
- mBlueBall.getHeight());
animator.setTarget(mBlueBall);
animator.setDuration(1000).start();
}
给你的感觉是不是。坑爹啊。这和ValueAnimator有毛线差别~可是细致看,你看会发现。没有设置操作的属性~~也就是说,上述代码是没有不论什么效果的,没有指定属性~
这就是和ValueAnimator的差别之处:ValueAnimator并没有在属性上做操作,你可能会问这样有啥优点?我岂不是还得手动设置?
优点:不须要操作的对象的属性一定要有getter和setter方法,你能够自己依据当前动画的计算值,来操作不论什么属性。记得上例的那个【我希望一个动画能够让View既能够缩小、又能够淡出(3个属性scaleX,scaleY,alpha)】吗?事实上就是这么个使用方法~
实例:
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/id_container" > <ImageView
android:id="@+id/id_ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bol_blue" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="verticalRun"
android:text="垂直" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="paowuxian"
android:text="抛物线" /> </LinearLayout> </RelativeLayout>
左上角一个小球,底部两个button~我们先看一个自由落体的代码:
/**
* 自由落体
* @param view
*/
public void verticalRun( View view)
{
ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight
- mBlueBall.getHeight());
animator.setTarget(mBlueBall);
animator.setDuration(1000).start();
// animator.setInterpolator(value)
animator.addUpdateListener(new AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
mBlueBall.setTranslationY((Float) animation.getAnimatedValue());
}
});
}
与ObjectAnimator不同的就是我们自己设置元素属性的更新~尽管多了几行代码。可是貌似提高灵活性~
以下再来一个样例。假设我希望小球抛物线运动【实现抛物线的效果,水平方向100px/s,垂直方向加速度200px/s*s 】,分析一下,貌似仅仅和时间有关系。可是依据时间的变化。横向和纵向的移动速率是不同的。我们该咋实现呢?此时就要重写TypeValue的时候了,由于我们在时间变化的同一时候。须要返回给对象两个值,x当前位置,y当前位置:
代码:
/**
* 抛物线
* @param view
*/
public void paowuxian(View view)
{ ValueAnimator valueAnimator = new ValueAnimator();
valueAnimator.setDuration(3000);
valueAnimator.setObjectValues(new PointF(0, 0));
valueAnimator.setInterpolator(new LinearInterpolator());
valueAnimator.setEvaluator(new TypeEvaluator<PointF>()
{
// fraction = t / duration
@Override
public PointF evaluate(float fraction, PointF startValue,
PointF endValue)
{
Log.e(TAG, fraction * 3 + "");
// x方向200px/s ,则y方向0.5 * 10 * t
PointF point = new PointF();
point.x = 200 * fraction * 3;
point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);
return point;
}
}); valueAnimator.start();
valueAnimator.addUpdateListener(new AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation)
{
PointF point = (PointF) animation.getAnimatedValue();
mBlueBall.setX(point.x);
mBlueBall.setY(point.y); }
});
}
能够看到,由于ofInt,ofFloat等无法使用,我们自己定义了一个TypeValue,每次依据当前时间返回一个PointF对象,(PointF和Point的差别就是x,y的单位一个是float,一个是int;RectF,Rect也是)PointF中包括了x,y的当前位置~然后我们在监听器中获取,动态设置属性:
效果图:
有木有两个铁球同一时候落地的感觉~~对。我应该搞两个球~~ps:物理公式要是错了,就当没看见哈
自己定义TypeEvaluator传入的泛型能够依据自己的需求,自己设计个Bean。
好了,我们已经分别解说了ValueAnimator和ObjectAnimator实现动画。二者差别;怎样利用部分API。自己更新属性实现效果;自己定义TypeEvaluator实现我们的需求;可是我们并没有讲怎样设计插值,事实上我觉得把,这个插值默认的那一串实现类够用了~~非常少,会自己去设计个超级变态的~嗯~所以:略。
5、监听动画的事件
对于动画,一般都是一些辅助效果,比方我要删除个元素,我可能希望是个淡出的效果,可是终于还是要删掉,并非你透明度没有了,还占着位置。所以我们须要知道动画怎样结束。
所以我们能够加入一个动画的监听:
public void fadeOut(View view)
{
ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, "alpha", 0.5f); anim.addListener(new AnimatorListener()
{ @Override
public void onAnimationStart(Animator animation)
{
Log.e(TAG, "onAnimationStart");
} @Override
public void onAnimationRepeat(Animator animation)
{
// TODO Auto-generated method stub
Log.e(TAG, "onAnimationRepeat");
} @Override
public void onAnimationEnd(Animator animation)
{
Log.e(TAG, "onAnimationEnd");
ViewGroup parent = (ViewGroup) mBlueBall.getParent();
if (parent != null)
parent.removeView(mBlueBall);
} @Override
public void onAnimationCancel(Animator animation)
{
// TODO Auto-generated method stub
Log.e(TAG, "onAnimationCancel");
}
});
anim.start();
}
这样就能够监听动画的開始、结束、被取消、反复等事件~可是有时候会觉得,我仅仅要知道结束即可了,这么长的代码我不能接收,那你能够使用AnimatorListenerAdapter
anim.addListener(new AnimatorListenerAdapter()
{
@Override
public void onAnimationEnd(Animator animation)
{
Log.e(TAG, "onAnimationEnd");
ViewGroup parent = (ViewGroup) mBlueBall.getParent();
if (parent != null)
parent.removeView(mBlueBall);
}
});
AnimatorListenerAdapter继承了AnimatorListener接口,然后空实现了全部的方法~
效果图:
animator还有cancel()和end()方法:cancel动画马上停止,停在当前的位置。end动画直接到终于状态。
6、AnimatorSet的使用
实例:
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/id_container" > <ImageView
android:id="@+id/id_ball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/bol_blue" /> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" > <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="togetherRun"
android:text="简单的多动画Together" /> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="playWithAfter"
android:text="多动画按次序运行" /> </LinearLayout> </RelativeLayout>
继续玩球~
代码:
package com.example.zhy_property_animation; import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView; public class AnimatorSetActivity extends Activity
{
private ImageView mBlueBall; @Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.anim_set); mBlueBall = (ImageView) findViewById(R.id.id_ball); } public void togetherRun(View view)
{
ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",
1.0f, 2f);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",
1.0f, 2f);
AnimatorSet animSet = new AnimatorSet();
animSet.setDuration(2000);
animSet.setInterpolator(new LinearInterpolator());
//两个动画同一时候运行
animSet.playTogether(anim1, anim2);
animSet.start();
} public void playWithAfter(View view)
{
float cx = mBlueBall.getX(); ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",
1.0f, 2f);
ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",
1.0f, 2f);
ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBlueBall,
"x", cx , 0f);
ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBlueBall,
"x", cx); /**
* anim1,anim2,anim3同一时候运行
* anim4接着运行
*/
AnimatorSet animSet = new AnimatorSet();
animSet.play(anim1).with(anim2);
animSet.play(anim2).with(anim3);
animSet.play(anim4).after(anim3);
animSet.setDuration(1000);
animSet.start();
}
}
写了两个效果:
第一:使用playTogether两个动画同一时候运行,当然还有playSequentially依次运行~~
第二:假设我们有一堆动画,怎样使用代码控制顺序,比方1,2同一时候;3在2后面。4在1之前等~就是效果2了
有一点注意:animSet.play().with();也是支持链式编程的。可是不要想着狂点。比方 animSet.play(anim1).with(anim2).before(anim3).before(anim5); 这样是不行的,系统不会依据你写的这一长串来决定先后的顺序。所以麻烦你依照上面样例的写法,多写几行:
效果图:
好了,由于篇幅~~关于属性动画还有点知识:
1、xml文件创建属性动画
2、布局动画
3、View的animate方法等。
那就考虑写到下一篇了。只是核心的功能就这些了~~
对了,假设使用11以下的SDK 。请导入nineoldandroids动画库。使用方法基本全然一致~
Android 属性动画(Property Animation) 全然解析 (上)的更多相关文章
- Android动画主要包含补间动画(Tween)View Animation、帧动画(Frame)Drawable Animation、以及属性动画Property Animation
程序运行效果图: Android动画主要包含补间动画(Tween)View Animation.帧动画(Frame)Drawable Animation.以及属性动画Property Animatio ...
- Android 动画——属性动画Property Animation
Android在3.0之前只提供了两种动画:View Animation .Drawable Animation .也就是我们在<Android 动画——Frame Animation与Twee ...
- 使用属性动画 — Property Animation
属性动画,就是通过控制对象中的属性值产生的动画.属性动画是目前最高级的2D动画系统. 在API Level 11中添加.Property Animation号称能控制一切对象的动画,包括可见的和不可见 ...
- 使用属性动画 — Property Animation
属性动画,就是通过控制对象中的属性值产生的动画.属性动画是目前最高级的2D动画系统. 在API Level 11中添加.Property Animation号称能控制一切对象的动画,包括可见的和不可见 ...
- 属性动画(Property Animation)
属性动画系统是一个强大的可以绘制任意事物.你可以定义改变物体属性的动画,不管它是不是在屏幕上.属性动画随着时间的推移去改变物体的属性.如果要让某个事物动起来,你只需指定该事物的某个属性,如物体的坐标. ...
- 属性动画Property Animation
ViewPropertyAnimation 使用方式:View.animate() 后跟 translationX() 等方法,动画会自动执行. 注意translationX实现是调用对应的set方法 ...
- 【转】android 属性动画之 ObjectAnimator
原文网址:http://blog.csdn.net/feiduclear_up/article/details/39255083 前面一篇博客讲解了 android 简单动画之 animtion,这里 ...
- Android属性动画完全解析(上),初识属性动画的基本用法
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/43536355 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系 ...
- Android属性动画完全解析(上)
Android属性动画完全解析(上) 转载:http://blog.csdn.net/guolin_blog/article/details/43536355 在手机上去实现一些动画效果算是件比较炫酷 ...
- Android属性动画完全解析
转载:http://blog.csdn.net/guolin_blog/article/details/43536355 在手机上去实现一些动画效果算是件比较炫酷的事情,因此Android系统在一开始 ...
随机推荐
- [转]深入ASP.NET MVC之九:Ajax支持
本文转自:http://www.cnblogs.com/yinzixin/archive/2012/12/22/2824384.html 目前前端页面和服务端进行Ajax交互大多采用的都是jQuery ...
- 生成清除某个数据库下的所有表的SQL语句
方法1:重建库和表 用mysqldump --no-data把建表SQL导出来,然后drop database再create database,执行一下导出的SQL文件: 方法2:生成清空所有表的SQ ...
- 两个input可能会用到的小方法
1.一个普通的input元素,在不被 form包裹的时候,如何跳转或搜索 var oInput = document.getElementsByTagName('input')[0]; oInput. ...
- PAT1021Deepset Root
题意: 连通则输出最深点.第一步找某个点的最深的,然后从这个最深的点查找其他最深点,做并集. 不连通则输出连通图个数. #include<iostream> #include<cst ...
- redis的安装、启动、主从配置,以及.Net下StackExchange.Redis的使用
开门见山,Linux下配个环境真是苦逼死了,这里记录一下,囧 一.环境 服务端:Ubuntu16.04 LTS(虚拟机,redis官方没有window发布版本,而且在Linux下运行更稳定) 客户端: ...
- mongo 3.4分片集群系列之三:搭建分片集群--哈希分片 + 安全
这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...
- javascript按钮点击事件问题
今天弄了个小测试,结果发现了点问题. 就是有一个按钮的点击事件,页面加载时候没反应,只有F12启用开发人员工具的时候才有反应.后来反复测试发现名字起的不太合理 function onclick(){ ...
- jquery.ajax之beforeSend方法使用介绍
常见的一种效果,在用ajax请求时,没有返回前会出现前出现一个转动的loading小图标或者“内容加载中..”,用来告知用户正在请求数据.这个就可以用beforeSend方法来实现. 下载demo:a ...
- iOS,Core Animation--负责视图的复合功能
简介 UIKit API UIKit是一组Objective-C API,为线条图形.Quartz图像和颜色操作提供Objective-C 封装,并提供2D绘制.图像处理及用户接口级别的动画. ...
- 苹果和虫子问题C++
如果需要转载,请注明出处:http://www.cnblogs.com/wongyi/p/8205305.html 最近在给小朋友补奥数课,顺便看了几道题目,觉得写出来会比较集中,整理一下哈哈哈. 问 ...