Я пытаюсь переместить свой ImageView вверх, когда смахиваю вверх. Он работает, но единственное, что мой ObjectAnimation перемещает ImageView в соответствии с позицией X, Y изображения, а не расстоянием. Я хотел бы иметь возможность перемещать изображение с расстояния 50dp, например, по X или Y. Не могли бы вы мне помочь?
package com.example.swipe2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GestureDetectorCompat;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements GestureDetector.OnGestureListener {
public static final int SWIPE_THRESHOLD = 100;
public static final int SWIPE_VELOCITY_THRESHOLD = 100;
private GestureDetector mDetector;
private ImageView pablo;
long animationDuration = 1000;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the gesture detector with the
// application context and an implementation of
// GestureDetector.OnGestureListener
mDetector = new GestureDetector(this, this);
pablo = (ImageView) findViewById(R.id.pablo);
pablo.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(final View view, final MotionEvent event) {
mDetector.onTouchEvent(event);
return true;
}
});
}
@Override
public boolean onDown(MotionEvent motionEvent) {
return true;
}
@Override
public void onShowPress(MotionEvent motionEvent) {
}
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return false;
}
@Override
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) {
return false;
}
@Override
public void onLongPress(MotionEvent motionEvent) {
}
@Override
public boolean onFling(MotionEvent downEvent, MotionEvent moveEvent, float velocityX, float velocityY) {
boolean result = false;
float diffY = moveEvent.getY() - downEvent.getY();
float diffX = moveEvent.getX() - downEvent.getX();
// which was greater? movement across Y or X?
if (Math.abs(diffX) > Math.abs(diffY)) {
// right or left swipe
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
result = true;
}
} else {
// up or down swipe
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
result = true;
}
}
return result;
}
private void onSwipeTop() {
Toast.makeText(this, "Swipe Top", Toast.LENGTH_SHORT).show();
ObjectAnimator animatorTOP = ObjectAnimator.ofFloat(pablo,"Y", +10);
animatorTOP.setDuration(animationDuration);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(animatorTOP);
animatorSet.start();
}
private void onSwipeBottom() {
Toast.makeText(this, "Swipe Bottom", Toast.LENGTH_SHORT).show();
}
private void onSwipeLeft() {
Toast.makeText(this, "Swipe Left", Toast.LENGTH_SHORT).show();
}
private void onSwipeRight() {
Toast.makeText(this, "Swipe Right", Toast.LENGTH_SHORT).show();
}
}