令人头疼的自定义view与属性动画(亲测水不是一般的深)

很详细的博客,可惜太高深
https://blog.csdn.net/sailist/article/details/81609726#%E5%8E%9F%E7%90%86%E7%AF%87

画圆,xml中全包名引用 (那个activity用就写到布局里)
package com.baway.show;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import com.baway.R;

public class AnimatorCircle extends View {
    private int width;
    private int hight;
    private Paint paint;

    public AnimatorCircle(Context context) {
        super(context);
    }

    public AnimatorCircle(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public AnimatorCircle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint = new Paint();
        int color = Color.argb(120,85,85,55);
        Resources resources = getResources();
        int color1 = resources.getColor(R.color.green);

        paint.setColor(color1);
        canvas.drawCircle(width/2,280,300,paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
       width= getMeasuredWidth();
       hight = getMeasuredHeight();
    }
}

activity里操作组合动画

package com.baway;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;

import com.baway.show.AnimatorCircle;

public class ShowActivity extends AppCompatActivity {

    private AnimatorCircle yuan;
    private AnimatorSet set;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        yuan = findViewById(R.id.yuan);
        getSupportActionBar().hide();
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int height = displayMetrics.heightPixels;
        //缩放
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(yuan, "scaleX", 2, 1);
        //旋转
        ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(yuan, "rotation", 0, 360);
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(yuan, "alpha", 0, 1);
        ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(yuan, "translationY", height/2-yuan.getHeight()/2);
        set = new AnimatorSet();
        set.play(objectAnimator).with(objectAnimator1).with(objectAnimator2).with(objectAnimator3);
        set.setDuration(5000);
        set.start();
        yuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                set.start();
            }
        });
    }
}

你可能感兴趣的:(令人头疼的自定义view与属性动画(亲测水不是一般的深))