gpt4 book ai didi

Android - CircleView 内的谷歌地图

转载 作者:可可西里 更新时间:2023-11-01 11:42:39 25 4
gpt4 key购买 nike

我想在圆形 View 内显示 map ,其中圆形的外部区域填充了一种颜色。我提到了一个帖子 Draw transparent circle filled outside .但现在的问题是触摸事件。可以通过外部圆形 View 触摸 map ,而我需要只能在圆形 View 内部( map 可见的地方)触摸(缩放或移动) map 。

我试过的,

  1. setEnabled=false
  2. 可点击=假

但仍然从外部圆形 View 触摸 map 。

是否有可能实现可以从圆形 View 内触摸 map 。

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>

结果: enter image description here

任何帮助将不胜感激。

最佳答案

您可以为您的 RadiusOverlayView 设置一个 View.OnTouchListener 并计算 RadiusOverlayView 是否需要管理触摸事件。在此示例中,我通过测试触摸的 RadiusOverlayView 颜色是否为 != 0 来计算此值(也许您想改进它):

final RadiusOverlayView radiusOverlayView = (RadiusOverlayView) findViewById(R.id.radiusView);
radiusOverlayView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent motionEvent) {
view.setDrawingCacheEnabled(true);
Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

return bmp.getPixel((int) motionEvent.getX(), (int) motionEvent.getY()) != 0;
}
});

关于Android - CircleView 内的谷歌地图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35647057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com