2010-07-23 74 views
4

我正在爲Android繪製應用程序。現在我想實現一個橡皮擦,允許通過使用觸摸輸入刪除部分加載的位圖,並使位圖沿手指路徑透明。通過觸摸輸入在位圖上繪製透明線

Android應用程序Steamy Window中顯示了我嘗試實現的一個非常好的示例。 Steamy Window模擬一個模糊的窗口,用戶可以通過觸摸輸入擦除部分霧。

任何建議如何實現該功能?

更新:我已經發布了我當前代碼下面最重要的部分。由於以下原因,我對此並不滿意:

  1. 繪圖非常緩慢。我可以在這裏改進什麼?

  2. 我正在尋找一種方法來使用alpha蒙版,以設置像素的像素透明度/ alpha,因爲我想模擬畫筆。任何建議如何實現?

 
     public DrawView(Context context, String imageName) { 

     super(context); 

     // Get the overlay image and its dimensions 
     Bitmap overlayImageOriginal = BitmapFactory.decodeResource(
       getResources(), R.drawable.overlay); 
     width = overlayImageOriginal.getWidth(); 
     height = overlayImageOriginal.getHeight(); 

     // Get a copy of the overlay image and recycle the original 
     overlayImage = overlayImageOriginal.copy(Config.ARGB_8888, true); 
     overlayImageOriginal.recycle(); 

     // Get background image of this level 
     int resId = getResources().getIdentifier(imageName, "drawable", 
       "com.xxx"); 
     backgroundImage = BitmapFactory.decodeResource(getResources(), resId); 

     // Copy pixels 
     overlayImagePixels = new int[width * height]; 
     overlayImage.getPixels(overlayImagePixels, 0, width, 0, 0, width, 
       height); 

     getHolder().addCallback(this); 
     _thread = new DrawThread(getHolder(), this); 
     setFocusable(true); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     synchronized (_thread.getSurfaceHolder()) { 
      switch (event.getAction()) { 
      case MotionEvent.ACTION_DOWN: 
       touchStart(event.getX(), event.getY()); 
       return true; 
      case MotionEvent.ACTION_MOVE: 
       touchMove(event.getX(), event.getY()); 
       return true; 
      case MotionEvent.ACTION_UP: 
       touchStop(); 
       return true; 
      } 
      return false; 
     } 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     // draw the image to guess 
     canvas.drawBitmap(backgroundImage, 0, 0, null); 
     // apply user input to the overlay-image 
     setRegionTransparency((int) mTouchX, (int) mTouchY, 20, 0); 
     // draw the overlay-image (hiding the image to guess) 
     canvas.drawBitmap(overlayImage, 0, 0, null); 
    } 


    /** 
    * 
    * @param posX 
    * @param posY 
    * @param radius 
    * @param alpha - 0: full transparent, 255: full opaque 
    */ 
    private void setRegionTransparency(int posX, int posY, int radius, int alpha) { 
     int left = posX - radius; 
     int right = posX + radius; 
     int top = posY - radius; 
     int bottom = posY + radius; 
     int color = 0; 

     // onDraw() is called before any user input 
     // -> assume 0/0 as invalid arguments 
     if (posX == 0 && posY == 0) { 
       return; 
     } 

     // TODO currently only rectangular area is supported 
     // but we need circular 
     // we should use a pre-made alpha mask with smoothed edges 
     // we should take into account: pressure and/or duration of touch 

     for (int y = top; y = 0 && y = 0 && x = TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) { 
      mPath.quadTo(mTouchX, mTouchY, (x + mTouchX)/2, (y + mTouchY)/2); 
      mTouchX = x; 
      mTouchY = y; 
     } 
    } 
} 


+0

的背景是什麼應該是什麼樣子的?你可以做的是在所需位置的前景上繪製背景。 – Maz 2010-07-23 15:02:28

+0

您是否嘗試使用onDraw方法添加Alpha?你得到像素顏色,並且你在00到FF之間改變它的alpha值? – Sephy 2010-07-23 15:30:12

+0

我打算使用兩個位圖:一個作爲前景,一個作爲背景。 覆蓋位圖的部分應該在繪製時被「擦除」,即重疊位圖的像素的alpha值被改變。 – Philipp 2010-07-28 10:45:02

回答