美文网首页
SurfaceView实现进度条

SurfaceView实现进度条

作者: 王学岗 | 来源:发表于2019-08-09 17:47 被阅读0次
    package com.example.testprogreebar;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.PixelFormat;
    import android.graphics.RectF;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    import java.util.concurrent.Executors;
    import java.util.concurrent.ScheduledExecutorService;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @author writing
     * @time 2019/8/5 12:37
     * @note ${TODO}
     */
    public class TestProgreebar extends SurfaceView implements SurfaceHolder.Callback {
    
        private int viewWidth;
        private int viewHeight;
        private Paint paint1;
        private Paint paint2;
        private ScheduledExecutorService scheduledExecutorService;
        private boolean stop = false;
        private float progress;
        private int speed = 1;
        private SurfaceHolder holder;
        private RectF r2;
    
        public TestProgreebar(Context context) {
            this(context, null);
        }
    
        public TestProgreebar(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }
    
        public TestProgreebar(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            inits();
        }
    
        private void inits() {
            holder = this.getHolder();
            holder.addCallback(this);
            paint1 = new Paint();
            paint1.setAntiAlias(true);
            paint1.setColor(Color.GREEN);
            paint2 = new Paint();
            paint2.setAntiAlias(true);
            paint2.setColor(Color.BLUE);
    
            setZOrderOnTop(true);
            getHolder().setFormat(PixelFormat.TRANSLUCENT);
    
            setFocusable(true); // 设置焦点
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            viewWidth = MeasureSpec.getSize(widthMeasureSpec);
            viewHeight = MeasureSpec.getSize(heightMeasureSpec);
    
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            r2 = new RectF();
            r2.left = 0;
            r2.right = viewWidth;
            r2.top = 0;
            r2.bottom = viewHeight;
    
            canvas.drawRoundRect(r2, 10, 10, paint2);
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder surfaceHolder) {
            Log.i("zhang_xin", "创建");
            stop = false;
            scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
            scheduledExecutorService.scheduleAtFixedRate(new ScrollThread(), 1000,
                    10, TimeUnit.MILLISECONDS);
    
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
    
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
            stop = true;
            scheduledExecutorService.shutdown();
        }
    
        class ScrollThread implements Runnable {
    
            @Override
            public void run() {
                while (!stop) {
                    draw(progress);
                    if(progress>viewWidth){
                        stop = true;
                    }
                    Log.i("zhang_xin", "绘制中" + (progress));
                    progress = progress + speed;
                }
            }
        }
    
        private synchronized void draw(float v) {
            Canvas canvas = holder.lockCanvas();
            r2.left = 0;
            r2.right = v;
            r2.top = 0;
            r2.bottom = viewHeight;
    
            canvas.drawRoundRect(r2, 10, 10, paint1);
            holder.unlockCanvasAndPost(canvas);
        }
    }
    
    

    相关文章

      网友评论

          本文标题:SurfaceView实现进度条

          本文链接:https://www.haomeiwen.com/subject/lzzhjctx.html