机器人 - 借鉴谷歌地图可调整大小的圆 [英] android - draw resizable circle on google map

查看:129
本文介绍了机器人 - 借鉴谷歌地图可调整大小的圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动用我的谷歌地图,而用户将能够扩大或使用触摸手势(例如缩小圈子中的用户将捏在屏幕上缩小一圈的顶部调整大小的圆圈,我希望它像地图放大/缩小选择的工作,只有刚刚圈将得到更大的/在地图上小)。
这是可能实现?如果是的话如何将我去完成的。

I'm trying to draw a resizable circle on top of my google map, which the user will be able to expand or shrink using touch gestures (for example to shrink the circle the user will pinch the circle on the screen,I want it to work like zooming in/out option in the map, only that just the circle will get bigger/smaller on the map). Is this possible to implement? and if so how would i go about accomplishing that.

我搜索谷歌和#1和我的理解,我需要添加在我的地图片段的顶部自定义视图和执行OnTouchListener这个视图(这仅仅是个开始)。可有人请告知做什么或如何进行?
我可以在地图上绘制一个圆,但我不知道如何得到它的响应触摸事件。

I searched Google and Stackoverflow and as i understand, i need to add a custom view on top of my map fragment and implement OnTouchListener to this View (and that is just the beginning). can some one please advise on what to do or how to proceed? i can draw a circle on the map but i don't know how to get it to respond to touch events.

在此先感谢

推荐答案

根据你的问题,你要覆盖一个掐听观点,即绘制基于捏一个椭圆形。我做了一些不良的考验code用于此目的,因为你需要适应它:

Based on your question, you want to overlay a "pinch listening" view that draws an oval shape based on the pinch. I made some poorly-tested code for this purpose, adapt it as you need:

MainLayout:

MainLayout:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Replace the ImageView with your MapView or whatever you are 
         overlaying with the oval shape -->
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="#F00" />

    <com.example.testapp.CircleTouchView
        android:id="@+id/circle_drawer_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

CircleTouchView:

CircleTouchView:

public class CircleTouchView extends View {
private static final int MODE_PINCH = 0;
private static final int MODE_DONT_CARE = 1;

ShapeDrawable mCircleDrawable;
int mTouchMode = MODE_DONT_CARE;

public CircleTouchView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mCircleDrawable = new ShapeDrawable(new OvalShape());
    mCircleDrawable.getPaint().setColor(0x66FFFFFF);
}

public CircleTouchView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CircleTouchView(Context context) {
    this(context, null, 0);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        mCircleDrawable.setBounds(0, 0, 0, 0);
        invalidate();
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        prepareCircleDrawing(event);
        break;
    case MotionEvent.ACTION_MOVE:
        if (mTouchMode == MODE_PINCH) {
            prepareCircleDrawing(event);
        }
        break;
    case MotionEvent.ACTION_POINTER_UP:
        if (event.getActionIndex() <= 1) {
            mTouchMode = MODE_DONT_CARE;
        }
        break;
    default:
        super.onTouchEvent(event);
    }

    return true;
}

private void prepareCircleDrawing(MotionEvent event) {
    int top, right, bottom, left;
    int index = event.getActionIndex();

    if (index > 1) {
        return;
    }
    mTouchMode = MODE_PINCH;
    if (event.getX(0) < event.getX(1)) {
        left = (int) event.getX(0);
        right = (int) event.getX(1);
    } else {
        left = (int) event.getX(1);
        right = (int) event.getX(0);
    }

    if (event.getY(0) < event.getY(1)) {
        top = (int) event.getY(0);
        bottom = (int) event.getY(1);
    } else {
        top = (int) event.getY(1);
        bottom = (int) event.getY(0);
    }

    mCircleDrawable.setBounds(left, top, right, bottom);

    invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    mCircleDrawable.draw(canvas);
}
}

如果你想有一个完美的圆,而不是椭圆形,改变prepareCircleDrawing()方法,它需要事件0和1之间的X和Y的最小值。

If you want a perfect circle instead of an oval shape, change the prepareCircleDrawing() method so that it takes the smallest values for X and Y between event 0 and 1.

编辑:你可以在调用 mCircleDrawable.setBounds(左,上,右,下)前添加下面的代码片段; 来画一个完美的圆。还有其他的方法绘制圆,这取决于你想如何做人。

you can add the snippet below before calling mCircleDrawable.setBounds(left, top, right, bottom); to draw a perfect circle. There are other ways for drawing circles, it depends on how you want it to behave.

int height = bottom - top;
int width = right - left;

if (height > width) {
    int delta = height - width;
    top += delta / 2;
    bottom -= delta / 2;
} else {
    int delta = width - height;
    left += delta / 2;
    right -= delta / 2;
}

希望我自己清楚,至于

Hope I made myself clear, regards.

这篇关于机器人 - 借鉴谷歌地图可调整大小的圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