关于Android绘制指针 和传感器的demo

废话不多说直接上代码
xml



    

    

        

        
    


mainActivity

public class DemoZhiNaZhen extends View implements SensorEventListener {

    private int mWidth;
    private int mHeigth;
    private Sensor sensor;
    private SensorManager sensorManager;
    private int toDegrees;
    private Paint mPaint;

    public void setCureentAngla(int mCureentAngla) {
        this.mCureentAngla = mCureentAngla;
        if (sensorManager == null) {
            sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
            //通过 getDefaultSensor 获得指南针传感器
            sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
            //为传感器管理者注册监听器,第三个参数指获取速度正常
            sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_GAME);
        }
    }

    private int mCureentAngla = 181;
    private Paint mBitmapPaint;
    private Paint mTextPaint;
    private Context context;

    public DemoZhiNaZhen(Context context) {
        this(context, null);
    }

    public DemoZhiNaZhen(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DemoZhiNaZhen(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.GRAY);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(getResources().getDimension(R.dimen.dp_1));
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeJoin(Paint.Join.ROUND);

        mBitmapPaint = new Paint();
        mBitmapPaint.setDither(true);
        mBitmapPaint.setAntiAlias(true);

        mTextPaint = new Paint();
        mTextPaint.setDither(true);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.GRAY);
        mTextPaint.setTextSize(getResources().getDimension(R.dimen.sp_12));

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = MeasureSpec.getSize(widthMeasureSpec);
        mHeigth = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeigth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int x = mWidth / 2;
        int y = mHeigth / 2;
        int r = (int) (mWidth * 0.42);
        canvas.save();
        for (int i = 0 ; i < 120 ; i++) {
            if (i == 0) {
                mPaint.setColor(Color.RED);
                drawText("北", canvas);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mPaint);
            } else if (i == 30) {
                mPaint.setColor(Color.RED);
                drawText("东", canvas);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mPaint);
            } else if (i == 60) {
                mPaint.setColor(Color.RED);
                drawText("南", canvas);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mPaint);
            } else if (i == 90) {
                mPaint.setColor(Color.RED);
                drawText("西", canvas);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mPaint);
            } else if (i == 5 || i == 10 || i == 20 || i == 25 || i == 15 || i == 35 || i == 40
                    || i == 45 || i == 50 || i == 55 || i == 65 || i == 70 || i == 75 || i == 80
                    || i == 85 || i == 95 || i == 100 || i == 105 || i == 110 || i == 115) {
                mPaint.setColor(Color.BLACK);
                drawText(i * 3 + "", canvas);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mPaint);
            } else {
                //绘制下层菊花
                mPaint.setColor(Color.GRAY);
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.1), mPaint);
            }
            canvas.rotate(360 / 120, x, y);
        }
        for (int i = 0 ; i < 360 ; i++) {
            if (mCureentAngla == i) {
                canvas.drawLine(x, y - r, x, (float) (y - r + x * 0.2), mPaint);
            }
            canvas.rotate(360 / 360, x, y);
        }
        canvas.restore();
    }

    public void drawText(String string, Canvas canvas) {

        //画中间文字
        String text = string;
        Rect textRect = new Rect();
        mTextPaint.getTextBounds(text, 0, text.length(), textRect);
        int startX = getWidth() / 2 - textRect.width() / 2;
        int textHeight = textRect.height();
        int baseLine = 0;
        Paint.FontMetricsInt fontMetricsInt = mTextPaint.getFontMetricsInt();
        int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;
        baseLine = textHeight / 2 + dy;

        canvas.drawText(text, startX, baseLine, mTextPaint);
    }

    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {
        switch (sensorEvent.sensor.getType()) {
            case Sensor.TYPE_ORIENTATION:
                //顺时针转动为正,故手机顺时针转动时,图片得逆时针转动
                toDegrees = (int) -sensorEvent.values[0];
                if (this.onAnglaChanged != null) {
                    onAnglaChanged.onChanged(toDegrees);
                }
                break;
        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (sensorManager != null) {
            sensorManager.unregisterListener(this);
        }
    }

    public interface OnAnglaChanged {
        void onChanged(int angla);
    }

    public OnAnglaChanged onAnglaChanged;

    public void setOnAnglaChanged(OnAnglaChanged onAnglaChanged) {
        this.onAnglaChanged = onAnglaChanged;
    }
}

在values 中创建dimen 资源文件


    1dp
    80dp
    24sp
    240dp
    22dp
    12sp

最后给大家看看效果

最后的最后给大家分享一个可以查找图标的网站https://www.easyicon.net/

码字不易
请点赞评论加关注❤

你可能感兴趣的:(关于Android绘制指针 和传感器的demo)