How can I implement onTouch function android?

前端 未结 2 1191
后悔当初
后悔当初 2021-01-06 08:33

Hi I want to create aplication which loads large image, and simple gesture I can move across it. I have to image printed out but I can not implement onTouch so it remains st

相关标签:
2条回答
  • 2021-01-06 08:38

    Within your view, you need to create the "OnTouchListener" which should look something like this:

    myView.setOnTouchListener(new View.OnTouchListnener(){
        @Override
        public boolean onTouch(View v, MotionEvent e){
            switch(e.getAction()){
            case MotionEvent.ACTION_DOWN:
            //and code will go here for putting the finger on the screen
    

    I would have a look at MotionEvent and looking at the various levels. You'll want to pay attention to how it can pack several bits of movement information into one MotionEvent.

    0 讨论(0)
  • 2021-01-06 08:41

    As you're already writing a custom view, instead of setting a listener, you might want to incorporate a GestureDetector and listener inside your view, and above all avoid the switch(e.getAction()) thing, because OnGestureListener is on a higher level and will provide you with event already detected as a gesture (scroll, fling, long press...).

    See an example here.

    0 讨论(0)
提交回复
热议问题