gpt4 book ai didi

android - 使用双击和捏合放大 android 时如何使谷歌居中?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:52:16 32 4
gpt4 key购买 nike

我在使用双击和双指缩放进行缩放时搜索了谷歌地图中心。我为 Here 找到了一个解决方案, 这仅适用于双击缩放解决方案。

TouchableWrapper

 public class TouchableWrapper extends FrameLayout {

GestureDetectorCompat mGestureDetector;


public TouchableWrapper(Context context) {
super(context);
mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
}


private final GestureDetector.SimpleOnGestureListener mGestureListener
= new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.e("GestureDetector", "Executed");
//Notify the event bus (I am using Otto eventbus of course) that you have just received a double-tap event on the map, inside the event bus event listener
EventBus_Singleton.getInstance().post(new EventBus_Poster("double_tapped_map"));

return true;
}
};

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
mGestureDetector.onTouchEvent(ev);
return super.onInterceptTouchEvent(ev);
}


}

我用过Otto用于事件触发器。

public class EventBus_Singleton {

private static final Bus BUS = new Bus();

public static Bus getInstance() {
return BUS;
}

public EventBus_Singleton() {
}

public void post(String s) {
BUS.post(s);
}
}

EventBus_Poster

public class EventBus_Poster {
public final String string;

EventBus_Poster(String string) {
this.string = string;
}
}

在我的 map Activity 中,我使用事件接收方法在同一位置缩放 map 。

 @Subscribe
public void EventBus_singleton(EventBus_Poster event) {
Log.e("EventBus_singleton", "Executed");
if (event != null && event.string.equals("double_tapped_map")) {
if (mapFragment != null) {
mapFragment.getMap().getUiSettings().setZoomGesturesEnabled(false);
mapFragment.getMap().getUiSettings().setRotateGesturesEnabled(false);

mapFragment.getMap().setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
LatLng lng = cameraPosition.target;
Log.e("LatLng", "" + lng);
}
});
mapFragment.getMap().animateCamera(CameraUpdateFactory.zoomIn(), 400, null);
}

}
}

我曾尝试使用 ScaleGestureDetector.SimpleOnScaleGestureListener 进行捏合缩放,但它没有用,而且有时它会滞后。谁能帮我在谷歌地图中进行缩放?

最佳答案

好吧,我发现在尝试执行此操作时,成功的双指缩放涉及速度比例 条件。测量速度以确定你是否真的想要缩放,这取决于你将两根手指移开或靠近的速度,你可以在谷歌地图和 Uber 的应用程序中尝试这个,你实际上可以保持手指远离或靠近彼此靠近但速度很慢,以至于您不会触发 map 上的放大/缩小事件!

因此,在尝试确定完整的捏合手势时肯定涉及速度。第二,当然你也需要比例,我已经打印出比例值,它总是小于正好1.0当你开始捏缩小并且总是大于1.0,当您开始捏合以放大时。

这两者的结合肯定会给你带来更好的结果,我会说先看速度

private final ScaleGestureDetector.OnScaleGestureListener mScaleGestureListener
= new ScaleGestureDetector.SimpleOnScaleGestureListener() {
/**
* This is the active focal point in terms of the viewport. Could be a local
* variable but kept here to minimize per-frame allocations.
*/

float startingSpan;
float startFocusX;
float startFocusY;

@Override
public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
startingSpan = scaleGestureDetector.getCurrentSpan();
startFocusX = scaleGestureDetector.getFocusX();
startFocusY = scaleGestureDetector.getFocusY();

return true;
}

@Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
float scale = scaleGestureDetector.getCurrentSpan() / startingSpan;

mVelocityTracker.computeCurrentVelocity(1000);

Log.d("VELOCITY", "X vel : " + mVelocityTracker.getXVelocity());
Log.d("VELOCITY", "Y vel : " + mVelocityTracker.getYVelocity());

if (scale <= 1.0) {
EventBus_Singleton.getInstance().post(new EventBus_Poster("pinched_map", "out"));
} else {
EventBus_Singleton.getInstance().post(new EventBus_Poster("pinched_map", "in"));
}

return true;
}
};

关于android - 使用双击和捏合放大 android 时如何使谷歌居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31353743/

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