技术干货自定义views Android开发

11.自定义控件-指南针

2017-07-26  本文已影响0人  android_赵乐玮

样式

compassView-gif.gif
控件分析:

线条分为3种
1.普通小线条;2每隔30°会绘制一个关键线条;3中间上部不变的指针
文字分为3种
1.边缘的度数标尺;2.方向标尺;3当前度数;4.当前方向

绘制思路:
    private static final float DIVIDE_COUNT = 120; //将圆划分为120等份
    private static final double CONVERSION_ANGLE_CONST = Math.PI / 180;  //转换角所用的常量

 //坐标旋转公式
    private float getRotatePointX(float a, float x, float y) {
        return (float) ((x - width / 2) * Math.cos(CONVERSION_ANGLE_CONST * a) + (y - height / 2) * Math.sin(CONVERSION_ANGLE_CONST * a)) + width / 2;
    }

    private float getRotatePointY(float a, float x, float y) {
        return (float) ((y - height / 2) * Math.cos(CONVERSION_ANGLE_CONST * a) - (x - width / 2) * Math.sin(CONVERSION_ANGLE_CONST * a)) + height / 2;
    }

其余线条绘制方法大致一样,修改修改参数就可以了。

String test = "120°";
        Rect rect = new Rect();
        mTextPaint.getTextBounds(test, 0, test.length(), rect);
        textW = rect.width();//文字宽
        textH = rect.height();//文字高

其余文字大致也是这样找好文字位置再偏移致中点

void initSize() {
        edgeTextSize = 38 - (1080 - width) / 30;
        edgeTextMargin = 50 - (1080 - width) / 30;
        orientationTextSize = 42 - (1080 - width) / 30;
        oriTextMargin = 92 - (1080 - width) / 12;
        angleTextSize = 60 - (1080 - width) * 4 / 75;
        rowPitch = 56 - (1080 - width) / 20;
        mainLineLength = 35 - (1080 - width) / 30;
    }

渐变公式推导:

△X1/△Y1 = △X2/△Y2        (暂定控件大小的可变范围为 480~1080)
=>>
(1080-480)/(x2-x1)= (1080-x) /(x2-y)
=>>
y= x2 - (1080-x)*(x2-x1)/(600)
文字大小的范围为[18-38]
y= 38 - (1080-x)*(38-18)/(600)
  =38 - (1080-x)/30

以此渐变文字大小

项目地址:
https://github.com/zhaolewei/CompassView

上一篇 下一篇

猜你喜欢

热点阅读