Java Android - 如何在滑动时移动 ImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19876408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - How to move ImageView on Swipe
提问by Avadhani Y
I am new to implement Swipe Gesture for Android application. I am trying to swipe an ImageView from Left to Right of the screen using the below code:
我是为 Android 应用程序实现 Swipe Gesture 的新手。我正在尝试使用以下代码从屏幕的左到右滑动 ImageView:
public class MainActivity extends Activity implements OnClickListener{
private static final int SWIPE_MIN_DISTANCE = 10;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;
View.OnTouchListener gestureListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView btnSwipe = (ImageView)findViewById(R.id.imgBtnSwipe);
btnSwipe.setOnClickListener(this);
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
};
btnSwipe.setOnTouchListener(gestureListener);
}
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
try {
if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
return false;
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(MainActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
Toast.makeText(MainActivity.this, "Right Swipe", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
// nothing
}
return false;
}
}
@Override
public void onClick(View v) {
}
}
I am getting Toast message as Right Swipe. But image is not moving. How can i implement that? Please help me with sample code/links.
我收到 Toast 消息作为向右滑动。但图像不动。我该如何实现?请帮助我提供示例代码/链接。
采纳答案by Avadhani Y
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.
现在,ImageView 正在从右到左和从左到右水平过渡。但这适用于 Android API
11 级以上。
回答by Abhishek V
You need to use PagerAdapter for swiping feature. Something like this :
您需要使用 PagerAdapter 来实现滑动功能。像这样的东西:
public class FullScreenImageAdapter extends PagerAdapter {
//Stores all the image paths
private ArrayList<String> _imagePaths;
.........
@Override
public Object instantiateItem(ViewGroup container, int position) {
//This method is called each time user swipes the screen
//Write suitable code to display next image on the screen
}
......
}
Here is the link for official documentation.
这是官方文档的链接。
http://developer.android.com/training/animation/screen-slide.htmlhttp://developer.android.com/reference/android/support/v4/view/PagerAdapter.html
http://developer.android.com/training/animation/screen-slide.html http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html
Here is the link for an excellent tutorial about Image Slider
这是关于图像滑块的优秀教程的链接