canvas circle

package com.example.appuser.testother.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;

import com.corelibs.utils.DisplayUtil;
import com.example.appuser.testother.R;

/**
 * Created by appuser on 2017/12/28.
 */

public class TestLoadingView extends View {

    private Ball redBall;
    private Ball blueBall;

    private float centerXBlue;
    private float centerXRed;

    public TestLoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    public TestLoadingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    public TestLoadingView(Context context) {
        super(context);
        init();
    }

    private Paint paintRed;
    private Paint paintBlue;
    private int min;
    private int max;


    private void init(){
        paintBlue=new Paint();
        paintBlue.setColor(getContext().getResources().getColor(R.color.ball_blue));
        paintBlue.setAntiAlias(true);
        paintBlue.setStyle(Paint.Style.FILL);

        paintRed=new Paint();
        paintRed.setColor(getContext().getResources().getColor(R.color.ball_red));
        paintRed.setAntiAlias(true);
        paintBlue.setStyle(Paint.Style.FILL);

        blueBall=new Ball();
        redBall =new Ball();
        max=DisplayUtil.dip2px(getContext(),15);
        min=DisplayUtil.dip2px(getContext(),8);
        initAnim();

        redBall.setRadius(max);
        blueBall.setRadius(min);
    }
    private AnimatorSet mAnimatorSet;
    private void initAnim() {
        mAnimatorSet = new AnimatorSet();
        //左側小球的大小變化分別爲: mid --> max --> mid -->min -->mid
        ValueAnimator mOneScale = ObjectAnimator.ofInt(
                redBall,
                "radius",
                max, min,max);
        mOneScale.setInterpolator(new LinearInterpolator());
        mOneScale.setRepeatCount(ValueAnimator.INFINITE);
        //左側小球的位移變化通過改變圓心
//        ValueAnimator mOneMove = ValueAnimator.ofFloat(-1, 0, 1, 0, -1);
//        mOneMove.setRepeatCount(ValueAnimator.INFINITE);
        mOneScale.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                float value = (float) valueAnimator.getAnimatedValue();
//                redBall.setCenterX(centerX + value * MOVE_DISTANCE);
                invalidate();
            }
        });


        //右側小球動畫
        ValueAnimator mTwoScale = ObjectAnimator.ofInt(
                blueBall,
                "radius",
                min,max,min);
        mTwoScale.setRepeatCount(ValueAnimator.INFINITE);
        mTwoScale.setInterpolator(new LinearInterpolator());
//        //右側小球動畫
//        ValueAnimator mTwoMove = ValueAnimator.ofFloat(1, 0, -1, 0, 1);
//        mTwoMove.setRepeatCount(ValueAnimator.INFINITE);
        mTwoScale.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                float value = (float) valueAnimator.getAnimatedValue();
//                mRightBall.setCenterX(centerX + value * MOVE_DISTANCE);

                invalidate();
            }
        });

        mAnimatorSet.setDuration(1200);
        mAnimatorSet.setInterpolator(new DecelerateInterpolator());
        mAnimatorSet.playTogether(mOneScale,  mTwoScale);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        int viewcenter= DisplayUtil.dip2px(getContext(),48)/2;
        centerXRed=DisplayUtil.dip2px(getContext(),15);
        centerXBlue=DisplayUtil.dip2px(getContext(),46)-DisplayUtil.dip2px(getContext(),8);
        redBall.setCenterX(centerXRed);
        blueBall.setCenterX(centerXBlue);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        Log.e("yzh","onDraw()"+blueBall.radius);
//        int viewcenter= DisplayUtil.dip2px(getContext(),60)/2;
        canvas.drawCircle(redBall.getCenterX(),getHeight()/2, redBall.radius,paintRed);
        canvas.drawCircle(blueBall.getCenterX(),getHeight()/2,blueBall.radius,paintBlue);
    }
    /**
     * 啓動動畫, 動畫默認啓動
     */
    public void startAnimation() {
        if (mAnimatorSet.isRunning()) {
            mAnimatorSet.cancel();
        }
        mAnimatorSet.start();
//        if (!mBlueScale.isRunning())
//            mBlueScale.start();
    }
    public void stopAnimation() {
        if (mAnimatorSet.isRunning())
            mAnimatorSet.cancel();
//        if (animBlue.isRunning())
//            animBlue.cancel();
    }
    class Ball {
        //小球的半徑
        private int radius;
        //小球的圓心點
        private float centerX;
        //小球的顏色
        private int color;

        public Ball() {
        }

        public Ball(int radius, float centerX, int color) {
            this.radius = radius;
            this.centerX = centerX;
            this.color = color;
        }

        public int getRadius() {
            return radius;
        }

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

        public float getCenterX() {
            return centerX;
        }

        public void setCenterX(float centerX) {
            this.centerX = centerX;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }
    }
}


package com.example.appuser.testother.widget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.appuser.testother.R;

/**
 * Created by appuser on 2017/12/22.
 */
@SuppressLint("InflateParams")
public class LoadingView extends Dialog {
    private static int theme = com.corelibs.R.style.dialog;
    private Context context;
    private ImageView iv_progress;
    private TextView tv_message;
   private TestLoadingView loadingView;

    public LoadingView(Context context) {
        super(context,theme);

        this.context=context;
        init();
        setMessage(null);

    }

    public LoadingView(Context context,String message){
        super(context,theme);

        this.context=context;
        init();
        setMessage(message);
    }

    private void init(){
        View contentView = LayoutInflater.from(context).inflate(R.layout.view_dialog, null);
        setContentView(contentView);
        setCanceledOnTouchOutside(false);
        iv_progress=findViewById(R.id.iv_progress);
        tv_message=findViewById(R.id.tv_message);
        Glide.with(context).load(R.mipmap.loading_tras)
                .diskCacheStrategy(DiskCacheStrategy.ALL).into(iv_progress);
        loadingView=findViewById(R.id.testloading);
    }

    private void setMessage(String message){
        if(message==null|| TextUtils.isEmpty(message)){
           if(tv_message.getVisibility()==View.VISIBLE){
               tv_message.setVisibility(View.GONE);
           }
           return;
        }

        if(tv_message.getVisibility()==View.GONE)
            tv_message.setVisibility(View.VISIBLE);
        tv_message.setText(message);
    }

    @Override
    public void show() {
        super.show();
        loadingView.startAnimation();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        loadingView.stopAnimation();
    }
}
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章