如何获得android touch MotionEvent axix长度?

时间:2012-07-15 13:24:03

标签: android touch ontouchevent

我是一个Android初学者程序员。我不知道如何处理android运动事件。任何身体都可以帮助我使用以下代码。我想得到一个onEouchListener.XML上的motionEvent起始位置,结束位置和.java给出。谢谢。提前 xml代码:

    <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代码:

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;
            }

        });


    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用此

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;
        }

    });


}
}