Android 自定义属性动画&Camera动画
1.相关知识点
对于Androi的帧动画,可以制作gif图片,有时为了能够动态的生成帧动画,就得需要使用代码构建了
AnimationDrawable类中使用 addFrame用来添加帧。
AnimationDrawable类中使用 start来启动动画。
AnimationDrawable类中使用 stop来停止动画。
当移动位置不是相对于ParentView或者Window时,补间动画只实现了View图像位置的改变,但控件并没有发生位移
说明:当属性动画移动后,如果不会到原来的位置,那么点击新的位置,将接受不到Click事件,点击原来的位置可以接收到点击事件
补间动画通过不断的调用OnDraw方法来进行UI的绘制,而属性动画一般只调用ViewGroup进行绘制
属性动画不会主动恢复到原来的状态,而是一直保持新的状态,直到下一次改变
属性动画可以使用playToggther,play..with,play...[width]... after,playSequentaily进行动画的控制,使用起来非常方便
属性动画可以通过ObjectAnimator和PropertyValueHolder进行动态控制,增加了动画的灵活性
2.pivot:X和piovtY中心点
中心点对所有动画属性都起作用,scale(参见QQ侧滑),translate,rotate
中心点描述了动画的发展方向
另外对于补间动画的理解中容易出现错误的地方,更正如下:
RotateAnimation ra = new RotateAnimation(fromDegrees, toDegrees, pivotX, pivotY)
pivotX,pivotY当数值大于1时表示的是实际像素
RotateAnimation ra = new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue)
pivotX,pivotY当数值大于1时表示的是比例位置
3.Animation自定义动画
3.1继承Animation自定义动画
public class CustomAnimation extends Animation {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
t.getMatrix().setTranslate((float) (Math.sin(10*interpolatedTime)*50), 0);
}
}
3.2使用ValueAnimator结合监听器自定义动画
final ShapeHolder ball5 = balls.get(4);
ValueAnimator valueAnimator5 = ValueAnimator.ofFloat(0f,
getHeight() - ball5.getHeight());
valueAnimator5.setDuration(500);
valueAnimator5.addUpdateListener(new AnimatorUpdateListener() {
// ValueAnimator需要自己在监听处理中设置对象参数
@Override
public void onAnimationUpdate(ValueAnimator animation) {
// 用animation.getAnimatedValue()得到当前的属性值,设置进动画对象中
ball5.setY((Float) animation.getAnimatedValue());
// 记得要刷新View否则不会调用重新绘制
invalidate();
}
});
3.3使用TypeEvaluator自定义动画
public class Point {
private float x;
private float y;
public Point(float x, float y) {
this.x = x;
this.y = y;
}
public void setX(float x){
this.x = x
}
public void setY(float y){
this.y = y;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
}
public class PointEvaluator implements TypeEvaluator{
@Override
public Object evaluate(float fraction, Object startValue, Object endValue) {
Point startPoint = (Point) startValue;
Point endPoint = (Point) endValue;
float x = startPoint.getX() + fraction * (endPoint.getX() - startPoint.getX());
float y = startPoint.getY() + fraction * (endPoint.getY() - startPoint.getY());
Point point = new Point(x, y);
return point;
}
}
public class MyTaget{
private Point point = new Point(0f,0f);
public void setPoint(Point p){
this.point = point;
}
public Point getPoint(){
return this.point;
}
}
Point point1 = new Point(0, 0);
Point point2 = new Point(300, 300);
ValueAnimator anim = ValueAnimator.ofObject(new PointEvaluator(), point1, point2);
anim.setTarget(new MyTarget());
anim.setDuration(5000);
anim.start();
或如下使用
ObjectAnimator.ofObject(Object target, String propertyName, TypeEvaluator evaluator, Object... values)
4.通关过扩展原有属性方式自定义动画
(由于属性动画的属性必须具有setter与getter,对于一些特别的属性,需要使用代理)
public class WrapperView{
private View targetView;
public WrapperView(View targetView){
this.targetView = targetView;
}
public void setWidth(int width){
targetView.getLayoutParams().width = width;
targetView.requestLayout();
}
public int getWidth(){
return targetView.getLayoutParams().width;
}
}
使用方法
WrapperView wrapper = new WrapperView(mLinearLayout);
ObjectAnimator.ofInt(wrapper,"width",300).setDuration(3000).start();
Android 自定义动画Animation 使用Camera实现3D动画效果,这里的Camera不是相机,而是场景动画,意味着有导演
public class MyAnimation extends Animation {
int mCenterX,mCenterY;
Camera camera = new Camera();
public MyAnimation() {
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
Matrix matrix = t.getMatrix();
camera.save();
camera.translate(0f, 0f, (1300 - 1300*interpolatedTime));
camera.rotateY(360*interpolatedTime);
camera.getMatrix(matrix);
matrix.preTranslate(-mCenterX, -mCenterY);
matrix.postTranslate(mCenterX,mCenterY);
camera.restore();
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
//初始化中间坐标
mCenterX = width/2;
mCenterY = height/2;
setDuration(2000);
setFillAfter(true);
setInterpolator(new LinearInterpolator());
}
}
Android 自定义属性动画&Camera动画的更多相关文章
- Android之滑屏动画和自定义控件
滑屏动画 在Android系统中,通过手势识别切换界面时,通常会在界面切换时加入动画,以提高用户的体验效果,这种动画一般都采用平移动画,下一个界面进入时,上一个界面移除屏幕. 图中标识的均为左上角坐标 ...
- Android View的滑动 动画
[scrollTo/scrollBy] //控件内的文字会移动,但是控件本身不会移动,而且移动到控件之外之后,文字也就看不见了 if(v.equals(button2)){ button2.scrol ...
- android 帧动画,补间动画,属性动画的简单总结
帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...
- android 后台代码设置动画
1.设置旋转动画 final RotateAnimation animation =new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF, 0. ...
- android中设置Animation 动画效果
在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...
- Android使用XML做动画UI
在Android应用程序,使用动画效果,能带给用户更好的感觉.做动画可以通过XML或Android代码.本教程中,介绍使用XML来做动画.在这里,介绍基本的动画,如淡入,淡出,旋转等. 效果: htt ...
- Android开发之三种动画
转载:http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html http://www.lightskystreet.com/2 ...
- android学习日记22--Animation动画简介
Animation动画主要有两种:帧动画(Frame Animation)和补间动画(Tween Animation).补间动画主要包括对位置.角度.尺寸等属性的变化,而帧动画则是通过若干帧图片轮流切 ...
- Android Property Animation 物业动画
效果图: Property Animation介绍: 出生在sdk3.0,是利用了View所拥有的属性,进行一系列的操作. 比方一个View有什么样的setAbc的属性,那么理论上就能够设置它. ...
随机推荐
- [置顶] 利用Global.asax的Application_BeginRequest 实现url 重写 无后缀
利用Global.asax的Application_BeginRequest 实现url 重写 无后缀 <%@ Application Language="C#" %> ...
- linux驱动杂项
linux驱动 结构体中的逗号 http://zhouyang340.blog.163.com/blog/static/3024095920123495051607/ 下面我们看一个例子,Linux- ...
- Java从零开始学二十四(集合工具类Collections)
一.Collections简介 在集合的应用开发中,集合的若干接口和若干个子类是最最常使用的,但是在JDK中提供了一种集合操作的工具类 —— Collections,可以直接通过此类方便的操作集合 二 ...
- MongodbUtil
import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; import com.mongodb.MongoClien ...
- webpack 生命周期
1.插件 可以安装lifecycle-webpack-plugin 插件来查看生命周期信息. 2.webpack流程(生命周期图) 地址:https://img.alicdn.com/tps/TB1G ...
- 重置outlook 2010
1.进入 D:\program files\mirosoft office\ioffice14 2.outlook /importprf .\.prf 3.账号问题可以-->控制面板--> ...
- 修改终端下vim的PopupMenu选种项的背景颜色
我平常比较喜欢使用终端下的 VIM,最方便的就是随时可以使用ctrl+z切换到终端下执行命令, 然后再通过fg切换回 VIM.如果再有个透明效果,那就更赞了.不过最近换了一个配色ron 后, 有个比较 ...
- 【线程篇】stop() 和suspend()
1.为什么不推荐用 stop()和 suspend() stop这个方法将终止所有未结束的方法,包括run方法.当一个线程停止时候,他会立即释放所有他锁住对象上的锁.这会导致对象处于不一致的状态.假如 ...
- 【独立开发人员er Cocos2d-x实战 001】csb文件导出和载入
使用cocos studio进行资源文件导出: 然后在cocosproject中进行载入csb文件: auto myLayout = CSLoader::createNode("/res/ ...
- 在notepad++中运行python代码
#在notepad++中运行python代码 ''' 1.安装插件pyNPP, 2.允许插件pyNPP中的第一个和第二个选项即可,如果代码过少代码执行一闪而过,可能无法看到,可加入少量sleep时间即 ...