你好开发人员, 我想要构建的是Anroid Studio上的3D查看器,在大约30帧的FrameAnimation之后,您可以使用滑动手势处理帧。我的意思是我想通过滑动手势移动框架动画的帧。向左滑动将让我回到之前的帧并向右滑动将引导我到下一帧。到目前为止我在这里:
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnTouchListener {
private ImageView view;
private AnimationDrawable frameAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Type casting the Image View
view = (ImageView) findViewById(R.id.imageView);
// Setting animation_list.xml as the background of the image view
view.setBackgroundResource(R.drawable.frame_animation_list_intro);
// Type casting the Animation drawable
frameAnimation = (AnimationDrawable) view.getBackground();
//set true if you want to animate only once
frameAnimation.setOneShot(true);
view.setOnTouchListener(this);
frameAnimation.start();
}
@Override
public boolean onTouch(View view, MotionEvent event){
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
view.setBackgroundResource(R.drawable.frame_animation_list_swipe);
frameAnimation = (AnimationDrawable) view.getBackground();
frameAnimation.setOneShot(true);
break;
case MotionEvent.ACTION_MOVE:
view.setOnTouchListener(new OnSwipeTouchListener(this){
@Override
public void onSwipeLeft() {
Toast.makeText(MainActivity.this, "Left", Toast.LENGTH_SHORT).show();
frameAnimation.start();
}
@Override
public void onSwipeRight() {
Toast.makeText(MainActivity.this, "Right", Toast.LENGTH_SHORT).show();
frameAnimation.start();
}
});
break;
}
return true;
}
}
OnSwipe类:
import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
/*
Usage:
myView.setOnTouchListener(new OnSwipeTouchListener(this) {
@Override
public void onSwipeDown() {
Toast.makeText(MainActivity.this, "Down", Toast.LENGTH_SHORT).show();
}
}
*/
public class OnSwipeTouchListener implements View.OnTouchListener {
private GestureDetector gestureDetector;
public OnSwipeTouchListener(Context c) {
gestureDetector = new GestureDetector(c, new GestureListener());
}
public boolean onTouch(final View view, final MotionEvent motionEvent) {
return gestureDetector.onTouchEvent(motionEvent);
}
private final class GestureListener extends GestureDetector.SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
// Determines the fling velocity and then fires the appropriate swipe event accordingly
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeDown();
} else {
onSwipeUp();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
public void onSwipeUp() {
}
public void onSwipeDown() {
}
}