0

我需要在屏幕上绘制,捕捉它并在另一个活动中显示,但我不需要显示它正在绘制的油漆颜色,即只有手指会移动,没有人能够看到它在绘制什么。

不知道如何在这种情况下取​​得进展。我做过这样的画,但这是正常的,你会看到有人在画什么:

    class SignatureView extends View {

      private static final float STROKE_WIDTH = 5f;

      private static final float HALF_STROKE_WIDTH = STROKE_WIDTH / 2;

      private Paint paint = new Paint();
      private Path path = new Path();

      private float lastTouchX;
      private float lastTouchY;
      private final RectF dirtyRect = new RectF();

      public SignatureView(Context context) {
        super(context);

        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeWidth(STROKE_WIDTH);
      }

      public void clear() {


        path.reset();
        AddView();

        invalidate();
      }

      @Override
      protected void onDraw(Canvas canvas) {
        canvas.drawPath(path, paint);
      }

      @Override
      public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();

        switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            lastTouchX = eventX;
            lastTouchY = eventY;
            return true;

          case MotionEvent.ACTION_MOVE:
          case MotionEvent.ACTION_UP:
            resetDirtyRect(eventX, eventY);

            int historySize = event.getHistorySize();
            for (int i = 0; i < historySize; i++) {
              float historicalX = event.getHistoricalX(i);
              float historicalY = event.getHistoricalY(i);
              expandDirtyRect(historicalX, historicalY);
              path.lineTo(historicalX, historicalY);
            }

            path.lineTo(eventX, eventY);
            break;

          default:
            debug("Ignored touch event: " + event.toString());
            return false;
        }

        invalidate(
            (int) (dirtyRect.left - HALF_STROKE_WIDTH),
            (int) (dirtyRect.top - HALF_STROKE_WIDTH),
            (int) (dirtyRect.right + HALF_STROKE_WIDTH),
            (int) (dirtyRect.bottom + HALF_STROKE_WIDTH));

        lastTouchX = eventX;
        lastTouchY = eventY;

        return true;
      }

      private void expandDirtyRect(float historicalX, float historicalY) {
        if (historicalX < dirtyRect.left) {
          dirtyRect.left = historicalX;
        } else if (historicalX > dirtyRect.right) {
          dirtyRect.right = historicalX;
        }
        if (historicalY < dirtyRect.top) {
          dirtyRect.top = historicalY;
        } else if (historicalY > dirtyRect.bottom) {
          dirtyRect.bottom = historicalY;
        }
      }


      private void resetDirtyRect(float eventX, float eventY) {

        dirtyRect.left = Math.min(lastTouchX, eventX);
        dirtyRect.right = Math.max(lastTouchX, eventX);
        dirtyRect.top = Math.min(lastTouchY, eventY);
        dirtyRect.bottom = Math.max(lastTouchY, eventY);
      }
    }

甚至尝试将油漆 alpha 设置为 0。但没有用它设置透明图像。

4

1 回答 1

0

尝试以下更改

paint.setColor(Color.TRANSPARENT);
于 2013-03-16T10:45:25.580 回答