安卓判断滑动方向onTouchEvent
来源:互联网 发布:淘宝小蜜是干嘛的 编辑:程序博客网 时间:2024/05/30 22:49
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
textView = (TextView) findViewById(R.id.tv1);
}
float x_temp01 = 0.0f;
float y_temp01 = 0.0f;
float x_temp02 = 0.0f;
float y_temp02 = 0.0f;
private TextView textView;
@Override
public boolean onTouchEvent(MotionEvent event) {
// 获得当前坐标
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x_temp01 = event.getX();
y_temp01 = event.getY();
break;
case MotionEvent.ACTION_UP:
x_temp02 = event.getX();
y_temp02 = event.getY();
direction();
break;
}
return super.onTouchEvent(event);
}
private void direction() {
float h = x_temp02 - x_temp01;
float l = y_temp02 - y_temp01;
if (Math.abs(h) > Math.abs(l)) {
if (h > 0)// 向左
{
// 移动了x_temp01-x_temp02
textView.setText("向左滑动");
textView.setBackgroundColor(Color.BLUE);
} else {
// 移动了x_temp02-x_temp01
textView.setText("向右滑动");
textView.setBackgroundColor(Color.GREEN);
}
} else {
if (l > 0)// 向下
{
textView.setText("向下滑动");
textView.setBackgroundColor(Color.RED);
} else {
textView.setText("向上滑动");
textView.setBackgroundColor(Color.YELLOW);
}
}
// 比较x_temp01和x_temp02
// x_temp01>x_temp02 //向左
// x_temp01==x_temp02 //竖直方向或者没动
// x_temp01<x_temp02 //向右
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
}
private void initview() {
textView = (TextView) findViewById(R.id.tv1);
}
float x_temp01 = 0.0f;
float y_temp01 = 0.0f;
float x_temp02 = 0.0f;
float y_temp02 = 0.0f;
private TextView textView;
@Override
public boolean onTouchEvent(MotionEvent event) {
// 获得当前坐标
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x_temp01 = event.getX();
y_temp01 = event.getY();
break;
case MotionEvent.ACTION_UP:
x_temp02 = event.getX();
y_temp02 = event.getY();
direction();
break;
}
return super.onTouchEvent(event);
}
private void direction() {
float h = x_temp02 - x_temp01;
float l = y_temp02 - y_temp01;
if (Math.abs(h) > Math.abs(l)) {
if (h > 0)// 向左
{
// 移动了x_temp01-x_temp02
textView.setText("向左滑动");
textView.setBackgroundColor(Color.BLUE);
} else {
// 移动了x_temp02-x_temp01
textView.setText("向右滑动");
textView.setBackgroundColor(Color.GREEN);
}
} else {
if (l > 0)// 向下
{
textView.setText("向下滑动");
textView.setBackgroundColor(Color.RED);
} else {
textView.setText("向上滑动");
textView.setBackgroundColor(Color.YELLOW);
}
}
// 比较x_temp01和x_temp02
// x_temp01>x_temp02 //向左
// x_temp01==x_temp02 //竖直方向或者没动
// x_temp01<x_temp02 //向右
}
}
1 0
- 安卓判断滑动方向onTouchEvent
- 通过OnTouchEvent(Motionevent event)判断手指滑动方向
- 判断滑动方向UITableView
- 判断scrollviewd滑动方向
- touch滑动判断方向
- android GestureDetector依赖onTouchEvent判断左右滑动
- uitableview scrollview 判断滑动方向
- uitableview scrollview 判断滑动方向
- 如何判断UIScrollView滑动方向
- 判断scrollView的滑动方向
- 判断ScrollView滑动的方向
- android判断viewpager滑动方向
- 判断UISrollview的滑动方向
- 动态判断UIScrollview滑动方向
- UIScrollView的滑动方向判断
- uitableview scrollview 判断滑动方向
- GestureDetector和onTouchEvent判断手势方向(上下左右)
- Android 判断手势的滑动方向
- Android4.4以上改变状态栏颜色的几种实现方式
- BZOJ1433 假期的宿舍(最大流)
- andorid资源动画学习
- 【matlab】:图像获得其边缘
- linux学习目录(更新中)
- 安卓判断滑动方向onTouchEvent
- 多元线性回归例子
- POJ-1273 Drainage Ditches(最大流)
- private、protected、default、public权限
- eclipse通过maven插件添加Jersey archetype
- php环境的搭建
- 操作系统总览
- Mybatis入门及配置详解
- 一次内存错误调试总结(平台Cortex-M3)