Android MotionLayout 五角星

Android MotionLayout 五角星_第1张图片

有偏差





    







    

        

        

            
                
                

                
                
                
                

            

            

            

            



        

    

    
        
    

    
        
    

package com.as.app2;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathEffect;
import android.graphics.PathMeasure;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewParent;
import android.view.animation.AccelerateDecelerateInterpolator;

/**
 * -----------------------------
 * Created by zqf on 2019/8/19.
 * ---------------------------
 */
public class XingxingView extends View {


    private Paint paint;
    private Path path;
    private ValueAnimator mAnimator;


//     private int x;
//    private int y;
//
//    public void setX(int x) {
//        this.x = x;
//    }
//
//    public void setY(int y) {
//        this.y = y;
//    }

//    public XingxingView(Context context, AttributeSet attrs) {
//        this(context, attrs, 0);
//    }

    public XingxingView(Context context, AttributeSet attrs, int defStyleAttr,int x,int y) {
        super(context, attrs, defStyleAttr);


        //首先吧 五个点 确定了
        /**
         * 0.5y
         * 1.0x,0.5y
         * 0.25x ,0.8y
         * 0.5x,0.3y
         * 0.8x,0.8y
         * 0.5y
         */

        path = new Path();
        path.moveTo(0, (float) (0.5 * y));
        path.lineTo(x, (float) (0.5 * y));
        path.lineTo((float) (x * 0.25), (float) (0.8 * y));
        path.lineTo((float) (x * 0.5), (float) (0.3 * y));
        path.lineTo((float) (x * 0.8), (float) (0.8 * y));
        path.lineTo(0, (float) (0.5 * y));
        path.close();

        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);

        PathEffect pathEffect = new DashPathEffect(new float[]{5, 5, 5, 5}, 1);
        paint.setPathEffect(pathEffect);

        PathMeasure mPathMeasure = new PathMeasure(path, false);
        final float length = mPathMeasure.getLength();

        mAnimator = ValueAnimator.ofFloat(1, 0);
        mAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        mAnimator.setDuration(3000);

        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float fraction = (float) valueAnimator.getAnimatedValue();
                DashPathEffect mEffect = new DashPathEffect(new float[]{length, length}, fraction * length);
                paint.setPathEffect(mEffect);
                invalidate();
            }
        });
//        mAnimator.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        canvas.drawPath(path, paint);
    }

    public void start() {
        mAnimator.start();
    }

}
 private XingxingView xingxingView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main4);

        MotionLayout motion_layout = findViewById(R.id.motion_layout);

        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int widthPixels = outMetrics.widthPixels;
        int heightPixels = outMetrics.heightPixels;

        xingxingView = new XingxingView(this, null, 0, widthPixels, heightPixels - 60);
        motion_layout.addView(xingxingView);
        motion_layout.setTransitionListener(new TransitionAdapter() {
            @Override
            public void onTransitionCompleted(MotionLayout motionLayout, int currentId) {
                super.onTransitionCompleted(motionLayout, currentId);
                xingxingView.start();
            }
        });

    }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(Android MotionLayout 五角星)