2

我有一个带有背景和前景图像的 imageView。当我尝试使用具有清除设置的路径和绘画通过前景擦除时,而不是删除前景路径,其绘制黑线。有人能帮助我吗?

这是我的代码片段

    public class ShowBackgrounOnDrawImage extends ImageView {

  private static final float MINP = 0.25f;
  private static final float MAXP = 0.75f;

  private Bitmap mBitmap, blur = null;
  private Canvas mCanvas;
  private Path mPath;
  private Paint mBitmapPaint;
  public boolean imageLoaded = false;
  private Canvas imageCanvas = null;
  Context here;
  Long width = null, height = null;
  private Paint mPaint;

  public BlurOnDrawImage(Context c) {
    super(c);
    initialize();
  }

  public BlurOnDrawImage(Context context, AttributeSet attrs)
  {
    super(context, attrs);
    initialize();
  }

  public BlurOnDrawImage(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);
    initialize();
  }

  private void initialize() {
    mPath = new Path();
    mPaint = new Paint();
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    mPaint.setColor(Color.TRANSPARENT);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeWidth(10f);
    mPaint.setAntiAlias(true);

    mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    mBitmapPaint.setStrokeWidth(80f);
  }

  public void setBitmap(Bitmap bitmap) {
    mBitmap = bitmap;
    mCanvas = new Canvas(bitmap);
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    //mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    // mCanvas = new Canvas(mBitmap);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (imageLoaded) {
      canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
      canvas.drawPath(mPath, mPaint);
      //canvas.drawPath(circlePath, circlePaint);
    } else {
      //imageCanvas = canvas;
    }
  }

  private float mX, mY;
  private static final float TOUCH_TOLERANCE = 4;

  private void touch_start(float x, float y) {
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;
  }

  private void touch_move(float x, float y) {
    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
    if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
      mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
      mX = x;
      mY = y;
    }
    //circlePath.reset();
    //circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
  }

  private void touch_up() {
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    //circlePath.reset();
    mPath.reset();
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    if (!imageLoaded) {
      return super.onTouchEvent(event);
    }
    float x = event.getX();
    float y = event.getY();

    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        touch_start(x, y);
        invalidate();
        break;
      case MotionEvent.ACTION_MOVE:
        touch_move(x, y);
        invalidate();
        break;
      case MotionEvent.ACTION_UP:
        touch_up();
        invalidate();
        break;
    }
    return true;
  }
}

谢谢

4

1 回答 1

0

我遇到了同样的问题。我在某处看到这是某些设备上的硬件加速问题,因此建议解决此问题的方法是在相应的绘制方法之前禁用硬件加速,因此在您的情况下应该是这样的:

    if (imageLoaded) {
      canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
      setLayerType(LAYER_TYPE_SOFTWARE, null);
      canvas.drawPath(mPath, mPaint);
    } else {
      //imageCanvas = canvas;
    }

这适用于我,Mode.CLEAR. 但不适用于其他一些类型DST_IN,例如。而且我没有找到这个问题的确切解决方案。

有在这个库中使用位图着色器的想法
https://github.com/lopspower/CircularImageView
可能这会起作用,但我没有测试它

于 2014-06-11T14:52:05.177 回答