2011-12-18 199 views
3

我有一个mapview,我想绘制一个圆形以专注于给定的区域。但是我想要这个圆被倒置。也就是说,而不是填充圆的内部,它是透明的,其他一切都被填充。请参阅此图片以了解我的意思(http://i.imgur.com/zxIMZ.png)。上半部分显示了我可以用正常的圆圈做什么。底部显示“倒置”的圆圈。绘制透明圆形外部填充

我试过搜索,但很难找到我想要的东西。有谁知道我可以如何去做这样的事情?

+0

? – 2017-08-22 07:11:17

回答

1

您可以创建一个新的BufferedImage,将其填充为灰色,然后擦除所需的圆。

然后,在视图顶部绘制BufferedImage。

BufferedImage img = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_INT_RGBA); 
Graphics2D g = img.createGraphics(); 

int ovalX = 50; 
int ovalY = 70; 
int ovalRadius = 20; 

/* Draw the grey rectangle */ 
g.setColor(Color.GRAY); 
g.fillRect(0, 0, sizeX, sizeY); 

/* Enable Anti-Alias */ 
g.setRenderingHint(RenderingHints.HINT_ANTIALIAS, RenderingHints.VALUE_ANTIALIAS_ON); 

/* Clear the circle away */ 
g.setComposite(AlphaComposite.CLEAR, 1.0f); 
g.fillOval(ovalX - ovalRadius, ovalY - ovalRadius, 2 * ovalRadius, 2 * ovalRadius); 

g.dispose(); 
+0

+1。我想不出一个更好的解决方案 – GETah 2011-12-18 11:16:59

+3

据我所知,awt库在android中不可用,我找不到任何相当的东西可以做到这一点。 – user1104351 2011-12-18 14:49:57

+0

@ user1104351:哦,那很伤心。 BufferedImage是一个非常有用的强大类。这很伤心。我不熟悉Android。但我试图帮助... – 2011-12-18 15:17:23

13

我的回答很晚,但可能有助于某人达到此目的。 这里是一个例子,如何使半透明的视图与最小尺寸的透明圆匹配大小,放置在中间的小边距。它可以作为覆盖放置在任何视图上。

/* 
* Copyright (c) 2015 Singularex Inc. 
*/ 

package your_package.ui.widget; 

import android.annotation.TargetApi; 
import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.PorterDuff; 
import android.graphics.PorterDuffXfermode; 
import android.graphics.RectF; 
import android.os.Build; 
import android.util.AttributeSet; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 

import your_package.R; 

/** 
* @author Victor Kosenko 
*/ 
public class RadiusOverlayView extends LinearLayout { 
    private Bitmap windowFrame; 

    public RadiusOverlayView(Context context) { 
     super(context); 
    } 

    public RadiusOverlayView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public RadiusOverlayView(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public RadiusOverlayView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    protected void dispatchDraw(Canvas canvas) { 
     super.dispatchDraw(canvas); 

     if (windowFrame == null) { 
      createWindowFrame(); // Lazy creation of the window frame, this is needed as we don't know the width & height of the screen until draw time 
     } 

     canvas.drawBitmap(windowFrame, 0, 0, null); 
    } 

    protected void createWindowFrame() { 
     windowFrame = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); // Create a new image we will draw over the map 
     Canvas osCanvas = new Canvas(windowFrame); // Create a canvas to draw onto the new image 

     RectF outerRectangle = new RectF(0, 0, getWidth(), getHeight()); 

     Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); // Anti alias allows for smooth corners 
     paint.setColor(getResources().getColor(R.color.map_radius_outer)); // This is the color of your activity background 
     paint.setAlpha(84); 
     osCanvas.drawRect(outerRectangle, paint); 

     paint.setColor(Color.TRANSPARENT); // An obvious color to help debugging 
     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT)); // A out B http://en.wikipedia.org/wiki/File:Alpha_compositing.svg 
     float centerX = getWidth()/2; 
     float centerY = getHeight()/2; 
     float radius = Math.min(getWidth(), getHeight())/2 - getResources().getDimensionPixelSize(R.dimen.view_margin_small2); 
     osCanvas.drawCircle(centerX, centerY, radius, paint); 
    } 

    @Override 
    public boolean isInEditMode() { 
     return true; 
    } 

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     super.onLayout(changed, l, t, r, b); 

     windowFrame = null; // If the layout changes null our frame so it can be recreated with the new width and height 
    } 
} 

在这里,它的外观在我的情况:

Transparent circle overlay view.

+3

这对我很有帮助,但我遇到了一个问题。使用上面的代码,将圆形内部渲染为黑洞。 [这篇文章](http://stackoverflow.com/questions/11337679/porterduffxfermode-clear-a-section-of-a-bitmap)建议将以下内容添加到我的构造函数代码中进行修复: setLayerType(View。 LAYER_TYPE_SOFTWARE,null); – sidecarcat 2016-02-12 15:56:35

1

做,你会做画一个圆圈正常,但与补体的位置和补充半径。

像{0纬度,(180龙)180%,20037508.34半径}

你想掩盖了地图的“休息”,并留下了地图的只有一个圆形锁孔视图
+0

这是所有人唯一正确和完美的答案。需要被接受的! 但它需要是(0 - lat,lng - 180) – kaiser 2018-01-18 00:32:36