Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓-谷歌地图圈内视图_Android_Google Maps_Android Canvas_Android Custom View_Android Maps V2 - Fatal编程技术网

Android 安卓-谷歌地图圈内视图

Android 安卓-谷歌地图圈内视图,android,google-maps,android-canvas,android-custom-view,android-maps-v2,Android,Google Maps,Android Canvas,Android Custom View,Android Maps V2,我想在圆形视图中显示地图,圆形的外部区域用颜色填充。我推荐了一个帖子。但现在的问题是触摸事件。地图可以通过外圆视图触摸,而我需要的地图只能从内圆视图(地图可见)触摸(缩放或移动) 我所尝试的, setEnabled=false 可点击=错误 但仍然可以从外圆视图触摸地图 public class RadiusOverlayView extends LinearLayout { private Bitmap windowFrame; public RadiusOverlayVie

我想在圆形视图中显示地图,圆形的外部区域用颜色填充。我推荐了一个帖子。但现在的问题是触摸事件。地图可以通过外圆视图触摸,而我需要的地图只能从内圆视图(地图可见)触摸(缩放或移动)

我所尝试的,

  • setEnabled=false
  • 可点击=错误
  • 但仍然可以从外圆视图触摸地图

    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);
        }
    
        @Override
        public boolean isEnabled() {
            return false;
        }
    
        @Override
        public boolean isClickable() {
            return false;
        }
    
        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(Color.CYAN); // This is the color of your activity background
            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 - 50;
            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
        }
    }
    
    这是可能实现的,地图可以从内圆视图触摸

    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);
        }
    
        @Override
        public boolean isEnabled() {
            return false;
        }
    
        @Override
        public boolean isClickable() {
            return false;
        }
    
        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(Color.CYAN); // This is the color of your activity background
            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 - 50;
            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
        }
    }
    

    XML布局

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
        <!--loading map in container-->
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
        <mypackage.RadiusOverlayView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            />
    </RelativeLayout>
    
    
    
    结果:


    任何帮助都将不胜感激

    您可以将
    视图设置为您的
    RadiusOverlayView
    ,并计算
    RadiusOverlayView
    是否需要管理触摸事件。 在本例中,我通过测试是否触摸了
    RadiusOverlayView
    颜色来计算它!=0(可能您希望对此进行改进):


    只是澄清一下,如果我总是返回false,它就不会向右移动吗?不,如果你总是返回true,地图就不会移动。返回true意味着RadiusOverlayView的侦听器已使用该事件,从而避免将其传播到映射。唯一困难的是找到圆内外的xy(圆形状)并返回布尔值。你能帮我看一下吗,这样我就可以确保触摸发生在圆圈区域之外。我的示例的颜色检查是不够的?对不起,我的错误,我不理解返回值,但现在我理解了。。它工作得很好。