Imageview animation start on Swipe gesture
You have to use an OnTouchListener and a GestureDetector
Here is an example:
package com.example.androidlayout;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView iv = (ImageView) findViewById(R.id.imageview);
final GestureDetector mDetector = new GestureDetector(MainActivity.this, new GestureListener());
iv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mDetector.onTouchEvent(event);
return true;
}
});
}
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// Right to left
return false;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
// Left to right
return false;
}
if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
// Bottom to top
//Your code to flip the coin
Log.println(Log.VERBOSE,"Coin", "Coin flipped");
return false;
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
// Top to bottom
return false;
}
return false;
}
}
}
I've test it on the emulator, and it works just fine.
Android - How to move ImageView on Swipe
I found a simple solution as below:
For Right Swipe(towards Right):
btnSwipe.setTranslationX(e2.getX());
For Left Swipe(towards Left)"
btnSwipe.setTranslation(e1.getX());
Now, the ImageView is transiting from Right to Left and Left to Right horizontally. But this works from Android API
Level 11 onwards.
fling and pinchzoom for an imageview in one activity
Assuming you already researched separately on pinch-to-zoom and swipe gestures, you may want to impose limitations on useability. When the image is zoomed (occupying an area greater than the screen size), you should disable swipe detection, and enable it again once the image size is less than the screen size.
How to add a gesture detector to a view in Android
This example shows how to add a gesture detector to a view. The layout is just a single View
inside of an Activity. You can use the same method to add a gesture detector to any type of view.
We will add the gesture detector to the green View
.
MainActivity.java
The basic idea is to add an OnTouchListener
to the view. Normally we would get all the raw touch data here (like ACTION_DOWN
, ACTION_MOVE
, ACTION_UP
, etc.), but instead of handling it ourselves, we will forward it on to a gesture detector to do the interpretation of the touch data.
We are using a SimpleOnGestureListener
. The nice thing about this gesture detector is that we only need to override the gestures that we need. In the example here I included a lot of them. You can remove the ones you don't need. (You should always return true
in onDown()
, though. Returning true means that we are handling the event. Returning false will make the system stop giving us any more touch events.)
public class MainActivity extends AppCompatActivity {
private GestureDetector mDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// this is the view we will add the gesture detector to
View myView = findViewById(R.id.my_view);
// get the gesture detector
mDetector = new GestureDetector(this, new MyGestureListener());
// Add a touch listener to the view
// The touch listener passes all its events on to the gesture detector
myView.setOnTouchListener(touchListener);
}
// This touch listener passes everything on to the gesture detector.
// That saves us the trouble of interpreting the raw touch events
// ourselves.
View.OnTouchListener touchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// pass the events to the gesture detector
// a return value of true means the detector is handling it
// a return value of false means the detector didn't
// recognize the event
return mDetector.onTouchEvent(event);
}
};
// In the SimpleOnGestureListener subclass you should override
// onDown and any other gesture that you want to detect.
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent event) {
Log.d("TAG","onDown: ");
// don't return false here or else none of the other
// gestures will work
return true;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Log.i("TAG", "onSingleTapConfirmed: ");
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Log.i("TAG", "onLongPress: ");
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.i("TAG", "onDoubleTap: ");
return true;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Log.i("TAG", "onScroll: ");
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d("TAG", "onFling: ");
return true;
}
}
}
It is a quick setup to run this project, so I recommend you try it out. Notice how and when the log events occur.
Related Topics
How to Check the Multiple Permission at Single Request in Android M
Android Gridview Row Dividers/Separators
How to Filter Listview Data When Typing on Edittext in Android
Play Sound Using Soundpool Example
Proguard Ignores Config File of Library
How to Use Front and Back Camera at Same Time in Android
How to Write a Custom Filter for Listview with Arrayadapter
Oauth 2.0 Authorization for Linkedin in Android
What Is a Full Android Database Helper Class for an Existing SQLite Database
How to Scroll to Bottom in a Scrollview on Activity Startup
How to Check If Facebook Is Installed Android
Room Persistence: Error:Entities and Pojos Must Have a Usable Public Constructor
How to Order My SQLite Database in Descending Order, for an Android App
How to Get Profile Like Gender from Google Signin in Android
Classloader Referenced Unknown Path: /Data/App/