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;
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);
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;
Cache_canvas.drawLine(100f,y_axis_height+50,100+x_axis_width,y_axis_height+50,AxesPaint);
Cache_canvas.drawLine(100f,50f,100f,50+y_axis_height,AxesPaint);
if(tmp_Box == null)
{
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);
}
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();
float y_axis_max = y_array[0];
float y_axis_min = y_array[0];
for(int m = 1;mif(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;
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);
}
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;
}
}
}