0

I've figured out how to work with the ontouch listener, but I still some problems. I want to animate an 2D Map viewed from bird perspective, the viewed area is calculated by an set coordinate which is in the middle of the screen. I want to get the distance which had been moved in the event and change the position of the middle coordinate by these values. But it seems to work only once, did I made anything wrong with the listener?

Code:

Activity implemets OnTouchListener:

    @SuppressWarnings("static-access")
@Override
public boolean onTouch(View v, MotionEvent e) {
    switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            startX = e.getX();
            startY = e.getY();
            gb.movedDist.put("startX", startX);
            gb.movedDist.put("startY", startY);
            break;
        case MotionEvent.ACTION_UP:
            finX = e.getX();
            finY = e.getY();
            gb.movedDist.put("finX", finX);
            gb.movedDist.put("finY", finY);
            break;
    }
    return false;
}

}

Custom SurfaceView, implements runnable:

        @Override
    public void run() {
        frame.removeCallbacks(thread);

        while(isRunning) {
            if(!holder.getSurface().isValid())
                continue;
            setMoveValues();
            handleTouch();

            Canvas canvas = holder.lockCanvas();

            p.setColor(Color.BLACK);
            for(Coordinate dC : ch.getVisibleCoords()) {
              drawLandscape(dC, canvas);
            }
            drawItems(canvas);
            drawBeings(canvas);
            drawGUI(canvas);

            holder.unlockCanvasAndPost(canvas);

// frame.postDelayed(thread, FRAME_RATE); }

    public void handleTouch() {
        int distX = (int) Math.floor(startX - finX);
        int distY = (int) Math.floor(startY - finY);
        if(distX <= 0 || distY <= 0) return;
        if(distX > sh.getTSize() && distY > sh.getTSize()) {
            int rectDistX = distX/sh.getTSize();
            int rectDistY = distY/sh.getTSize();
            boolean checkingEdges = true;
            while(checkingEdges) {
                if(rectDistX > 0 && animateMap.checkRightEdgeVisible())
                    break;
                if(rectDistX < 0 && animateMap.checkLeftEdgeVisible())
                    break;
                if(rectDistX > 0 && animateMap.checkBottomEdgeVisible())
                    break;
                if(rectDistY < 0 && animateMap.checkTopEdgeVisible())
                    break;
                animateMap.animateXYDistance(rectDistX, rectDistY);
                checkingEdges = false;
            }
        }
        else {
            animateMap.tellCoordinate(finX, finY);
        }
    }

    public void setMoveValues() {
        startX = movedDist.get("startX");
        startY = movedDist.get("startY");
        finX = movedDist.get("finX");
        finY = movedDist.get("finY");           
    }

Thank you very much :)

4

1 回答 1

0

对于有相同问题的每个人,请使用“MotionEvent.ACTION_MOVE”,它只会在您移动的整个过程中返回值,您是否可以创建像谷歌地图这样的地图。

于 2013-05-03T18:35:34.967 回答