Android可设置结果的Loading

可以设置加载结果Loading控件

效果图:

 

 

源码

 

package com.example.zloadingdemo;

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

/**
 * Created by Zqil on 2017/5/20.
 */
public class ZLoadingView extends View {
    private RectF mRectF = null;
    private int line1_x = 0;
    private int line1_y = 0;
    private int line2_x = 0;
    private int line2_y = 0;
    //画笔宽度
    private int strokeWidth = 6;

    private int mState = 0;
    public static final int STATE_LOADING = 0;
    public static final int STATE_SUCCESS = 1;
    public static final int STATE_FAILED = -1;

    private final String colorLoading = "#00BAAF";
    private final String colorSuccess = "#00BAAF";
    private final String colorFailure = "#FF8C28";

    public ZLoadingView(Context context) {
        super(context);
    }

    public ZLoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ZLoadingView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    boolean increasing = true;
    int start = 0;
    int sweep = 0;
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        /**
         * initialize paint
         */
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(strokeWidth);
        paint.setAntiAlias(true);
        if (mState == STATE_LOADING) {
            paint.setColor(Color.parseColor(colorLoading));
        } else if (mState == STATE_SUCCESS) {
            paint.setColor(Color.parseColor(colorSuccess));
        } else if (mState == STATE_FAILED) {
            paint.setColor(Color.parseColor(colorFailure));
        }

        //圆弧半径
        int radius = getWidth() / 2 - strokeWidth;
        if (mRectF == null) {
            mRectF = new RectF(strokeWidth, strokeWidth, getWidth() - strokeWidth, getWidth() - strokeWidth);
        }
        
        /**
         * paint loading
         * */
        if (mState == STATE_LOADING) {
            if (sweep >= 340 && increasing) {
                increasing = false;
            } else if (sweep <= 20 && !increasing) {
                increasing = true;
            }
            if (increasing) {
                sweep+=10 ;
                start+=5;
                if (start>360) {
                    start=start-360;
                }
            } else {
                start+=10 ;
                sweep-=5;
            }
            canvas.drawArc(mRectF, start, sweep, false, paint);
            postInvalidate();
        }



        /**
         * paint result
         * */

        boolean postPaint = true;
        int line1_startX = 0;
        int line1_startY = 0;
        int line1_width = 0;
        int line2_width = 0;
        int line2_startX = 0;
        int line2_startY = 0;
        if (mState == STATE_SUCCESS) {
            line1_startX = getWidth() * 3 / 10;
            line1_startY = getWidth() / 2;
            line1_width = radius / 3;
            line2_width = radius * 3 / 5;
            line2_startX = line1_startX + line1_width;
            line2_startY = line1_startY + line1_width;
        } else if (mState == STATE_FAILED) {
            line1_startX = getWidth() * 3 / 10;
            line1_startY = line1_startX;
            line1_width = getWidth() - line1_startX * 2;
            line2_width = line1_width;
            line2_startX = line1_startX;
            line2_startY = line1_startY + line1_width;
        } else {
            postPaint = false;
        }
        if (postPaint) {
            canvas.drawArc(mRectF, 0, 360, false, paint);
            //paint line1
            canvas.drawLine(line1_startX, line1_startY, line1_startX + line1_x, line1_startY + line1_y, paint);
            //paint line2
            canvas.drawLine(line2_startX, line2_startY, line2_startX + line2_x, line2_startY - line2_y, paint);
            if(line1_x <= line1_width) {
                line1_x++;
                line1_y++;
            } else if (line2_x < line2_width) {
                line2_x++;
                line2_y++;
            } else {
                postPaint = false;
            }
            if (postPaint) {
                postInvalidate();
            }
        }
    }
    
    public void setState(int state) {
    	if (this.mState != state) {
            line1_x = 0;
            line1_y = 0;
            line2_x = 0;
            line2_y = 0;
		}
        this.mState = state;
        postInvalidate();
    }
    

    public void showLoading() {
        setState(STATE_LOADING);
    }
    public void showSuccess() {
        setState(STATE_SUCCESS);
    }
    public void showFailure() {
        setState(STATE_FAILED);
    }
}

 

 

 

 

 

 

示例源码下载

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值