OnTouch在图形页面只触发第一次图形、页面、OnTouch

由网友(只为等你守千年)分享简介:我想实现双击缩放就像在我图形页面的功能。该事件始终闪光第一次,但从来没有后续时间。下面是我的code。我有一种感觉它是与第一次的事件被触发后,在地图控制器迷路。I'm trying to implement a double-tap zoom like function in my MapView. The even...

我想实现双击缩放就像在我图形页面的功能。该事件始终闪光第一次,但从来没有后续时间。下面是我的code。我有一种感觉它是与第一次的事件被触发后,在地图控制器迷路。

I'm trying to implement a double-tap zoom like function in my MapView. The event always fires the first time, but never subsequent times. Below is my code. I have a feeling it has something to do with the map controller getting lost after the first time the event is fired.

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

public class mainmap extends MapActivity implements OnTouchListener{

    long lasttime = -1;
    MapController mapc;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapc = mapView.getController();
        mapView.setOnTouchListener(this);       
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){

            if(event.getEventTime()-lasttime<2000){
                mapc.zoomInFixing((int)event.getX(),(int)event.getY());             
            }
        }       
        lasttime=event.getEventTime();
        return true;
    }

  }

我也尝试过编辑OnTouch方法传入视图转换为图形页面,得到了控制器,而事件被触发。不过,我得到第一个事件的触发而不是后来的相同的结果。

I have also tried editing the OnTouch method to cast the incoming View to a MapView, getting the controller while the event is fired. However, I get the same results where the first event is fired but not subsequent ones.

public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){

            if(event.getEventTime()-lasttime<2000){
                ((MapView)v).getController().zoomInFixing((int)event.getX(), (int)event.getY());                
            }
        }       
        lasttime=event.getEventTime();
        return true;
    }

作为基本越好,我砍了所有的code在OnTouch方法和程序也只显示一个小面包的消息。

Being as basic as possible, I cut out all of the code in the OnTouch method and programmed it to simply display a small toast message.

public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN){

               Toast.makeText(this,"Down!",Toast.LENGTH_SHORT).show();

        }       

        return true;
}

这正常工作,的MapView每次轻触此显示吐司。

This works as expected, displaying the Toast each time the MapView is touched.

我不明白为什么事件会触发正确在这种情况下,但不是在我的previous实施。

I don't understand why the event will fire properly in this case but not in my previous implementation.

推荐答案

如果你正在使用这个方法mapView.setBuiltInZoomControls(真);那么你的触摸正在一次。

If you are using this method "mapView.setBuiltInZoomControls(true);" then your touch is working at once .

请删除该行并检查我相信它会工作。

Please remove that that line and check I am sure it will work..

在,如果你想BuiltInZoomControls那么你可以覆盖你像如下OnTouch方法。

In some case if you want BuiltInZoomControls then you can you OnTouch method of Overlay like as below..

public class MapOverlay extends Overlay {

    public MapOverlay(Context ctx) {super(ctx);}

    @Override
    protected void draw(Canvas c, MapView osmv, boolean shadow) { }

    @Override
    public boolean onTouchEvent(MotionEvent e, MapView mapView) {
        //Write yout touch code here..
        return false;
    }
}
阅读全文

相关推荐

最新文章