美文网首页
Android 仿雷达扫瞄View

Android 仿雷达扫瞄View

作者: Luckflower | 来源:发表于2020-07-09 17:21 被阅读0次

    先给出效果图:

    20170305223358375.gif
    public class ScanView extends View {
    
        private Paint mCirclePaint;
        private Paint mSweepPaint;
        private int mSize = 800;
        private int mWidth;
        private int mHeight;
        private int mScreenWidth;
        private int mScreenHeight;
        private int mRadius;
        private Matrix mMatrix;
        private int mStart = 0;
        private ScanThread mScanThread;
        private Shader mShader;
    
        public ScanView(Context context) {
            super(context);
            init();
        }
    
        public ScanView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public ScanView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init(){
            mCirclePaint = new Paint();
            mCirclePaint.setColor(Color.BLACK);
            mCirclePaint.setStrokeWidth(5);
            mCirclePaint.setStyle(Paint.Style.FILL);
            mCirclePaint.setAntiAlias(true);
    
            //暗绿色的画笔
            mSweepPaint = new Paint();
            mSweepPaint.setColor(0x9D00ff00);
            mSweepPaint.setAntiAlias(true);
    
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
            //获取最短的边作为直径
            mSize = (mWidth >= mHeight) ? mHeight : mWidth;
            mRadius = mSize / 2;
            mScreenWidth = getResources().getDisplayMetrics().widthPixels;
            mScreenHeight = getResources().getDisplayMetrics().heightPixels;
    
            mShader = new SweepGradient(mSize / 2, mSize / 2, Color.TRANSPARENT, Color.RED);
            mSweepPaint.setShader(mShader);
            Log.e("jielong","mSize ="+mSize);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
    
    
            mCirclePaint.setColor(0xffB8DCFC);
            canvas.drawCircle(mSize/2, mSize/2, mRadius-20, mCirclePaint);
            //画圆
            mCirclePaint.setColor(Color.BLACK);
            mCirclePaint.setStyle(Paint.Style.STROKE);
            canvas.drawCircle(mSize/2, mSize/2, mRadius/3+40 , mCirclePaint);
            canvas.drawCircle(mSize/2, mSize/2, mRadius/2+80, mCirclePaint);
            canvas.drawCircle(mSize/2, mSize/2, mRadius-20, mCirclePaint);
            //画十字线
            //竖线
            canvas.drawLine(mSize/2, 20, mSize/2, mSize-20, mCirclePaint);
            //横线
            canvas.drawLine(20, mSize/2, mSize-20, mSize/2, mCirclePaint);
    
            canvas.concat(mMatrix);
            canvas.drawCircle(mSize / 2, mSize / 2, mSize/2-20, mSweepPaint);
            super.onDraw(canvas);
    
        }
    
        public void start() {
            mScanThread = new ScanThread(this);
            mScanThread.start();
        }
    
        protected class ScanThread extends Thread {
    
            private ScanView mView;
    
            public ScanThread(ScanView view) {
                this.mView = view;
            }
    
            @Override
            public void run() {
                while (true) {
                    mView.post(new Runnable() {
                        public void run() {
                            mStart ++;
                            mMatrix = new Matrix();
                            mMatrix.preRotate(mStart, mSize/2, mSize/2);
                            mView.invalidate();
                        }
                    });
                    try {
                        Thread.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    使用很简单,如下所示:

    public class MainActivity extends AppCompatActivity {
        private ScanView mScanView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mScanView = (ScanView) findViewById(R.id.scan_view);
            mScanView.start();
        }
    }
    
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.cjl.scanview.MainActivity">
    
        <com.cjl.scanview.ScanView
            android:id="@+id/scan_view"
            android:layout_width="300dp"
            android:layout_height="300dp"
            android:layout_gravity="center"/>
    
    </FrameLayout>
    

    相关文章

      网友评论

          本文标题:Android 仿雷达扫瞄View

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