Android属性动画(ObjectAnimation)

来源:互联网 发布:中国大数据产业联盟 编辑:程序博客网 时间:2024/04/29 08:13
public void objectAnimation(View v){        LinearLayout llProgress = (LinearLayout) findViewById(R.id.ll_progress);        final float scale = getResources().getDisplayMetrics().density;        float distance = (255 * scale + 0.5f);        ViewWrapper wrapper = new ViewWrapper(llProgress);        ObjectAnimator animator = ObjectAnimator.ofInt(wrapper,"width",(int)distance);        animator.setDuration(4000);        animator.start();    }
public void ivAnimation(final View v){        //单个属性        ObjectAnimator.ofFloat(v,"rotationX",0f,180f).setDuration(1000).start();        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(v, "yjs", 1f, 0f);        objectAnimator.setDuration(1000);        objectAnimator.start();        //多个属性动画方法一        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                float scale = animation.getAnimatedFraction();                v.setAlpha(scale);                v.setScaleX(scale);                v.setScaleY(scale);            }        });        //多个属性动画方法二        PropertyValuesHolder phx = PropertyValuesHolder.ofFloat("alpha",1f,0f);        PropertyValuesHolder phy = PropertyValuesHolder.ofFloat("scaleX",1f,0f);        PropertyValuesHolder phz = PropertyValuesHolder.ofFloat("scaleY",1f,0f);        ObjectAnimator.ofPropertyValuesHolder(v,phx,phy,phz).setDuration(3000).start();        //动画定义在xml文件中        Animator animator = AnimatorInflater.loadAnimator(this,R.animator.scale);        animator.setTarget(v);        animator.start();    }
//如果某个属性没有get和set方法需要写一个wrapper类将get set封装    private  class ViewWrapper{        private View target;        public ViewWrapper(View v){            target= v;        }        public int getWidth(){            return target.getLayoutParams().width;        }        public void setWidth(int width){            target.getLayoutParams().width = width;            target.requestLayout();        }    }

xml文件

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"    android:ordering="together">    <objectAnimator        android:duration="1000"        android:propertyName="scaleX"        android:valueFrom="1.0"        android:valueTo="2.0"        android:valueType="floatType" >    </objectAnimator>    <objectAnimator        android:duration="1000"        android:propertyName="scaleY"        android:valueFrom="1.0"        android:valueTo="2.0"        android:valueType="floatType"></objectAnimator></set>

参考链接一
参考链接二

0 0