欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 下载进度条, 自定义加载进度条,loading动画

程序员文章站 2022-03-18 20:17:58
...

1、自定义loadingline动画

/**
 * 作者:created by meixi
 * 邮箱:[email protected]
 * 日期:2018/9/6 10
 */

public class LoadingLine extends View {

    private Paint paint;
    private Paint backPaint;
    private Paint textPaint;
    private boolean init = false;
    private ValueAnimator valueAnimator;
    private float param = 0;
    private int length = 0;

    public LoadingLine(Context context) {
        super(context);
        init();
    }


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

    private void init() {
        paint = new Paint();
        paint.setColor(Color.BLUE);
        paint.setStrokeWidth(25);
        backPaint = new Paint();
        backPaint.setColor(Color.RED);
        backPaint.setStrokeWidth(25);
        textPaint = new Paint();
        textPaint.setColor(Color.WHITE);
        textPaint.setTextSize(10);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (!init) {
//            start();
            init = true;
        }
        int width = getWidth();
        int height = getHeight();
        canvas.drawLine(0, height / 2, width, height / 2, backPaint);
        canvas.drawLine(0, height / 2, length * width / 100, height / 2, paint);
        if (length<100){
            canvas.drawText(length + "", length * width / 100 - 22, height / 2 + 6, textPaint);

        }else {
            canvas.drawText(length + "", length * width / 100 - 42, height / 2 + 6, textPaint);

        }
//        if (valueAnimator.isRunning()) {
//            param = (float) valueAnimator.getAnimatedValue();
//            setLength((int) param);
//        }
    }

    /**
     * 也可在外部手动控制进度条,自行关闭valueAnimator
     *
     * @param param
     */
    public void setLength(int param) {
        this.length = param;
        invalidate();
    }

    private void start() {
        if (valueAnimator == null) {
            valueAnimator = ValueAnimator.ofFloat(0, 100);
            valueAnimator.setInterpolator(new LinearInterpolator());
            valueAnimator.setDuration(6000);
            valueAnimator.start();
        } else {
            valueAnimator.start();
        }
        postDelayed(new Runnable() {
            @Override
            public void run() {
//                start();
                invalidate();
            }
        }, valueAnimator.getDuration());
        invalidate();
    }


}

 

2、activity调用

布局文件

<com.administrator.tests.LoadingLine
    android:id="@+id/myloadline"
    android:layout_width="match_parent"
    android:layout_height="5dp" />

初始化

    private boolean running;
    private LoadingLine loadingLine;
    int i = 0;

    static final int REFRESH_COMPLETE = 0X1112;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case REFRESH_COMPLETE:
                    if (i==100){
                        running=false;
                    }
                    Log.i("lgq","...===="+i);
                    loadingLine.setLength(i++);//主要加载进度方法
//todo...
                    break;
            }
        }
    };

线程加载进度

 loadingLine =(LoadingLine)findViewById(R.id.myloadline);

new Thread() {
    @Override
    public void run() {
        super.run();
        running = true;
        while (running) {
            mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 0);
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}.start();