Android 控件 拖动
来源:互联网 发布:linux查看所有硬盘 编辑:程序博客网 时间:2024/05/17 02:43
要实现的效果就是在界面上拖动这一个按钮到处跑步骤如下:
PS: getX()方法 是返回当前View的相对x点坐标,而 getRawX()方法是返回从屏幕原点计算的x点坐标。
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:id="@+id/btn_hello" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>
Touch.java
public class Touch extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button btn = (Button) findViewById(R.id.btn_hello);
btn.setOnTouchListener(new OnTouchListener() {
int[] temp = new int[] { 0, 0 };
public boolean onTouch(View v, MotionEvent event) {
int eventaction = event.getAction();
Log.i("&&&", "onTouchEvent:" + eventaction);
int x = (int) event.getRawX() ;
int y = (int) event.getRawY();
switch (eventaction) {
case MotionEvent.ACTION_DOWN: // touch down so check if the
temp[0] = (int) event.getX() ;
temp[1] = y - v.getTop();
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
v.layout(x - temp[0], y - temp[1], x + v.getWidth() - temp[0], y - temp[1] + v.getHeight());
v.postInvalidate(); //redraw
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}
});
}
}
- Android 控件 拖动
- Android拖动控件
- Android 控件随意拖动
- Android拖动控件
- Android控件拖动
- Android控件拖动
- android 拖动控件例子
- android控件拖动
- android 可拖动控件
- Android控件的拖动
- Android控件拖动效果
- Android 控件的拖动
- Android控件拖动实现
- Android控件拖动的实现
- Android控件拖动的实现
- android可拖动控件初探
- Android控件拖动的实现
- Android控件拖动的实现
- Java词法分析器的设计和实现 设计篇
- VC/MFC之ListCtrl控件使用经验总结
- 男人好累!
- VC ListCtrl应用总结
- 字符串未被识别为有效的布尔值
- Android 控件 拖动
- Java词法分析器 实现篇
- 解读复杂指针声明
- 通过调节坐标进行jfree图的放大缩小
- XNA学习笔记——文档翻译1
- html添加tooltip
- 自己写一个串口调试小助手
- 朋友
- case