Android属性动画(ObjectAnimation)

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值