欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

自定义view圆加属性动画

程序员文章站 2024-03-22 21:57:10
...

自定义view圆加属性动画
从左上到右下
第一步自定义view

package views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by wangyajie on 2017/10/25.
 */

public class ViewYuan extends View {
    private Context context;
    private int rimWitdth=10;//宽

    public ViewYuan(Context context) {
        super(context);
    }
    public ViewYuan(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint rimPrint=new Paint();//画笔
        RectF rectF=new RectF(5,5,50,50);//
        rimPrint.setAntiAlias(true);
        //        rimPrint.setStyle(Paint.Style.STROKE);//空心圆
        rimPrint.setStyle(Paint.Style.FILL);//实心圆
        rimPrint.setStrokeWidth(rimWitdth);
        rimPrint.setColor(Color.RED);//圆的颜色
        canvas.drawArc(rectF,360,360,false,rimPrint);
    }
}

第二步引用到布局中
我的是由点击事件开始的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始动画"/>

    <views.ViewYuan
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

第三步使用属性动画

package fragments;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import views.ViewYuan;

/**
 * Created by wangyajie on 2017/10/25.
 */

public class F2 extends Fragment {

    private View view;
    Button btn;
    ViewYuan viewYuan;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.f2, null, false);
        //初始化控件
        initView();
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //初始化数据
                initData();
            }
        });

        return view;
    }

    /**
     * 获取数据
     */
    private void initData() {
        final ValueAnimator animator=new ValueAnimator();
        animator.setDuration(3000);
        animator.setObjectValues(new PointF(1,1));
        animator.setInterpolator(new LinearInterpolator());
        animator.setEvaluator(new TypeEvaluator() {
            @Override
            public Object evaluate(float v, Object o, Object t1) {
                PointF point=new PointF();
                point.x=200*v*3;
                point.y=0.56f*200*(v*3)*(v*3);
                return point;
            }
        });
        animator.start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                PointF point= (PointF) animator.getAnimatedValue();
                viewYuan.setX(point.x);
                viewYuan.setY(point.y);
            }
        });
    }
    /**
     * 获取控件
     */
    private void initView(){
        viewYuan= (ViewYuan) view.findViewById(R.id.view);
        btn= view.findViewById(R.id.btn);
    }
}