安卓属性动画

来源:互联网 发布:淘宝商品发布规则 编辑:程序博客网 时间:2024/05/17 09:00
安卓动画:  参照博客:http://blog.csdn.net/lmj623565791/article/details/38067475
逐帧动画  补间动画  属性动画
逐帧动画:Drawable Animation  ,FrameAnimation  对一个ImageView 不停的设置新的图片
补间动画:ViewAnimation ,也叫Tweened Animation  平移,缩放,透明,旋转  有局限性.
属性动画:PropertyAnimation  补间动画的升级 :通过动画的方式改变对象的属性

属性动画与补间动画最大的区别就是,控件通过属性动画改变位置后,控件的位置会随着改变,而补间动画控件还是在原地

相关类:(类似于ImageLoader的框架)
 动画的执行类 :ObjectAnimator,ValueAnimator
 AnimatorSet:动画集合
 AnimatorInflater:加载文件夹为animator的XML文件
 TypeEvaluator:类型估值,只要用于设置动画操作属性的值
 TimeInterpolator:时间插值器(也叫插补器). 设置动画加速减速.变化率
    属性动画执行流程:动画的执行类来设置动画操作的对象的属性.持续时间,开始和结束的属性值,时间插值等,然后系统会根据设置的参数,动态的变化对象的属性.
    
    PropertyName 有四个
    translationXY,rotation,alpha,scaleXY
    translationXY和scaleXY只有后缀加XY的方法,不表明XY方向的画会出现找不到方法的错误.    注意XY必须大写.我们将他们作为字符串当做参数传入方法里,由刚才的错误可以发现这个字符串是作为一个方法名来运行的.
    通过方法
    ObjectAnimator.ofFloat.setDuration.start
    简单实现
    也可以通过
    PropertyValuesHolder 属性持有者这个属性来设置同时播放俩种动画
    方法
     ObjectAnimator.ofPropertyValuesHolder.setDuration.start这个方法参数是一个可变数组
    示例代码:
    case R.id.image:
                //TODO
                ObjectAnimator//3个参数:作用对象  动画名  动画需要配置的值:可变数组
                        .ofFloat(mImage, "translationX", 30f,90f)
//                        .ofFloat(mImage, "translationX", 90f,30f)
                        .setDuration(1500)
                        .start();
                        
    之前的补间动画有AnimationSet实现多种动画组合播放,而属性动画也有这个功能就是AnimatorSet
    
    AnimatorSet通过Play().with().after().before()通过这些方法来实现 动画和动画的先后顺序,以及同时播放功能.
    
    修改背景色:闪光灯效果  方法名:backgroundColor字符串
    ObjectAnimator.ofInt  至于颜色则是通过可变参数实现

    关于属性动画插值器的笔记
    Interpolator,在安卓1.0的时候就已经出现Interpolator接口了,可以翻译成插值器,用于计算补间动画的变化速率. 而在安卓3.0的时候引入了属性动画,同时新增了TimeInterpolator接口,这就使得
    Interpolator的实现类可以直接拿到属性动画中使用.
    主要的实现类
    AccelerateDecelerateInterpolator   在动画开始与介绍的地方速率改变比较慢,在中间的时候加速
    AccelerateInterpolator                     在动画开始的地方速率改变比较慢,然后开始加速
    AnticipateInterpolator                      开始的时候向后然后向前甩
    AnticipateOvershootInterpolator     开始的时候向后然后向前甩一定值后返回最后的值
    BounceInterpolator                          动画结束的时候弹起
    CycleInterpolator                             动画循环播放特定的次数,速率改变沿着正弦曲线
    DecelerateInterpolator                    在动画开始的地方快然后慢
    LinearInterpolator                            以常量速率改变
    OvershootInterpolator                      向前甩一定值后再回到原来位置
    
    使用:一个方法  animator.setInterpolator(new BounceInterpolator());
    实例代码:
      ObjectAnimator animator = ObjectAnimator
                        .ofFloat(mImage, "rotationY", 0.0f, 360f);
                animator.setInterpolator(new BounceInterpolator());
                animator.setDuration(5000)
                        .start();
    属性动画课堂笔记
    ObjectAnimator属性动画特点:动画效果会改变控件的位置,且开启动画的是动画对象,而不是控件对象.
    注意:
       如果你想让你的App与众不同,请用自定义控件,
       吊炸天+狂拽=android动画+自定义动画
    
    Android3.0之后才出现的新特性.最低兼容API11

    
    在XML定义动画类属性,浮点型小数,直接写小数即可,不用再带f
    提示:控件位移的参数在XML文件里有所不同,不过更简单,不用再特意去定义参照物的属性,直接根据值,区分俩种方式.
    一种:一整个屏幕为参照物,在XML文件属性定义值是int%p
    一种:以控件自身大小为参照物,
    使用方式:
    示例代码
     Animator animator1 = AnimatorInflater.loadAnimator(MainActivity.this, R.animator.set);
                animator1.setTarget(mImage);
                animator1.start();

    ValueAnimator 实现动画
    无需设置操作的属性,这就是和ObjectAnimator的区别
    好处:不需要操作对象的属性,一定要有getter,setter方法,你可以根据当前动画的计算值,来操作任何属性
    
    示例代码
    private void Vertical() {//heightPixels - mImage.getHeight()
        final ValueAnimator animator = ValueAnimator.ofFloat(0,300 );
        animator.setTarget(mImage);
        animator.setDuration(2000).start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                mImage.setTranslationY((float)animator.getAnimatedValue());
            }
        });
    }



