2011-12-18 37 views
3

私は、特定の領域に焦点を合わせるために円を描画したいmapviewを持っています。しかし、私は円を逆転させたい。つまり、円の内側が塗りつぶされるのではなく、透明で、他のすべてが塗りつぶされています。私が意味することについてはこの図を参照してください(http://i.imgur.com/zxIMZ.png)。上半分は、私が通常の円で何ができるかを示しています。ボトムは「反転した」円を示す。外側に塗りつぶした透明円を描く

私は検索しようとしましたが、私が欲しいものを見つけるのは難しいです。誰かがこのようなことをやっていく方法を知っていますか?

+0

あなたは、マップの「休止」とマップの唯一の円形鍵穴ビューを残すマスクアウトしたいですか? –

答えて

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

+3

私が知っている限り、awtライブラリはアンドロイドでは利用できず、私はこれを行うことができる同等のものを見つけることができません。 – user1104351

+0

@ user1104351:ああ、それは悲しいことだ。 BufferedImageはそのような強力なクラスです。それは非常に悲しいです。私はAndroidに慣れていない。しかし、私は助けようとしました... –

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

これは私にとって非常に役に立ちましたが、私は1つの問題に遭遇しました。上記のコードを使用すると、円の内側がブラックホールとしてレンダリングされます。 [この記事](http://stackoverflow.com/questions/11337679/porterduffxfermode-clear-a-section-of-a-bitmap)私のコンストラクタのコードを修正するために、次のコードを追加することをお勧めします: setLayerType(View。 LAYER_TYPE_SOFTWARE、null); – sidecarcat

1

あなたは、通常の円を描くが、補体の位置および補体半径とするだろうとしています。

と同様に{0-緯度、(180-ロング)%180、20037508.34半径}

+0

これは、すべての唯一の正確で完璧な答えです。受け入れられる必要があります! しかし、それは(0 - lat、lng - 180)である必要があります – kaiser

関連する問題