移动屏幕

时间:2017-05-10 15:34:15

标签: android android-studio

我希望有一个布局(实物并不重要),从屏幕的实际尺寸乘以5的实际大小将增加5.我创建了一个使用画布并绘制我想要的画面的浏览视图,但是如何通过触摸移动屏幕,这样我就可以看到屏幕的每个部分?我怎样才能知道哪些视图已被点击(我希望在点击时会发生什么事情)?

1 个答案:

答案 0 :(得分:0)

在我看来,您希望使用OnTouchListenerView.onTouchEvent(MotionEvent ev)来检测触摸并采取相应的行动:

例如:

myView.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()){
            case MotionEvent.ACTION_UP:
                // Perform click operation
                return true;
            case MotionEvent.ACTION_MOVE:
                // Move to another area based on the event
                return true;
        }
        return false;
    }
});

或在课堂内:

int downX = -1;
int downY = -1;

public boolean onTouchEvent(MotionEvent event) {

    int eventAction = event.getAction();

    // You may need the x/y location
    int x = (int)event.getX();
    int y = (int)event.getY();

    // put your code in here to handle the event
    switch (eventAction) {
        case MotionEvent.ACTION_DOWN:
            downX = x;
            downY = y;
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            downX = -1;
            downY = -1;
            // decide what to do here about movement
            break;
        case MotionEvent.ACTION_MOVE:
            int deltaX = x - downX;
            int deltaY = y - downY;
            // This is one way of doing it, it might not be exactly what you want, but you can start from her
            setTranslationX(getTranslationX() + deltaX);
            setTranslationY(getTranslationY() + deltaY);
            break;
    }

    return true;
}