/** * 单点触控操作 * * @param canvas * @param paint SinglePointTouch(Canvas canvas, Paint paint) { (moveXOfFirst < points.get(0).x) { moveXOfFirst = points.get(0).x; } if (moveXOfFirst > points.get(points.size() - 1).x) { moveXOfFirst = points.get(points.size() - 1).x; } float moveX = moveXOfFirst; // 绘制度数框 背景后的 横线 canvas.drawBitmap(mTitleLine, SPACING, mGradientHeight + SPACING_HEIGHT - mTrendLine.getHeight() + 5, paint); // 绘制 移动的 点 onPointMove(canvas, paint, moveXOfFirst, isDown); canvas.drawBitmap( mDegree, moveX - mDegree.getWidth() / 2, (mGradientHeight + SPACING_HEIGHT - mTrendLine.getHeight() + 5) / 2, paint); moveY = getMoveY(moveXOfFirst); float energyHeight = (float) (mGradientHeight + SPACING_HEIGHT) - moveY; String energyText = String.valueOf(energyHeight / spacingOfY); // 为了避免误差 如果单点 手指在X轴 在预定的 X点上 那么直接将显示读书设置为 服务器传回的数据 EnergyItem energy = isInPoint(moveXOfFirst); if (energy != null) { energyText = String.valueOf(energy.value); } int indexOf = energyText.indexOf("."); String substring = energyText.substring(0, indexOf + 2); paint.setTextSize(28); paint.setColor(Color.BLACK); canvas.drawText(substring + "度", moveX - mDegree.getWidth() / 3, mGradientHeight + SPACING_HEIGHT - mTrendLine.getHeight() + mDegree.getHeight() / 3, paint); }
多点触摸
/** * 多点触控操作 * * @param canvas * @param paint multiPointTouch(Canvas canvas, Paint paint) { paint.setColor(Color.rgb(240, 150, 40)); paint.setStrokeWidth(4); pointSpacing = 0.0f; (moveXOfFirst < points.get(0).x) { moveXOfFirst = points.get(0).x; } if (moveXOfFirst > points.get(points.size() - 1).x) { moveXOfFirst = points.get(points.size() - 1).x; } if (moveXOfSecond < points.get(0).x) { moveXOfSecond = points.get(0).x; } if (moveXOfSecond > points.get(points.size() - 1).x) { moveXOfSecond = points.get(points.size() - 1).x; } int moveXOfOne = getLocation(moveXOfFirst); int moveXOfTwo = getLocation(moveXOfSecond); (moveXOfOne < moveXOfTwo) { if (!(moveXOfSecond == points.get(points.size() - 1).x)) { moveXOfTwo = moveXOfTwo - 1; } // 重绘 两点间的 的连接线 canvas.drawLine(moveXOfFirst, getMoveY(moveXOfFirst), points.get(moveXOfOne).x, points.get(moveXOfOne).y, paint); for (int j = moveXOfOne; j < moveXOfTwo; j++) { PointF startPoint = points.get(j); if (j + 1 == points.size()) { // 绘制 最后一个圆点 为剪切的图片 canvas.drawBitmap(mLastPoint, startPoint.x - mLastPoint.getWidth() / 2, startPoint.y - mLastPoint.getHeight() / 2, paint); break; } PointF endPoint = points.get(j + 1); // 绘制曲线,并且覆盖剪切后的锯齿 canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, paint); // 为了使线条圆滑 绘一个点 每个坐标系 绘制一个圆点 canvas.drawPoint(startPoint.x, startPoint.y, paint); } canvas.drawLine(points.get(moveXOfTwo).x, points.get(moveXOfTwo).y, moveXOfSecond, getMoveY(moveXOfSecond), paint); } (moveXOfOne > moveXOfTwo) { if (!(moveXOfFirst == points.get(points.size() - 1).x)) { moveXOfOne = moveXOfOne - 1; } // 重绘 两点间的 的连接线 canvas.drawLine(moveXOfSecond, getMoveY(moveXOfSecond), points.get(moveXOfTwo).x, points.get(moveXOfTwo).y, paint); for (int j = moveXOfTwo; j < moveXOfOne; j++) { PointF startPoint = points.get(j); if (j + 1 == points.size()) { // 绘制 最后一个圆点 为剪切的图片 canvas.drawBitmap(mLastPoint, startPoint.x - mLastPoint.getWidth() / 2, startPoint.y - mLastPoint.getHeight() / 2, paint); break; } PointF endPoint = points.get(j + 1); // 绘制曲线,并且覆盖剪切后的锯齿 canvas.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y, paint); // 为了使线条圆滑 绘一个点 每个坐标系 绘制一个圆点 canvas.drawPoint(startPoint.x, startPoint.y, paint); } canvas.drawLine(points.get(moveXOfOne).x, points.get(moveXOfOne).y, moveXOfFirst, getMoveY(moveXOfFirst), paint); } (getArea(moveXOfFirst) == getArea(moveXOfSecond)) { canvas.drawLine(moveXOfFirst, getMoveY(moveXOfFirst), moveXOfSecond, getMoveY(moveXOfSecond), paint); } float allEnergy = 0.0f; // 区域内的温度总和 (moveXOfFirst < moveXOfSecond) { pointSpacing = Math.abs(moveXOfSecond - moveXOfFirst) / 2 + moveXOfFirst; (int j = moveXOfOne; j <= moveXOfTwo; j++) { allEnergy += energyItems.get(j).value; } } (moveXOfFirst > moveXOfSecond) { pointSpacing = Math.abs(moveXOfFirst - moveXOfSecond) / 2 + moveXOfSecond; for (int j = moveXOfTwo; j <= moveXOfOne; j++) { allEnergy += energyItems.get(j).value; } } (moveXOfFirst == moveXOfSecond) { pointSpacing = Math.abs(moveXOfFirst); } // 绘制 移动的 点 onPointMove(canvas, paint, moveXOfFirst, isDown); if (eventCount >= 2) { onPointMove(canvas, paint, moveXOfSecond, isDown); } // 绘制度数框 背景后的 横线 canvas.drawBitmap(mTitleLine, SPACING, mGradientHeight + SPACING_HEIGHT - mTrendLine.getHeight() + 5, paint); // 顶部的度数框 canvas.drawBitmap(mDegree, pointSpacing - mDegree.getWidth() / 2 + mTrendLine.getWidth() / 2, (mGradientHeight + SPACING_HEIGHT - mTrendLine.getHeight() + 5) / 2, paint); // 绘制 总共消耗的能耗为多少 String energyText = String.valueOf(allEnergy); int indexOf = energyText.indexOf("."); String substring = energyText.substring(0, indexOf + 2); paint.setTextSize(28); paint.setColor(Color.BLACK); canvas.drawText(substring + "度", pointSpacing - mDegree.getWidth() / 4, mGradientHeight + SPACING_HEIGHT - mTrendLine.getHeight() + mDegree.getHeight() / 3, paint); }
源码下载:EnergyCurve.rar