• 安卓开发断崖揽道VIEW


    public class GeographicProfileViewV1 extends View {
        public Paint profilePaint,RadiusPaint;
        public Path profilePath;
        public Paint waterLinePaint;
        public Paint waterFillPaint; // 新增的水位填充颜色
        public static final int PROFILE_COLOR = Color.parseColor("#967C57");
        public static final int WATER_LINE_COLOR = Color.parseColor("#6699CC");
        public static final int SEA_LEVEL_COLOR = Color.GRAY;
        public static final int AXIS_COLOR = Color.parseColor("#49B6FF");
        public static final int AXIS_COLOR1 = Color.parseColor("#1FDCD5");
    
        public static final int TEXT_COLOR = Color.WHITE;
    
        public int scale=20;
        // 在你的类中定义一个缩放因子,初始值为1.0
        public float scaleFactor = 1f;
        // 地理数据
        public double[] elevationData = {};
        public double[] xData = {};
    
        public List YList=new ArrayList<>();
        public List XList=new ArrayList<>();
    
        // 水位高度
        public double waterLevel = 8; // 你可以根据需要调整水位高度
        public double waveAmplitude = 2; // 水波纹振幅
        public double waveFrequency = 200; // 水波纹频率
        public double wavePhase = 10; // 水波纹相位
        public float circleRadius = 3; // 小圆点的半径
    
        public List listCircle;
    
        public double minElevation;
        public double maxElevation;
        public float drawingHeight;
    
        public int viewWidth;
    
        public Matrix matrix = new Matrix();
    
        public int maxLane=81;
    
    
        public double verticalLine=8;
    
        private int currentDataIndex = 0;
        private Paint axisPaint;
    
        public GeographicProfileViewV1(Context context) {
            super(context);
    
            init();
        }
    
        public GeographicProfileViewV1(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public void init() {
            axisPaint = new Paint();
            axisPaint.setColor(TEXT_COLOR); // 设置文本颜色
            axisPaint.setStrokeWidth(4); // 设置文本笔触宽度
            axisPaint.setTextSize(14); // 设置文本大小
    
            XyModel xyModel = new Gson().fromJson("{\n" +
                    "    \"clist\":[\n" +
                    "        [\n" +
                    "            0,\n" +
                    "            12.224\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            0,\n" +
                    "            12.224\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            0,\n" +
                    "            12.224\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            0.77,\n" +
                    "            12.258\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            4.78,\n" +
                    "            9.999\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            6.79,\n" +
                    "            9.348\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            6.79,\n" +
                    "            9.348\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            9.59,\n" +
                    "            9.183\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            10.75,\n" +
                    "            8.851\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            15.64,\n" +
                    "            8.306\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            21.61,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            21.61,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            21.61,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            21.61,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            21.61,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            26.98,\n" +
                    "            7.599\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            27.78,\n" +
                    "            7.379\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            29.02,\n" +
                    "            7.379\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            29.37,\n" +
                    "            7.629\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            33.38,\n" +
                    "            7.599\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            34.07,\n" +
                    "            7.429\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            35.07,\n" +
                    "            7.489\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            37.13,\n" +
                    "            7.379\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            40.54,\n" +
                    "            7.539\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            41.21,\n" +
                    "            7.539\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            41.47,\n" +
                    "            7.299\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            44.09,\n" +
                    "            7.279\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            47.03,\n" +
                    "            7.299\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            47.2,\n" +
                    "            7.549\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            50.38,\n" +
                    "            7.339\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            52.62,\n" +
                    "            6.979\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            54.47,\n" +
                    "            7.159\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            55.01,\n" +
                    "            7.449\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            56.1,\n" +
                    "            7.469\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            56.64,\n" +
                    "            7.329\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            56.88,\n" +
                    "            7.169\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            58.64,\n" +
                    "            7.189\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            59.26,\n" +
                    "            7.279\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            59.51,\n" +
                    "            7.339\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            59.78,\n" +
                    "            7.379\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            60.27,\n" +
                    "            7.419\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            60.6,\n" +
                    "            7.279\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            61.74,\n" +
                    "            7.289\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            66.35,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            66.35,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            66.35,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            66.35,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            66.35,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            68.32,\n" +
                    "            8.259\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            70.17,\n" +
                    "            8.596\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            72.28,\n" +
                    "            9.099\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            74.21,\n" +
                    "            9.643\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            76.06,\n" +
                    "            10.017\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            77.61,\n" +
                    "            10.077\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            81.11,\n" +
                    "            10.349\n" +
                    "        ]\n" +
                    "    ],\n" +
                    "    \"plist\":[\n" +
                    "        [\n" +
                    "            21.61,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            66.35,\n" +
                    "            7.889\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            26.98,\n" +
                    "            7.599\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            27.78,\n" +
                    "            7.379\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            29.02,\n" +
                    "            7.379\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            29.37,\n" +
                    "            7.629\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            33.38,\n" +
                    "            7.599\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            34.07,\n" +
                    "            7.429\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            35.07,\n" +
                    "            7.489\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            37.13,\n" +
                    "            7.379\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            40.54,\n" +
                    "            7.539\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            41.21,\n" +
                    "            7.539\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            41.47,\n" +
                    "            7.299\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            44.09,\n" +
                    "            7.279\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            47.03,\n" +
                    "            7.299\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            47.2,\n" +
                    "            7.549\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            50.38,\n" +
                    "            7.339\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            52.62,\n" +
                    "            6.979\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            54.47,\n" +
                    "            7.159\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            55.01,\n" +
                    "            7.449\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            56.1,\n" +
                    "            7.469\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            56.64,\n" +
                    "            7.329\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            56.88,\n" +
                    "            7.169\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            58.64,\n" +
                    "            7.189\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            59.26,\n" +
                    "            7.279\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            59.51,\n" +
                    "            7.339\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            59.78,\n" +
                    "            7.379\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            60.27,\n" +
                    "            7.419\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            60.6,\n" +
                    "            7.279\n" +
                    "        ],\n" +
                    "        [\n" +
                    "            61.74,\n" +
                    "            7.289\n" +
                    "        ]\n" +
                    "    ]\n" +
                    "}", XyModel.class);
    
    
            List> clist = xyModel.getClist();
            List list =new ArrayList<>();
            list.add(90.0);
            list.add(0.0);
           clist.add(list);
            List list1 =new ArrayList<>();
            list1.add(100.0);
            list1.add(15.0);
            clist.add(list1);
    
            for (int i = 0; i  integers = clist.get(i);
                Double y = integers.get(0);
                Double x = integers.get(1);
                YList.add(x);
                XList.add(y);
            }
            elevationData = new double[YList.size()];
            for (int i = 0; i < YList.size(); i++) {
                elevationData[i] = YList.get(i);
            }
    
            xData = new double[XList.size()];
            for (int i = 0; i < XList.size(); i++) {
                xData[i] = XList.get(i);
            }
    
    
            minElevation = Double.MAX_VALUE;
            maxElevation = -Double.MAX_VALUE;
            // 找到最小和最大高程值
            for (double elevation : elevationData) {
                if (elevation < minElevation) {
                    minElevation = elevation;
                }
                if (elevation > maxElevation) {
                    maxElevation = elevation;
                }
            }
            drawingHeight = (float) (maxElevation - minElevation);
            profilePaint = new Paint();
            profilePaint.setColor(PROFILE_COLOR);
            profilePaint.setStrokeWidth(4);
            profilePaint.setStyle(Paint.Style.STROKE);
    
            profilePath = new Path();
    
            waterLinePaint = new Paint();
    
            waterLinePaint.setColor(WATER_LINE_COLOR); //
            waterLinePaint.setStrokeWidth(2);
    
            waterFillPaint = new Paint();
            Shader shader = new LinearGradient(10, 20, 50, 18, AXIS_COLOR1, AXIS_COLOR, Shader.TileMode.CLAMP);
            waterFillPaint.setShader(shader);
            waterFillPaint.setStyle(Paint.Style.FILL);
    
            RadiusPaint = new Paint();
            RadiusPaint.setColor(AXIS_COLOR);
            RadiusPaint.setStrokeWidth(2);
            RadiusPaint.setStyle(Paint.Style.FILL);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
    
            int viewWidth = getWidth();
            int viewHeight = getHeight();
            // 应用缩放变换
            matrix.reset();
            matrix.setScale(scaleFactor, scaleFactor);
             // 将缩放变换的锚点设置为左下角
            matrix.postTranslate(0, viewHeight * (1 - scaleFactor));
            // 将缩放变换应用到画布
            canvas.concat(matrix);
            drawWater(canvas);
            drawElevationData(canvas);
    
            // 绘制海平面虚线
            drawSeaLevelLine(canvas, viewWidth, viewHeight);
            // 在缩放后绘制内容
            drawAxis(canvas, viewWidth, viewHeight);
            // 更新水波纹相位以实现波动效果
            wavePhase += 0.1;
            drawCircles(canvas);
            getData();
            // 数据发生变化后,调用 invalidate() 触发重新绘制
        }
    
        private void drawWater(Canvas canvas) {
            int width = getWidth();
            int height = getHeight();
            // 计算水位线高度,加入正弦波动态效果
            float waterLineY = height - (float) ((waterLevel - minElevation) * (height / drawingHeight));
    
            waterLineY += (float) (waveAmplitude * Math.sin(2 * Math.PI * waveFrequency + wavePhase));
    
            // 绘制水位填充颜色
            canvas.drawRect(0, waterLineY, width, height, waterFillPaint);
    
            // 绘制水位线
            canvas.drawLine(0, waterLineY, width, waterLineY, waterLinePaint);
        }
    
    
        private void drawElevationData(Canvas canvas) {
            int width = getWidth();
            int height = getHeight();
            float intervalX = 0;
            // 计算绘制的线条间隔
            float screenPixelWidth = getWidth(); // 获取屏幕宽度
            float xDataRange = maxLane; // x 轴数据的范围  要跟最大的距离做换算
            intervalX = screenPixelWidth / xDataRange; // 计算间隔,确保 x 数据能够适应屏幕宽度
    
            // 找到最小和最大高程值
            double minElevation = Double.MAX_VALUE;
            double maxElevation = -Double.MAX_VALUE;
    
            for (double elevation : elevationData) {
                if (elevation < minElevation) {
                    minElevation = elevation;
                }
                if (elevation > maxElevation) {
                    maxElevation = elevation;
                }
            }
    
            // 计算绘图区域的高度
            float drawingHeight = (float) maxElevation - (float) minElevation;
    
            profilePath.reset();
    
            // 移动到起始点
            profilePath.moveTo(0, height);
            listCircle = new ArrayList<>();
            for (int i = 0; i < elevationData.length; i++) {
                float x = (float) (xData[i] * intervalX);
                float y = (float) ((elevationData[i] - minElevation) * (height / drawingHeight));
                profilePath.lineTo(x, height - y);
                // 在每个关键点位置绘制小圆点
                if (elevationData[i]0){
                 minY=0;
             }
    
            for (int i = 0; i <= maxY; i++) {
                if (i % 2 == 0) {
                    float y = height - (float) ((i - minElevation) * (height / drawingHeight));
                    // 添加Y轴刻度文字
                    String label = String.valueOf(i);
                    Double aDouble = Double.valueOf(label);
                    if (verticalLine==aDouble){
                        axisPaint.setColor(Color.parseColor("#49B6FF"));
                    }else {
                        axisPaint.setColor(Color.parseColor("#FFFFFF"));
                    }
                    canvas.drawLine(5, y, 10, y, axisPaint);
                    canvas.drawText(label, 25, y + 5, axisPaint);
                }
            }
        }
        private void drawCircles(Canvas canvas) {
            for (CircleModel circle : listCircle) {
                if (!circle.isColor()){
                    RadiusPaint = new Paint();
                    RadiusPaint.setColor(AXIS_COLOR);
                    RadiusPaint.setStrokeWidth(2);
                    RadiusPaint.setStyle(Paint.Style.FILL);
                }else {
                    RadiusPaint = new Paint();
                    RadiusPaint.setColor(TEXT_COLOR);
                    RadiusPaint.setStrokeWidth(2);
                    RadiusPaint.setStyle(Paint.Style.FILL);
                }
                canvas.drawCircle(circle.getX(), circle.getY(), circleRadius, RadiusPaint);
            }
        }
    
        public void getData(){
            // 添加Y轴刻度标注axisPaint
            int height = getHeight();
            float y=0;
            y = height - (float) ((verticalLine - minElevation) * (height / drawingHeight));
            setDataY.getY(minElevation,height,drawingHeight);
        }
    
        public setDataY setDataY;
    
        public void setSetDataY(GeographicProfileViewV1.setDataY setDataY) {
            this.setDataY = setDataY;
        }
    
        public  interface  setDataY{
          void  getY(double minElevation,int height,double drawingHeight);
        }
    
    }
    

    public class HomeView extends View {
    
    
        private Paint profilePaint,profilePaintV1;
        private int maxLane=81;
    
    
        private float tower1X, tower1Y; // 塔1底部坐标
        private float tower2X, tower2Y; // 塔2底部坐标
    
        private float towerHeight = 80; // 塔身高度
        private float towerWidth = 80; // 塔身宽度
    
        double drawingHeight;
        double minElevation;
        int heightView;
        /**
         * 垂线
         */
        private List LineList=new ArrayList<>();
        /**
         * 吊舱
         */
        private List ListGondola=new ArrayList<>();
    
        /**
         * 维修区域
         */
        private List RepairArea=new ArrayList<>();
    
        private Paint towerPaint;
        private int width;
        private int height;
    
    
        /**
         * 动画
         */
        private float currentX;
        private float maxX; // 最大 X 坐标值
    
    
    
    
        public HomeView(Context context) {
            super(context);
            init();
        }
    
        public HomeView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public HomeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
        private void init() {
            profilePaint = new Paint();
            profilePaint.setColor(Color.parseColor("#6CEBA2"));
            profilePaint.setStrokeWidth(2);
            profilePaint.setStyle(Paint.Style.FILL_AND_STROKE);
            profilePaintV1 = new Paint();
            profilePaintV1.setColor(Color.parseColor("#FFFFFF"));
            profilePaintV1.setStrokeWidth(2);
            profilePaintV1.setStyle(Paint.Style.FILL_AND_STROKE);
            towerPaint = new Paint();
            towerPaint.setColor(Color.parseColor("#6CEBA2"));
            towerPaint.setStyle(Paint.Style.FILL);
            tower1X=150;
            tower1Y=600;
            LineList.add(new GondolaModel("垂线8m",8,10));
            LineList.add(new GondolaModel("垂线12m",12,22));
    
    
            ListGondola.add(new GondolaModel("1吊舱35m",10,35));
            ListGondola.add(new GondolaModel("2吊舱50m",12,50));
    
    
            RepairArea.add(new RepairAreaModel(5,15,25));
            RepairArea.add(new RepairAreaModel(60,14,75));
        }
    
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            width = getWidth();
            height = getHeight();
            int myX= width /maxLane; //这里的100是揽道最大宽度
            profilePaint.setColor(Color.WHITE);
            //添加第一条揽线两边的圆
            //添加第一条揽线
            canvas.drawCircle(50, 10, 10, profilePaint);
            canvas.drawCircle(getWidth()-50, 10, 10, profilePaint);
            canvas.drawLine(50, 10, getWidth()-50, 10, profilePaint);
    
    
            profilePaint.setColor(Color.parseColor("#6BDC2E"));
            canvas.drawCircle(50, (getHeight()/2)/2, 10, profilePaint);
            canvas.drawCircle(getWidth()-50, (getHeight()/2)/2, 10, profilePaint);
            canvas.drawLine(50, (getHeight()/2)/2, getWidth()-50, (getHeight()/2)/2, profilePaint);
    
    
            profilePaintV1.setPathEffect(new DashPathEffect(new float[]{10, 10}, 0));
    
    
    
            /**
             * 画垂线
             */
            addLine(canvas,myX);
            /**
             * 画吊舱
             */
            addGondola(canvas,myX);
            /**
             * 画维修发车区域
             */
            addMaintenance(canvas,myX);
    
        }
    
        private void addMaintenance(Canvas canvas, int myX) {
            for (int i = 0; i  
    

    public class MyRuler extends View {
        private static final int AXIS_COLOR = Color.parseColor("#FFFFFF");
        private int maxLane=81;
        public MyRuler(Context context) {
            super(context);
            init();
        }
    
        private void init() {
    
        }
    
        public MyRuler(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public MyRuler(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
    
    
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            int width = getWidth();
            int height = getHeight();
            Paint axisPaint = new Paint();
            axisPaint.setColor(AXIS_COLOR);
            axisPaint.setStrokeWidth(4);
            axisPaint.setTextSize(20);
            int myX= width/maxLane; //这里的100是揽道最大宽度
    
    
            canvas.drawLine(0, height, width, height, axisPaint);
    
            for (int i = 0; i <=maxLane ; i++) {
                if (i % 10 == 0) {
                    float x = i * width / (maxLane - 1);
                    canvas.drawLine(x, height, x, height - 10, axisPaint);
                    canvas.drawText(i+"",myX*i, height - 30, axisPaint);
                }
    
            }
        }
    }
    

    
    
    
    
    
    
        
    
    
        
    
            
    
    
            
    
            
    
    
            
    
    
    
    
    
        
    
    
            
        
    
    
    
    
    
    

  • 相关阅读:
    【前端设计模式】之状态模式
    8.11学习日志 后缀数组+DLX
    JavaScript基础
    sql server 恢复数据库、恢复单表数据的方法
    Druid LogFilter输出可执行的SQL
    Lua基础
    12.LoadRunner,基于html录制和基于url录制
    asp.net core之依赖注入
    创建一个springboot项目并加入mybatis
    Linux进阶-文件
  • 原文地址:https://blog.csdn.net/zzf0521/article/details/134068991