/*********************************/

public class MainActivity extends AppCompatActivity implements View.OnClickListener {    int duration = 500;    private ImageView mImageJia;    private ImageView mImageIc1;    private ImageView mImageIc2;    private RelativeLayout mRela;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();    }    private void initView() {        mImageIc1 = (ImageView) findViewById(R.id.imageIc1);        mImageIc2 = (ImageView) findViewById(R.id.imageIc2);        mImageJia = (ImageView) findViewById(R.id.imageJia);        mImageJia.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.imageJia:                Toast.makeText(MainActivity.this, "222", Toast.LENGTH_SHORT).show();                StartAnimation();                break;        }    }    private boolean isChecked = false;    private void StartAnimation() {        if (isChecked == false) {//        PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("TranslationX", 0f, 150f);            ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageIc1, "TranslationX", 0f, 150f);            ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageIc2, "TranslationX", 0f, 300f);            ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageJia, "Rotation", 0f, 360f);            //添加自由落体效果插值器            animator1.setInterpolator(new BounceInterpolator());            animator2.setInterpolator(new BounceInterpolator());            animator3.setInterpolator(new BounceInterpolator());            //启动动画            animator1.setDuration(duration).start();            animator2.setDuration(duration).start();            animator3.setDuration(duration).start();            animator3.addListener(new Animator.AnimatorListener() {                @Override                public void onAnimationStart(Animator animation) {                }                @Override                public void onAnimationEnd(Animator animation) {                    mImageJia.setImageResource(R.drawable.jian);                }                @Override                public void onAnimationCancel(Animator animation) {                }                @Override                public void onAnimationRepeat(Animator animation) {                }            });            isChecked = true;        } else {            ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageIc1, "TranslationX",  150f,0f);            ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageIc2, "TranslationX", 300f,0f );            ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageJia, "Rotation", 0f, 720f);            //添加自由落体效果插值器//            animator1.setInterpolator(new CycleInterpolator(1));//            animator2.setInterpolator(new CycleInterpolator(5));//            animator3.setInterpolator(new CycleInterpolator(10));            //启动动画            animator1.setDuration(duration).start();            animator2.setDuration(duration).start();            animator3.setDuration(duration).start();            animator3.addListener(new Animator.AnimatorListener() {                @Override                public void onAnimationStart(Animator animation) {                }                @Override                public void onAnimationEnd(Animator animation) {                    mImageJia.setImageResource(R.drawable.jia);                }                @Override                public void onAnimationCancel(Animator animation) {                }                @Override                public void onAnimationRepeat(Animator animation) {                }            });            isChecked = false;        }    }}