绘制直角坐标系

public class DrawFragment extends Fragment {

    private SurfaceView drawView;
    private SurfaceHolder sfh;
    private Context mcontext;
    private UpdateBox preUpdateBox;
    private UpdateBox tmp_Box;
    private boolean draw = true;
    private int width = 0;
    private int height = 0;
    private int y_axis_n = 10;
    private int x_axis_n = 10;
    private float showTime = 30f; //最长30秒

    private Bitmap Cache_Bitmp = null;
    private Bitmap Screen_Bitmap = null;
    private Canvas Cache_canvas;
    private static DrawFragment df;
    private static CreateBitmapThread cbt;
    private static DrawThread drawThread;

    public DrawFragment(){}

    public static DrawFragment newInstance()
    {
        if(df == null)
        {
            df = new DrawFragment();
        }
        return df;
    }

    private class UpdateBox {
        private Double[] x;
        private Float[] y;
        private int rate;
        private int type;

        UpdateBox(Double[] x,Float[] y,int rate,int type)
        {
            this.x = x;
            this.y = y;
            this.rate = rate;
            this.type = type;
        }

        Double[] getX() {return x;}
        Float[] getY() {return y;}
        int getRate() {return rate;}
        int getType() {return type;}
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //设置布局文件
        View view = inflater.inflate(R.layout.drawlayout, container, false);
        drawView = (SurfaceView) view.findViewById(R.id.drawView);

        //添加自定义手势监听(GestureListener)
        //http://blog.csdn.net/shion1986_0514/article/details/78325835
        drawView.setOnTouchListener(new GestureListener(){
            @Override
            public void Zoom(float x,float y)
            {
                x = (x-1)*0.1f + 1;
                float time = showTime / x;
                if(time > 30){
                    time = 30;
                }
                if(time < 1)
                {
                    time = 1;
                }
                showTime = time;
            }
        });

        sfh = drawView.getHolder();
        sfh.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                if(cbt == null) {
                    cbt = new CreateBitmapThread();
                    cbt.start();
                }

                if(drawThread == null) {
                    drawThread = new DrawThread();
                    drawThread.start();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {

            }
        });

