How can I implement the onTouch android function?

Hi, I want to create an application that downloads a large image, and a simple gesture that I can move around it. I need to print an image, but I cannot implement onTouch so that it stays still. Any help came up. thanks

My code for creating a picture:

@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint paint = new Paint(); paint.setStyle(Paint.Style.FILL); // make the entire canvas white paint.setColor(Color.WHITE); canvas.drawPaint(paint); paint.setStrokeWidth(1); paint.setPathEffect(null); paint.setColor(Color.GRAY); paint.setAntiAlias(true); for (int i=1; i < 100; i++){ canvas.drawLine(0, i*1 , 600, i*20, paint); canvas.drawLine(i*1 ,0, i*20, 600, paint); } } 
+4
source share
2 answers

In your view, you need to create an "OnTouchListener" that 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 look at MotionEvent and look at different levels. You want to pay attention to how it can pack several bits of motion information into one MotionEvent.

+3
source

As you are already writing a custom view, instead of setting a listener, you can turn on the GestureDetector and the listener inside your view and, first of all, avoid switch(e.getAction()) , because the OnGestureListener enabled at a higher level and will provide you with an event already detected as gestures (scrolling, dropping, long press ...).

See an example here .

+6
source

Source: https://habr.com/ru/post/1341548/


All Articles