2017-10-12 147 views
0

我開發了一個繪圖應用程序,我需要執行橡皮擦工具來清除特殊區域的圖片。橡皮擦必須擦除一些不均勻的圓形區域。這意味着 - 只有圓的中心應該被完全擦除,並且圓的邊緣應該被部分地刪除,用alpha。以特殊位圖的形式清除畫布區域

爲了實現這種行爲我創建的representes橡皮擦的身影

enter image description here

然後,抹去針對某區域的圖像,我畫上面的一些圖片這樣的橡皮擦的身影:

private val picture: Bitmap 
    private val bgPaint = Paint() 
    private var eraser: Bitmap 

    private val eraserPaint: Paint = Paint().apply { 
    val mode: PorterDuff.Mode = PorterDuff.Mode.SRC_OUT 
    xfermode = PorterDuffXfermode(mode) 
    color = Color.TRANSPARENT 
    alpha = 0 
    } 

    init { 
    val options = BitmapFactory.Options() 
    options.inMutable = true 
    picture = BitmapFactory.decodeResource(resources, R.drawable.petushara, options) 
    eraser = BitmapFactory.decodeResource(resources, R.drawable.eraser, options) 
    setLayerType(View.LAYER_TYPE_SOFTWARE, bgPaint) 
    } 

    override fun draw(canvas: Canvas) { 
    super.draw(canvas) 
    canvas.drawBitmap(picture, 0f, 0f, bgPaint) 
    canvas.drawBitmap(eraser, 100f, 300f, eraserPaint) 
    } 

這是整個我的視圖類,不包括構造函數。我需要畫面的該區域將被橡皮擦的身影被刪除,但刪除用方的數字是這樣的:

enter image description here

回答

0

好吧,我剛剛找到了解決辦法。像這樣寫代碼:

private val picture: Bitmap 
    private val bgPaint = Paint() 
    private var eraser: Bitmap 

    private val eraserPaint: Paint = Paint().apply { 
    val mode: PorterDuff.Mode = PorterDuff.Mode.XOR 
    xfermode = PorterDuffXfermode(mode) 
    } 

    init { 
    val options = BitmapFactory.Options() 
    options.inMutable = true 
    picture = BitmapFactory.decodeResource(resources, R.drawable.petushara, options) 
    eraser = BitmapFactory.decodeResource(resources, R.drawable.eraser, options) 
    setLayerType(View.LAYER_TYPE_SOFTWARE, bgPaint) 
    } 

    override fun draw(canvas: Canvas) { 
    super.draw(canvas) 
    canvas.drawBitmap(picture, 0f, 0f, bgPaint) 
    canvas.drawBitmap(eraser, 100f, 300f, eraserPaint) 
    } 

然後導致圖片看起來像我的期望:

enter image description here

+0

其實,這不是我最好的解決方案。如果您嘗試擦除已被擦除的區域,則此區域將以擦除圖像顏色着色。黑色在我們的案件 –