        //获取窗口宽高
        if(Build.VERSION.SDK_INT >= 16) {
            ViewTreeObserver vto = drawView.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onGlobalLayout() {
                    drawView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    width = drawView.getWidth();
                    height = drawView.getHeight();
                }
            });
        }else{
            int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            drawView.measure(w,h);
            height = drawView.getMeasuredHeight();
            width = drawView.getMeasuredWidth();
        }

        return view;
    }

    @Override
    public void onResume()
    {
        super.onResume();
    }

    @Override
    public void onAttach(Context mcontext)
    {
        super.onAttach(mcontext);
        this.mcontext = mcontext;
    }

    //更新函数
    public void Update(Double[] x,Float[] y,int rate,int type)
    {
        preUpdateBox = null;
        preUpdateBox = new UpdateBox(x, y, rate, type);
        tmp_Box = preUpdateBox;
    }

    private class DrawThread extends Thread
    {
        @Override
        public void run()
        {
            while(draw)
            {
                if(Screen_Bitmap != null)
                {
                    Date d = new Date();
                    if(sfh != null) {
                        Canvas canvas = sfh.lockCanvas();
                        if(canvas != null) {
                            canvas.drawColor(Color.WHITE);
                            Paint mpaint = new Paint();
                            canvas.drawBitmap(Screen_Bitmap, 0, 0, mpaint);
                            sfh.unlockCanvasAndPost(canvas);
                        }
                    }
                    long time = new Date().getTime() - d.getTime();

                    try {
                        int stime = 50 - (int)time;
                        if(stime > 0)
                        {
                            sleep(stime);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private class CreateBitmapThread extends Thread
    {
        @Override
        public void run()
        {
            Paint textpaint_Y = new Paint();
            textpaint_Y.setTextSize(myTools.dip2px(mcontext,8));
            textpaint_Y.setColor(Color.BLACK);
            textpaint_Y.setTextAlign(Paint.Align.RIGHT);

            Paint textpaint_X = new Paint();
            textpaint_X.setTextSize(myTools.dip2px(mcontext,8));
            textpaint_X.setColor(Color.BLACK);
            textpaint_X.setTextAlign(Paint.Align.CENTER);

            Paint Linepaint = new Paint();
            Linepaint.setColor(Color.BLUE);
            Linepaint.setStyle(Paint.Style.STROKE);
            Linepaint.setStrokeWidth(2);  //线的宽度

            Paint AxesPaint = new Paint();
            AxesPaint.setColor(Color.BLACK);
            AxesPaint.setStrokeWidth(2);  //线的宽度

            while(draw)
            {
                //获取到长宽
                if (width > 0 && height > 0) {

                    if(Cache_Bitmp == null || Cache_canvas == null) {
                        Cache_Bitmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                        Screen_Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
                        Cache_canvas = new Canvas(Cache_Bitmp);
                    }

                    Cache_canvas.drawColor(Color.WHITE);

                    //绘制坐标系
                    int x_axis_width = width - 100 - 50;
                    int y_axis_height = height - 100;

                    //原点坐标
                    int x_axis_zero = 100;
                    int y_axis_zero = y_axis_height + 50;

                    int point_space_y = y_axis_height / y_axis_n;
                    int point_space_x = x_axis_width / x_axis_n;

                    //绘制X轴
                               Cache_canvas.drawLine(100f,y_axis_height+50,100+x_axis_width,y_axis_height+50,AxesPaint);
                    //绘制Y轴
                    Cache_canvas.drawLine(100f,50f,100f,50+y_axis_height,AxesPaint);

                    if(tmp_Box == null)
                    {
                        //填充Y轴坐标数据
                        float H = 10;
                        float H_space = H / y_axis_n;
                        float[] y_axis_labels = new float[y_axis_n + 1];

                        for(int i = 0;i <= y_axis_n;i++)
                        {
                            y_axis_labels[i] = 0 + H_space * i;
                            DecimalFormat df = new DecimalFormat(".00");

                            Cache_canvas.drawLine(100f,y_axis_zero - point_space_y * i,97f,y_axis_zero - point_space_y * i,AxesPaint);
                            Cache_canvas.drawText(df.format(y_axis_labels[i]),95f,y_axis_zero - point_space_y * i,textpaint_Y);
                        }

                        //填充X轴坐标数据
                        Cache_canvas.drawLine(100f,y_axis_height+50,100f,y_axis_height+54,AxesPaint);

                        AddCacheToScreen();

                        try {
                            sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        continue;
                    }

                    Double[] x_array = tmp_Box.getX();
                    Float[] y_array = tmp_Box.getY();

                    //计算Y轴数据的上下限
                    float y_axis_max = y_array[0];
                    float y_axis_min = y_array[0];
                    for(int m = 1;m<y_array.length;m++)
                    {
                        if(y_axis_max < y_array[m]){y_axis_max = y_array[m];}
                        if(y_axis_min > y_array[m]){y_axis_min = y_array[m];}
                    }

                    if(y_axis_max == y_axis_min)
                    {
                        y_axis_max = y_axis_max + 1;
                        y_axis_min = y_axis_min - 1;
                    }

                    y_axis_max = y_axis_max + (y_axis_max - y_axis_min) / 2;
                    y_axis_min = y_axis_min - (y_axis_max - y_axis_min) / 2;

                    //填充Y轴坐标数据
                    float H = y_axis_max - y_axis_min;
                    float H_space = H / y_axis_n;
                    float[] y_axis_labels = new float[y_axis_n + 1];

                    for(int i = 0;i <= y_axis_n;i++)
                    {
                        y_axis_labels[i] = y_axis_min + H_space * i;
                        DecimalFormat df = new DecimalFormat("0.00");

                        Cache_canvas.drawLine(100f,y_axis_zero - point_space_y * i,97f,y_axis_zero - point_space_y * i,AxesPaint);
                        Cache_canvas.drawText(df.format(y_axis_labels[i]),95f,y_axis_zero - point_space_y * i,textpaint_Y);
                    }

                    //填充X轴坐标数据
                    double X_axis_start = x_array[0];
                    double X_axis_end = X_axis_start + showTime * 1000;
                    double X_axis_space = (X_axis_end - X_axis_start) / 10;
                    double[] x_axis_labels = new double[x_axis_n + 1];

                    for(int i = 0;i <= x_axis_n;i++)
                    {
                        x_axis_labels[i] = X_axis_start + i * X_axis_space;
                        String s = timeToStr(x_axis_labels[i]);

                        Cache_canvas.drawLine(100f,y_axis_height+50,100f,y_axis_height+54,AxesPaint);
                        Cache_canvas.drawText(s,100f + point_space_x * i ,y_axis_height+50+25,textpaint_X);
                    }

                    //填充数据到坐标系
                    float _y = y_axis_max - y_axis_min;
                    float y_res = y_axis_height / _y;
                    double _x = X_axis_end - X_axis_start;
                    float x_res = (float)(x_axis_width / _x);

                    int i = 0;
                    int x_num = x_array.length;
                    Path path = new Path();

                    while(i < x_num)
                    {
                        double x = x_array[i];
                        float y = y_array[i];

                        float p_x = x_axis_zero + ((float)(x - X_axis_start) * x_res);
                        float p_y = y_axis_zero - (y - y_axis_min) * y_res;

                        if(i == 0)
                        {
                            path.moveTo(p_x,p_y);
                        }else{
                            path.lineTo(p_x,p_y);
                        }

                        i++;
                    }

                    Cache_canvas.drawPath(path,Linepaint);

                    AddCacheToScreen();
                }

                try {
                    sleep(15);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void AddCacheToScreen()
    {
        int bytes = Cache_Bitmp.getByteCount();
        ByteBuffer buf = ByteBuffer.allocate(bytes);
        Cache_Bitmp.copyPixelsToBuffer(buf);
        byte[] byteArray = buf.array();

        Screen_Bitmap.copyPixelsFromBuffer(ByteBuffer.wrap(byteArray));
    }

    private String timeToStr(double t)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS");
        return sdf.format(new Date((long)t));
    }

    public void close()
    {
        if(draw) {
            draw = false;
            cbt = null;
            drawThread = null;
            df = null;
        }
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值