当前位置: 首页 > 工具软件 > ShareCenter > 使用案例 >

QieZi sharecenter surface progress bar

符正信
2023-12-01
package com.lenovo.anyshare.widget;

import com.lenovo.common.Logger;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class RadarScannerSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
    private static final int TIME_INTERNAL = 100;

    private SurfaceTread mThread;
    private boolean mIsInAnimation = true;
    private View mCenterView;
    private Paint mLinePaint;

    private int mWidth = 0;
    private int mTop = 0;
    private int mLeft = 0;

    private Style mLinePaintStyle = Style.FILL;
    private int mTransparency = 0xFF;
    private int mRed = 0xff;
    private int mGreen = 0xff;
    private int mBlue = 0xff;

    private int mCurIndex[] = { 0, 0, 0, 0 };
    private float mTime[] = { 0, 0.8f, 1.6f, 2.4f };

    private int mV0 = 10;
    private int mAcc = 150;
    private int mInterval = 0;

    public RadarScannerSurfaceView(Context context) {
        super(context);
        initView(context);
    }

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

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

    public void setStyle(Style style, int transparency, int red, int green, int blue) {
        mLinePaintStyle = style;
        mTransparency = transparency;
        mRed = red;
        mGreen = green;
        mBlue = blue;

        mLinePaint.setStyle(mLinePaintStyle);
    }

    private void initView(Context context) {
        setWillNotDraw(false);
        getHolder().addCallback(this);

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setStyle(mLinePaintStyle);
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawColor(Color.WHITE);
        drawCanvas(canvas);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mThread = new SurfaceTread(holder);
        mThread.mIsRun = true;
        mThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mThread.mIsRun = false;
        mThread.interrupt();
    }

    public void setAlignView(View view) {
        mCenterView = view;
        this.postInvalidate();
    }

    public void startAnimation() {
//        for (int i = 0; i < mTime.length; i++) {
//            mTime[i] = mInterval * i;
//        }
        mIsInAnimation = true;
    }

    public void stopAnimation() {
//        for (int i = 0; i < mTime.length; i++) {
//            mTime[i] = mInterval * i;
//        }
        mIsInAnimation = false;
        this.postInvalidate();
    }

    private void drawCanvas(Canvas canvas) {
        if (mCenterView != null) {
            mTop = mCenterView.getTop();
            mLeft = mCenterView.getLeft();
            mWidth = mCenterView.getWidth();
        } else {
            mLeft = 0;
            mTop = canvas.getHeight() / 2;
            mWidth = canvas.getWidth();
        }
        
        Logger.v("xiaxing", "mWidth : " + mWidth);

        if (!mIsInAnimation)
            return;

        Rect line = new Rect();
        mCenterView.getHitRect(line);
        int color = Color.argb(0xff, 0x24, 0xde, 0xff);
        mLinePaint.setColor(color);
        canvas.drawRect(line, mLinePaint);

        color = Color.argb(mTransparency, mRed, mGreen, mBlue);
        mLinePaint.setColor(color);
        canvas.drawRect(mLeft + mCurIndex[0], mTop, mLeft + 6 + mCurIndex[0], mTop + 4, mLinePaint);

        color = Color.argb(mTransparency, mRed, mGreen, mBlue);
        mLinePaint.setColor(color);
        canvas.drawRect(mLeft + mCurIndex[1], mTop, mLeft + 6 + mCurIndex[1], mTop + 4, mLinePaint);

        color = Color.argb(mTransparency, mRed, mGreen, mBlue);
        mLinePaint.setColor(color);
        canvas.drawRect(mLeft + mCurIndex[2], mTop, mLeft + 6 + mCurIndex[2], mTop + 4, mLinePaint);

        color = Color.argb(mTransparency, mRed, mGreen, mBlue);
        mLinePaint.setColor(color);
        canvas.drawRect(mLeft + mCurIndex[3], mTop, mLeft + 6 + mCurIndex[3], mTop + 4, mLinePaint);
    }

    public class SurfaceTread extends Thread {
        public boolean mIsRun = false;
        private SurfaceHolder mHolder;

        public SurfaceTread(SurfaceHolder holder) {
            this.mHolder = holder;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(600);
            } catch (InterruptedException e) {}

            while (mIsRun) {
                if (!mIsInAnimation || !mHolder.getSurface().isValid()) {
                    try {
                        Thread.sleep(TIME_INTERNAL);
                    } catch (InterruptedException e) {}
                    continue;
                }

                RadarScannerSurfaceView.this.postInvalidate();

                try {
                    Thread.sleep(TIME_INTERNAL);
                } catch (InterruptedException e) {}

                // s = v0*t + a * t2 / 2
                for (int i = 0; i < mCurIndex.length; i++) {
                    if (mCurIndex[i] > mWidth)
                        mTime[i] = 0;

                    mTime[i] = mTime[i] + 0.1f;
                    mCurIndex[i] = (int)(mV0 * mTime[i] + mAcc * mTime[i] * mTime[i] / 2);
                }
            }
        }
    };
}

 

 类似资料:

相关阅读

相关文章

相关问答