Android圆形渐变Alpha蒙版

时间:2010-11-11 22:35:46

标签: android

有没有办法在Android中的位图上绘制圆形渐变蒙版?试图产生类似于雾窗的东西。单击窗口,透明的圆圈显示窗口后面的显示内容。可以使用渐变,因此圆的中心是完全透明的,离中心越远,透明度越低。这可能吗?

我是Android的新手,所以我们都会感谢任何代码示例。

感谢。

1 个答案:

答案 0 :(得分:20)

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);
}