Android可设置结果的Loading

来源:互联网 发布:淘宝描述不符有赔偿吗 编辑:程序博客网 时间:2024/05/18 15:23

可以设置加载结果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);    }}



示例源码下载


原创粉丝点击