Android Circular Gradient Alpha Mask

前端 未结 1 1679
情书的邮戳
情书的邮戳 2020-12-23 12:58

Is there a way to draw a circular gradient mask on a bitmap in Android? Trying to produce something similar to a foggy window. Click the window and a transparent circle sh

相关标签:
1条回答
  • 2020-12-23 13:28
    private void drawFoggyWindowWithTransparentCircle(Canvas canvas,
            float circleX, float circleY, float radius) {
    
        // Get the "foggy window" bitmap
        BitmapDrawable foggyWindow = 
            (BitmapDrawable) getResources().getDrawable(R.drawable.foggy_window);
        Bitmap foggyWindowBmp = foggyWindow.getBitmap();
    
        // Create a temporary bitmap
        Bitmap tempBitmap = Bitmap.createBitmap(
                foggyWindowBmp.getWidth(),
                foggyWindowBmp.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas tempCanvas = new Canvas(tempBitmap);
    
        // Copy foggyWindowBmp into tempBitmap
        tempCanvas.drawBitmap(foggyWindowBmp, 0, 0, null);
    
        // Create a radial gradient
        RadialGradient gradient = new android.graphics.RadialGradient(
                circleX, circleY,
                radius, 0xFF000000, 0x00000000,
                android.graphics.Shader.TileMode.CLAMP);
    
        // Draw transparent circle into tempBitmap
        Paint p = new Paint();
        p.setShader(gradient);
        p.setColor(0xFF000000);
        p.setXfermode(new PorterDuffXfermode(Mode.DST_OUT));
        tempCanvas.drawCircle(circleX, circleY, radius, p);
    
        // Draw tempBitmap onto the screen (over what's already there)
        canvas.drawBitmap(tempBitmap, 0, 0, null);
    }
    
    0 讨论(0)
提交回复
热议问题