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();
    }
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值