摘要: Android 自定义属性动画&Camera动画
1.相关知识点

对于Androi的帧动画,可以制作gif图片,有时为了能够动态的生成帧动画,就得需要使用代码构建了

AnimationDrawable类中使用 addFrame用来添加帧。

AnimationDrawable类中使用 start来启动动画。

AnimationDrawable类中使用 stop来停止动画。

  1. 当移动位置不是相对于ParentView或者Window时,补间动画只实现了View图像位置的改变,但控件并没有发生位移

    说明:当属性动画移动后,如果不会到原来的位置,那么点击新的位置,将接受不到Click事件,点击原来的位置可以接收到点击事件

  2. 补间动画通过不断的调用OnDraw方法来进行UI的绘制,而属性动画一般只调用ViewGroup进行绘制

  3. 属性动画不会主动恢复到原来的状态,而是一直保持新的状态,直到下一次改变

  4. 属性动画可以使用playToggther,play..with,play...[width]... after,playSequentaily进行动画的控制,使用起来非常方便

  5. 属性动画可以通过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动画的更多相关文章

  1. Android之滑屏动画和自定义控件

    滑屏动画 在Android系统中,通过手势识别切换界面时,通常会在界面切换时加入动画,以提高用户的体验效果,这种动画一般都采用平移动画,下一个界面进入时,上一个界面移除屏幕. 图中标识的均为左上角坐标 ...

  2. Android View的滑动 动画

    [scrollTo/scrollBy] //控件内的文字会移动,但是控件本身不会移动,而且移动到控件之外之后,文字也就看不见了 if(v.equals(button2)){ button2.scrol ...

  3. android 帧动画,补间动画,属性动画的简单总结

      帧动画——FrameAnimation 将一系列图片有序播放,形成动画的效果.其本质是一个Drawable,是一系列图片的集合,本身可以当做一个图片一样使用 在Drawable文件夹下,创建ani ...

  4. android 后台代码设置动画

    1.设置旋转动画 final RotateAnimation animation =new RotateAnimation(0f,360f,Animation.RELATIVE_TO_SELF, 0. ...

  5. android中设置Animation 动画效果

    在 Android 中, Animation 动画效果的实现可以通过两种方式进行实现,一种是 tweened animation 渐变动画,另一种是 frame by frame animation ...

  6. Android使用XML做动画UI

    在Android应用程序,使用动画效果,能带给用户更好的感觉.做动画可以通过XML或Android代码.本教程中,介绍使用XML来做动画.在这里,介绍基本的动画,如淡入,淡出,旋转等. 效果: htt ...

  7. Android开发之三种动画

    转载:http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html http://www.lightskystreet.com/2 ...

  8. android学习日记22--Animation动画简介

    Animation动画主要有两种:帧动画(Frame Animation)和补间动画(Tween Animation).补间动画主要包括对位置.角度.尺寸等属性的变化,而帧动画则是通过若干帧图片轮流切 ...

  9. Android Property Animation 物业动画

    效果图:   Property Animation介绍: 出生在sdk3.0,是利用了View所拥有的属性,进行一系列的操作. 比方一个View有什么样的setAbc的属性,那么理论上就能够设置它. ...

随机推荐

  1. 如何将thick provision lazy zeroed的VMDK文件转换为thick provision eager zeroed?

    详细步骤在此: Enabling clustering features for an existing virtual disk by converting in place(1035823) ht ...

  2. OkHttp 官方Wiki之【使用案例】

    原文位置:https://github.com/square/okhttp/wiki/Recipes Recipes 食谱/知识点清单 We've written some recipes that ...

  3. 升级iOS10后http网页定位失效解决方案

    最近我们在做项目时遇到这样一个新问题,用户在升级 iOS10 后,在 http 下使用 geolocation api 会报错,控制台输出 [blocked] Access to geolocatio ...

  4. [置顶] IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)

    首先了解一下CGContextRef: An opaque type that represents a Quartz 2D drawing environment. Graphics Context ...

  5. URAL 1698

    题目大意:统计位数不大于n的自守数的个数. KB     64bit IO Format:%I64d & %I64u 数据规模:1<=n<=2000. 理论基础: 自守数:参见链接 ...

  6. CentOS 7.0 安装 ZCS 8.6.0

    简介 Zimbra的核心产品是Zimbra协作套件(Zimbra Collaboration Suite,简称ZCS).除了它的核心功能是电子邮件和日程安排服务器,当然还包括许多其它的功能,就象是下一 ...

  7. 【转】十个经典的C开源项目代码

    原文: http://blog.51cto.com/chinalx1/2143904 --------------------------------------------------------- ...

  8. Android指令处理流程源代码追踪

    1.拨号界面输入*#06#.显示IMEI号,这是怎么出来的? 2.怎样高速的找出Android平台中的指令? 1. 在DialpadFragment中的EditText注冊一个Textchanged的 ...

  9. centos+apache+mod_ssl

    参考网站:配置Apache建立openssl证书实现SSL访问:http://blog.51yip.com/apachenginx/958.html用ca工具生成证书的方法:http://hi.bai ...

  10. Jquery attr("checked") 返回checked或undefined 获取选中失效

    $('#cb').attr('checked'); 返回的是checked或者是undefined,不是原来的true和false了,有关此问题的解决方法如下: <input type='che ...