Reputation: 2729
I'm a android beginner programmer.I don't know how to deal with android motion event.Can any body help me with the following code.I want to get the motionEvent starting position , ending position on a OnTouchListener.XML and .java is given.Thanks in advance.
xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background2"
android:gravity="top" >
<TextView
android:id="@+id/touch"
android:background="#00000000"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_above="@+id/tableRow1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="@+id/sidebar" />
< /RelativeLayout>
.java code:
package remote.bluefy.me;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class Touchpad extends Activity{
private Button lclick;
private Button rclick;
private View touch;
private View side;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.touch);
touch=(View)findViewById(R.id.touch);
touch.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
//do something
case MotionEvent.ACTION_MOVE:
//do something
case MotionEvent.ACTION_CANCEL:
//do something
}
return false;
}
});
}
}
Upvotes: 0
Views: 928
Reputation: 2691
Try using this
package remote.bluefy.me;
import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class Touchpad extends Activity{
private Button lclick;
private Button rclick;
private View touch;
private View side;
private float startX, startY;
private float endX, endY;
private boolean isDown = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.touch);
touch=(View)findViewById(R.id.touch);
touch.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
if(isDown == false)
{
startX = event.getX();
startY = event.getY();
isDown = true;
}
case MotionEvent.ACTION_UP:
endX = event.getX();
endY = event.getY();
}
return false;
}
});
}
}
Upvotes: 1