自定义view加属性动画

//自定义view类
public class MyView extends View {

    private Paint paint;
    private int mWidth;
    private int mHeight;


    public MyView(Context context) {
        this(context,null);
    }

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

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint=new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int cx = mHeight / 2;
        int cy = mWidth / 2;
        int radius = mWidth / 2;
        canvas.drawCircle(cx,cy,radius,paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mHeight = getMeasuredHeight();
        mWidth=getMeasuredWidth();
    }
}
 
main_activity布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.dell.animatordonghua.MainActivity">

    <com.example.dell.animatordonghua.MyView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="#000000"
        android:layout_gravity="center"
        android:id="@+id/my_view"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:layout_marginTop="200dp"
        android:orientation="horizontal">
        <Button
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:text="缩放"
            android:id="@+id/btn_suofang"/>
        <Button
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:text="选转"
            android:id="@+id/btn_xuanzhuan"/>
        <Button
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:text="平移"
            android:id="@+id/btn_pingyi"/>
        <Button
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:text="渐变效果"
            android:id="@+id/btn_jianbian"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/zuhe"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Animator组合动画"/>
        <Button
            android:id="@+id/valueyi"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="value平移"/>
    </LinearLayout>
</LinearLayout>
Activity类
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private MyView myView;
    private Button btn_xuanzhuan;
    private Button btn_pingyi;
    private Button btn_jianbian;
    private Button btn_suofang;
    private Button zuhe;
    private Button zuheyi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myView = findViewById(R.id.my_view);
        btn_xuanzhuan=findViewById(R.id.btn_xuanzhuan);
        btn_pingyi= findViewById(R.id.btn_pingyi);
        btn_jianbian=findViewById(R.id.btn_jianbian);
        btn_suofang=findViewById(R.id.btn_suofang);
        zuhe = findViewById(R.id.zuhe);
        zuheyi = findViewById(R.id.valueyi);
        zuhe.setOnClickListener(this);
        zuheyi.setOnClickListener(this);
        btn_xuanzhuan.setOnClickListener(this);
        btn_pingyi.setOnClickListener(this);
        btn_jianbian.setOnClickListener(this);
        btn_suofang.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //缩放
            case R.id.btn_suofang:
                ObjectAnimator scaleX = ObjectAnimator.ofFloat(myView, "scaleX", 1f, 2f, 1f);
                scaleX.setDuration(3000);
                scaleX.start();
                break;
                //旋转
            case R.id.btn_xuanzhuan:
                ObjectAnimator rotation = ObjectAnimator.ofFloat(myView, "rotation", 0f, 360f);
                rotation.setDuration(3000);
                rotation.start();
                break;
                //平移
            case R.id.btn_pingyi:
                float s = myView.getTranslationX();
                ObjectAnimator translationX = ObjectAnimator.ofFloat(myView, "translationX", s, -500f, +100);
                translationX.setDuration(3000);
                translationX.start();
                break;
                //渐变
            case R.id.btn_jianbian:
                ObjectAnimator alpha = ObjectAnimator.ofFloat(myView, "alpha", 1f, 0f, 1f);
                 alpha.setDuration(3000);
                 alpha.start();
                 break;
Animator组合动画
            case R.id.zuhe:
                float a = myView.getTranslationX();
                ObjectAnimator pingyi = ObjectAnimator.ofFloat(myView, "translationX", a, -500f, +100);
                ObjectAnimator jianbian = ObjectAnimator.ofFloat(myView, "alpha", 1f, 0f, 1f);
                AnimatorSet animatorSet = new AnimatorSet();
                animatorSet.playTogether(jianbian,pingyi);
                animatorSet.setDuration(3000);
                animatorSet.start();
                break;
ValueAnimator动画不管用
            case R.id.valueyi:
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 1);
                valueAnimator.setDuration(3000);
                valueAnimator.start();
                break;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值