Mar
9
废话少说,要实现的效果就是在界面上拖动这一个按钮到处跑。
1. 布局文件
<?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>
2. 代码
package com.roiding.sample;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
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();
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}
});
}
}
Tags: android, drag, MotionEvent, OnTouchListener, 拖动
June 25th, 2009 at 4:26
好,呀,这个网站是个人的吗,怎么不能评论呢,像是个做技术的人
November 2nd, 2009 at 1:28
小弟也新建了一个博客,希望大家以后多多交流!
January 21st, 2010 at 10:30
很厉害。。。。分享了
January 21st, 2010 at 10:30
第二个不是很懂唉。。
February 7th, 2010 at 12:27
thanks~~~很好用~
February 14th, 2010 at 8:45
so cool
thx
转摘ing…
March 1st, 2010 at 4:26
iXhCocQw [url=http://utkxmq.com/]HHCcgA[/url]
March 1st, 2010 at 4:29
sIhPHYt [url=http://ydxuxn.com/]BhPKEDa[/url]
March 1st, 2010 at 7:13
sfKpuV http://qolbne.com/
March 1st, 2010 at 7:17
PRDXasVj http://twoeen.com/
March 3rd, 2010 at 11:06
请问我想实现一个imageView移动 该怎么做呢
March 4th, 2010 at 4:53
iAtbhopQ http://samuelrutledge.com/xanax.html#6231 Xanax
March 5th, 2010 at 5:38
SJAtGzYr http://aidamexico.com/ambien.html#3596 Ambien
March 6th, 2010 at 6:04
bbaodbLc http://unioncountyvoice.com/#4585 Valium
March 8th, 2010 at 3:26
kdtGio http://taksimoldcity.com/xanax.html#1481 Cheap Xanax
March 9th, 2010 at 5:03
Hello! gddddcd interesting gddddcd site!
March 9th, 2010 at 5:03
Very nice site! cheap viagra
March 9th, 2010 at 5:03
Very nice site! [url=http://aieypxo.com/toxvss/2.html]cheap cialis[/url]
March 9th, 2010 at 5:03
Very nice site! cheap cialis http://aieypxo.com/toxvss/4.html
March 9th, 2010 at 5:03
Very nice site!
March 9th, 2010 at 5:08
phHFKm http://ranario.com/xanax.html#5460 Xanax
March 10th, 2010 at 7:53
gXChuDX http://rok-kosmac.com/valium.html#9999 Valium