2016-08-08 32 views
1

拉我聽到這一個。我有一個背景位圖,我想覆蓋另一個位圖上有透明切口。如果切口是一個基本的形狀,我沒有問題,但我需要切口是兩個圓的交點(有點葉形)。我嘗試製作第三個位圖來生成剪切模板,但我甚至無法獲得剪切圖的清晰表示,更不用說將其作爲剪切模板工作。由相互作用產生的透明度

任何人都知道如何做這樣的事情?這是我(簡化)嘗試:

@Override 
public void draw(Canvas canvas) { 
    float w = canvas.getWidth(); 
    float h = canvas.getHeight(); 

    // just used to set some proportions 
    float off = 300f; 

    Paint paint = new Paint(); 

    // make a background bitmap with a yellow to green gradient 
    Bitmap bitmapBkg = Bitmap.createBitmap((int) w, (int) h, Bitmap.Config.ARGB_8888); 
    Canvas canvasBkg = new Canvas(bitmapBkg); 
    paint.reset(); 
    paint.setShader(new LinearGradient(0, h/2 - off, 0, h/2 + off, Color.YELLOW, Color.GREEN, Shader.TileMode.CLAMP)); 
    canvasBkg.drawRect(new RectF(0, 0, w, h), paint); 

    // make an overlay bitmap with a red to magenta gradient which will have the cutouts 
    Bitmap bitmapOver = Bitmap.createBitmap((int) w, (int) h, Bitmap.Config.ARGB_8888); 
    Canvas canvasOver = new Canvas(bitmapOver); 
    paint.reset(); 
    paint.setShader(new LinearGradient(0, h/2 - off, 0, h/2 + off, Color.RED, Color.MAGENTA, Shader.TileMode.CLAMP)); 
    canvasOver.drawRect(new RectF(0, 0, w, h), paint); 

    // make a bitmap of intersecting circles to be used as the cutout shape 
    Bitmap bitmapCut = Bitmap.createBitmap((int) w, (int) h, Bitmap.Config.ARGB_8888); 
    Canvas canvasCut = new Canvas(bitmapCut); 

    paint.reset(); 
    paint.setColor(Color.BLACK); 
    //paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR)); 
    canvasCut.drawCircle(w/2 - (off/2), h/2, off, paint); 

    paint.reset(); 
    paint.setColor(Color.BLACK); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR)); 
    canvasCut.drawCircle(w/2 + (off/2), h/2, off, paint); 

    // apply cutout to overlay 
    paint.reset(); 
    paint.setColor(Color.BLACK); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)); 
    canvasOver.drawBitmap(bitmapCut, 0, 0, paint); 

    // draw background and overlay onto main canvas 
    paint.reset(); 
    paint.setColor(Color.BLACK); 
    canvas.drawBitmap(bitmapBkg, 0, 0, paint); 
    canvas.drawBitmap(bitmapOver, 0, 0, paint); 
} 

這裏是我得到的圖像:

enter image description here

什麼,我試圖讓本來外側部分也紅品紅;只有中間的眼睛應該是黃綠色的。

+0

也許通過利用PorterDuff來組合圖像? http://stackoverflow.com/questions/8280027/what-does-porterduff-mode-mean-in-android-graphics-what-does-it-do –

+0

這就是我一直在嘗試使用。 – trans

+0

出了什麼問題? –

回答

0

原來的訣竅是增加了另一層。

// make a secondary overlay that cuts out the whole circles 
    Bitmap bitmapOver2 = Bitmap.createBitmap((int) w, (int) h, Bitmap.Config.ARGB_8888); 
    Canvas canvasOver2 = new Canvas(bitmapOver2); 
    paint.reset(); 
    paint.setShader(new LinearGradient(0, h/2 - off, 0, h/2 + off, Color.RED, Color.MAGENTA, Shader.TileMode.CLAMP)); 
    canvasOver2.drawRect(new RectF(0, 0, w, h), paint); 
    paint.reset(); 
    paint.setColor(Color.BLACK); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); 
    canvasOver2.drawCircle(w/2 - (off/2), h/2, off, paint); 
    canvasOver2.drawCircle(w/2 + (off/2), h/2, off, paint); 

應用它像這樣:

// draw background and overlay onto main canvas 
    paint.reset(); 
    paint.setColor(Color.BLACK); 
    canvas.drawBitmap(bitmapBkg, 0, 0, paint); 
    canvas.drawBitmap(bitmapOver2, 0, 0, paint); 
    canvas.drawBitmap(bitmapOver, 0, 0, paint); 

基本上它是一個有點一招。它將中間背景畫出兩次,一次是全圓切割,另一次是眼形切割。這兩者合在一起恰到好處地達到了預期的效果。

當然@Rotwang,你可能是對的。使用PatharcTo()將是一個更好的解決方案。我避免這種做法的唯一原因是B/C arcTo()是一個API 21+功能。到目前爲止,我已經管理好了將api保持在17+以上。但如果其他人願意提供一個完整的解決方案,這將是比較酷的。