我正在开发一个应用程序,我正在实现触摸事件和滑动事件,但现在我需要实现一个事件,我向左或向右拖动屏幕.
有什么建议..
解决方法:
让我们分开:
触摸事件:
这是一个简单的例子,说明如何检测简单的触摸事件,获取坐标并显示祝酒词.
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;
public class MainActivity extends Activity {
private boolean isTouch = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int X = (int) event.getX();
int Y = (int) event.getY();
int eventaction = event.getAction();
switch (eventaction) {
case MotionEvent.ACTION_DOWN:
Toast.makeText(this, "ACTION_DOWN AT COORDS "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
isTouch = true;
break;
case MotionEvent.ACTION_MOVE:
Toast.makeText(this, "MOVE "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
break;
case MotionEvent.ACTION_UP:
Toast.makeText(this, "ACTION_UP "+"X: "+X+" Y: "+Y, Toast.LENGTH_SHORT).show();
break;
}
return true;
}
}
如果你想实现水平滑动,你可以使用它
private static final int SWIPE_MIN_disTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELociTY = 200;
int[] values = new int[] {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3 };
@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;
}
/**
* left to right swipe
*/
if (e1.getX() - e2.getX() > SWIPE_MIN_disTANCE
&& Math.abs(veLocityX) > SWIPE_THRESHOLD_VELociTY) {
//next image
setimage(image+1);
/**
* right to left
*/
} else if (e2.getX() - e1.getX() > SWIPE_MIN_disTANCE
&& Math.abs(veLocityX) > SWIPE_THRESHOLD_VELociTY) {
//prev image
setimage(image-1);
}
} catch (Exception e) {
}
return false;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。