@Override public void run() { DatagramSocket datagramSocket = null; while (isContinueListen) { try { if (datagramSocket == null) { datagramSocket = new DatagramSocket(null); datagramSocket.setReuseAddress(true); datagramSocket.bind(new InetSocketAddress(listenPort)); datagramSocket.setReceiveBufferSize(512 * 1024); } byte[] buffer = new byte[1024 * 1024]; DatagramPacket udpPacket = new DatagramPacket(buffer, buffer.length); datagramSocket.receive(udpPacket); LPP.put(udpPacket); Logg.e(TAG, "收到2000包"); } catch (Exception e) { e.printStackTrace(); Logg.e(TAG, "收到2000包 error"); continue; } } if (datagramSocket != null) { datagramSocket.close(); datagramSocket = null; } }
@Override public void run() { while (isContinueListen) { DatagramPacket udpPacket = null; try { Logg.i(TAG, "准备从LPP列表中取出一个UDP数据报"); udpPacket = LPP.take(); Logg.i(TAG, "从LPP列表中取出了一个UDP数据报"); } catch (InterruptedException e) { e.printStackTrace(); Logg.e(TAG, "run error"); continue; } boolean isScreenLocked = ((ClassWorkActivity) mContext).isScreenLocked(); if (!isScreenLocked) continue; int packetLen = udpPacket.getLength(); byte[] packetData = udpPacket.getData(); int operationType = myConvertByteArrToInt_bigend(packetData, 0); System.out.println("操作类型为" + operationType); // handDraw(packetData); switch (operationType) { case OPERATION_TYPE_HANDDRAW: handDraw(packetData); break; case OPERATION_TYPE_ERASE: eraseArea(packetData); break; case OPERATION_TYPE_LINE: drawLine(packetData); break; case OPERATION_TYPE_RECTANGLE: drawRectangle(packetData); break; case OPERATION_TYPE_ELLIPSE: drawEllipse(packetData); break; case OPERATION_TYPE_TEXT: drawText(packetData); break; case OPERATION_TYPE_PAGE: changePage(packetData); clearCanvas(); break; case OPERATION_TYPE_CLEARCANVAS: clearCanvas(); break; case OPERATION_TYPE_UNDO: undo(); break; case OPERATION_TYPE_REDO: redo(); break; case OPERATION_TYPE_MOVECANVAS: moveCanvas(packetData); break; } } // end of while } // end of run
private void eraseArea(byte[] packetData) { try { int penWidthNum = myConvertByteArrToInt_bigend(packetData, 4); int penColorNum = myConvertByteArrToInt_bigend(packetData, 8); int eraserWidth = myConvertByteArrToInt_bigend(packetData, 12); int eraserHeight = myConvertByteArrToInt_bigend(packetData, 16); int nums = myConvertByteArrToInt_bigend(packetData, 20); int BYTES_PER_COORDINATE = 2; for (int i = 0; i < nums / 4; i++) { int x = xCoordinateMap( myConvertByteArrToShort_bigend(packetData, 24 + i * 2 * BYTES_PER_COORDINATE)); int y = yCoordinateMap( myConvertByteArrToShort_bigend( packetData, 24 + i * 2 * BYTES_PER_COORDINATE + BYTES_PER_COORDINATE)); int x1, y1, x2, y2; x1 = x - (eraserWidth / 2 + penWidthNum); y1 = y - (eraserHeight / 2 + penWidthNum); x2 = x + (eraserWidth / 2 + penWidthNum); y2 = y + (eraserHeight / 2 + penWidthNum); if (i == 0) { mPath = new Path(); mPath.addRect(x1, y1, x2, y2, Direction.CW); mPaint = getAPaint(); mPaint.setStrokeWidth(penWidthMap.get(penWidthNum)); mPaint.setColor(penColorMap.get(penColorNum)); mPaint.setStyle(Style.FILL_AND_STROKE); mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR)); HistoryItem item = new HistoryItem(HistoryItem.TYPE_PATH, mPaint, mPath, null, null); undoPathList.add(item); // mCacheCanvas.drawPath(mPath, mPaint); } else { mPath.addRect(x1, y1, x2, y2, Direction.CW); mPaint.setXfermode(new PorterDuffXfermode(Mode.CLEAR)); mCacheCanvas.drawPath(mPath, mPaint); } handler.sendEmptyMessage(CustomDrawViewCH_INVALIDATE); if (!((ClassWorkActivity) mContext).isScreenLocked()) { break; } } // end of for } catch (Exception e) { e.printStackTrace(); Logg.e(TAG, "erase error"); } }
private void handDraw(byte[] packetData) { try { int penWidthNum = myConvertByteArrToInt_bigend(packetData, 4); int penColorNum = myConvertByteArrToInt_bigend(packetData, 8); int nums = myConvertByteArrToInt_bigend(packetData, 12); int BYTES_PER_COORDINATE = 2; for (int i = 0; i < nums / 4; i++) { int x = xCoordinateMap( myConvertByteArrToShort_bigend(packetData, 16 + i * 2 * BYTES_PER_COORDINATE)); int y = yCoordinateMap( myConvertByteArrToShort_bigend( packetData, 16 + i * 2 * BYTES_PER_COORDINATE + BYTES_PER_COORDINATE)); String str = (i + 1) + " (x=" + x + ",y=" + y + ")"; writeToMyLogFile(str); if (i == 0) { lastX = x; lastY = y; mPath = new Path(); mPath.moveTo(x, y); mPaint = getAPaint(); mPaint.setStrokeWidth(penWidthMap.get(penWidthNum)); mPaint.setColor(penColorMap.get(penColorNum)); HistoryItem item = new HistoryItem(HistoryItem.TYPE_PATH, mPaint, mPath, null, null); undoPathList.add(item); // mCacheCanvas.drawPoint(x, y, mPaint); } else { int dx = Math.abs(x - lastX); int dy = Math.abs(y - lastY); if (dx >= BezierCurveMinGap || dy >= BezierCurveMinGap) { mPath.quadTo(lastX, lastY, ((lastX + x) / 2.0f), ((lastY + y) / 2.0f)); lastX = x; lastY = y; mCacheCanvas.drawPath(mPath, mPaint); handler.sendEmptyMessage(CustomDrawViewCH_INVALIDATE); } } } } catch (Exception e) { e.printStackTrace(); Logg.e(TAG, "handdraw error"); } }
private void changePage(byte[] packetData) { try { int pageIndex = myConvertByteArrToInt_bigend(packetData, 4); int pagePart = myConvertByteArrToInt_bigend(packetData, 8); int strByteLen = myConvertByteArrToInt_bigend(packetData, 12); String dirName = myConvertByteArrToStr_bigend(packetData, 16, strByteLen); Message msg = new Message(); msg.what = MSG_CHANGE_PAGE; msg.arg1 = pageIndex; msg.arg2 = pagePart; msg.obj = dirName; handler.sendMessage(msg); } catch (Exception e) { e.printStackTrace(); Logg.e(TAG, "changePage error"); } }