ObjectAnimator(使用)

原文链接:https://blog.youkuaiyun.com/xiaochuanding/article/details/73290917
此文章作为笔记,自己私下学习使用 如果看到 还请移步原文观摩

ObjectAnimator继承自ValueAnimator,所以ValueAnimator所能使用的方法,ObjectAnimator都可以使用,ObjectAnimator同时也重写了几个方法,比如:ofInt() ofFloat()等

//透明度动画
ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",1,0,1);  
animator.setDuration(2000);  
animator.start(); 

//旋转动画:围绕x轴旋转
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"rotationX",0,270,0);  
animator.setDuration(2000);  
animator.start();

//旋转动画:围绕y轴旋转
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"rotationY",0,180,0);  
animator.setDuration(2000);  
animator.start();

//旋转动画:围绕z轴旋转
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"rotation",0,270,0);  
animator.setDuration(2000);  
animator.start();  

//平移动画:在x轴上平移
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationX", 0, 200, -200,0);  
animator.setDuration(2000);  
animator.start(); 

//平移动画:在y轴上平移
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationY", 0, 200, -100,0);  
animator.setDuration(2000);  
animator.start(); 

//缩放动画:在x轴缩放
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "scaleX", 0, 3, 1);  
animator.setDuration(2000);  
animator.start();

//缩放动画:在y轴上缩放
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "scaleY", 0, 3, 1);  
animator.setDuration(2000);  
animator.start(); 

ObjectAnimator改变控件背景颜色
以TextView为类,改变其背景颜色,关键方法是继承View的控件都实现了这个方法

 ObjectAnimator objectAnimator = ObjectAnimator.ofInt(tv,"backgroundColor",0xfff10f0f,0xff0f94f1,0xffeaf804,0xfff92a0f);
        objectAnimator.setDuration(2000);
        objectAnimator.setEvaluator(new ArgbEvaluator());
        objectAnimator.start();

自定义view

public class CircleView extends View {
    private Ponit mCurrentPoint = new Ponit();
    private Paint mPiant ;
    private int mScreenWidth;//屏幕宽度
    public CircleView(Context context) {
        this(context,null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPiant = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPiant.setColor(Color.RED);
        mPiant.setStyle(Paint.Style.FILL);
        mScreenWidth = ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mCurrentPoint != null){
            canvas.drawCircle(mScreenWidth/2,getY()+getPaddingTop(),mCurrentPoint.getRadius(),mPiant);
        }
    }
    public int getPointRadius(){  //这个get方法只作为默认值出现在这里的
        return 50;  
    }  
    public void setPointRadius(int radius){//这里set方法必须和ObjectAnimator中的属性值对应
        mCurrentPoint.setRadius(radius);
        invalidate();
    }
}

自定义point

public class Ponit {
    private int mRadius;

    public Ponit() {
    }

    public Ponit(int mRadius) {
        this.mRadius = mRadius;
    }

    public int getRadius() {
        return mRadius;
    }

    public void setRadius(int mRadius) {
        this.mRadius = mRadius;
    }
}


一个圆点变大变小动画
```java
 ObjectAnimator objectAnimator = ObjectAnimator.ofInt(mCircleView,"pointRadius",0,200,100,200,50);
        objectAnimator.setDuration(1000);
        objectAnimator.start();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值