2014-10-02 132 views
0

我有一个带有位图的ImageView。我想要的:当用户用手指在位图上绘制时,此部分应该变为透明(将像素Alpha值设置为0),您可以在图像视图下看到视图。 我可以使用ImageView吗?还是应该实现自定义视图?Android:在透明色中用手指在位图上绘制

我该如何认识到这一点? (只是大致)

+0

您可以用Bitmap.setPixel(INT X,INT Y,INT pixelColor)一起使用onTouch监听器,或者甚至是setPixels(int []像素,int偏移量,int stride,int x,int y,int width,int height),但这会超慢。我不认为这是可能的,如果不是每次触摸ImageView时都不会崩溃,除了可能使用像SurfaceView之类的东西外。 – zgc7009 2014-10-02 16:41:24

+0

你应该1)扩展一个视图2)覆盖onDraw()3)在其画布上绘制图像4)创建onTouchListener并处理onTouch事件:记住用户通过手指绘制了哪些内容并使其失效onDraw() – sberezin 2014-10-02 16:56:19

+0

我现在可以在位图上绘制,但是当我将绘画颜色设置为透明时,它会将位图透明化,即您看不到任何笔触。但我希望位图的像素变得透明。正如他已经提到的,zgc7009的解决方案太慢了。 – Kewitschka 2014-10-02 18:59:01

回答

1

您可以使用Canvas绘图。使用可变位图创建自定义视图。 擦除可以通过设置喷漆的对象是这样来实现:

Paint paint = new Paint(); 
paint.setStyle(Paint.Style.FILL); 
paint.setColor(Color.TRANSPARENT); 
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 

实现绘图:

@Override 
    protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
     super.onSizeChanged(w, h, oldw, oldh); 

     if (w > 0 && h > 0) { 


      // Set up canvas - bitmap can be initialized with your Image 
      mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
      mCanvas = new Canvas(mBitmap); 

     } 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 

     if (mBitmap == null || mCanvas == null || mPath == null) { 
      return; 
     } 
     mCanvas.drawPath(mPath, mPaint); 
     canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint); 

     canvas.drawPath(circlePath, circlePaint); 
    } 


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

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

    } 

    private void touchMove(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 touchUp() { 
     mPath.lineTo(mX + 1, mY + 1); 
     circlePath.reset(); 
     // commit the path to our offscreen 
     mCanvas.drawPath(mPath, mPaint); 
     // kill this so we don't double draw 
     mPath.reset(); 
    } 

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

     switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       touchStart(x, y); 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_MOVE: 
       touchMove(x, y); 
       invalidate(); 
       break; 
      case MotionEvent.ACTION_UP: 
       touchUp(); 
       invalidate(); 
       break; 
     } 
     return true; 
    } 
+0

有没有可能保存图纸? – Kewitschka 2014-10-02 21:43:45

+0

您可以保存一个由画布修改的位图,也可以创建用于以矢量方式保存路径的类 - 类似于点列表。 – JanKnotek 2014-10-02 21:46:12