View.OnTouchListener 触碰事件

来源:互联网 发布:盘古软件 编辑:程序博客网 时间:2024/05/16 07:17
开发Anroid软件的过程中,一开始会遇到成吨出现的问题。比如说,我照猫画虎的实现了View.OnClickLinstener事件,可以点击一个Button按钮,浏览事前定义好的图片了。但我突发奇想,可不可以自定义一个手势,别人不知道的手势,念咒语一样,在自己的手机上面一划,出现一些我想要的效果呢。。。成吨的问题就出现了。。。。。。这篇文章不打算攻克Gesture,先摸索一下View.OnTouchListener。实现一个例子就好。帮助文档是这样描述的:Public Methodspublic abstract boolean onTouch (View v, MotionEvent event)Called when a touch event is dispatched to a view. This allows listeners to get a chance to respond before the target view.Parametersv   The view the touch event has been dispatched to.***event    The MotionEvent object containing full information about the event.***Returns    True if the listener has consumed the event, false otherwise. 这个方法有两个参数,第一个参数View v很好理解,就是需要绑定的事件源(比如说点击一下一个按钮,这个按钮就是这个点击事件的事件源头)。令我困惑的是参数MotionEvent event,这是个什么东西?英文还是能看懂的,说是这个MotionEvent 对象包含event所有的信息。新手开发Android,一开始会遇到成吨的问题。。。那么问题就来了,这个event实例会包含哪些信息呢?这些信息一般会起到什么作用呢? 于是又点进去看看,到底这个MotionEvent 是个什么鬼类!!帮助文档是这么说的:Object used to report movement (mouse, pen, finger, trackball) events. Motion events may hold either absolute or relative movements and other data, depending on the type of device. 也就是说,MotionEvent这个类的对象,可能会持有绝对或者相对运动以及其他数据....这个要取决于你设备的类型.....Motion events describe movements in terms of an action code and a set of axis values.对象会给出轴值的集合...帮助文档下面就是一堆鬼。。。打死不看!!!反正现在知道了,MotionEvent这个类的对象,会包含这个event的所有信息,至少会包含一些轴值,比如说X,Y轴值。蛋了个疼,那么就写个例子来看看,当TouchEvent事件发生时,能不能得到轴值。

布局文件代码:很简单,一个ImageView,一个TextView

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:gravity="center"    android:id="@+id/root"    >    <ImageView        android:id="@+id/picture"        android:layout_width="wrap_content"        android:layout_height="360dp"        android:src="@drawable/meinv"/>    <TextView        android:id="@+id/info"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="坐标信息"/></LinearLayout>

主程序代码:

public class MainActivity extends AppCompatActivity {    //声明TextView、ImageView对象    private TextView info = null;    private ImageView pictrue = null;    //定义一个图片资源    int luonv = R.drawable.meinv2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //获取TextView、ImageView对象        info = (TextView) findViewById(R.id.info);        pictrue = (ImageView) findViewById(R.id.picture);        //注册onTouch监听器        pictrue.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View view, MotionEvent motionEvent) {                // event.getX获取X坐标; event.getY获取Y坐标                String perinfo = "X=" + String.valueOf(motionEvent.getX() +                                " Y=" + String.valueOf(motionEvent.getY()));                info.setText(perinfo);                pictrue.setImageResource(luonv);                return true;            }        });    }}
程序中用到的图片资源,自己随便照两张,丢到res/drawable目录下面去就可以了。将程序安装到手机上,点击图片时,有两个效果,一是图片切换了一张,这是由于代码picture.setImageResource(luonv);二是文本框给出了touch事件发生处的X Y 轴值。小结一下,View.OnTouchListener是个蛋疼的interface既接口,使用它的时候,必须要重写它的方法,重写publi abstract boolean onTouch(View v, MotionEvent event)方法中,就要知道event包含了什么东西。尤其是更View.OnClickListener对比,查同样是接口类View.OnClickListener的方法便知,它的方法是这样定义的:public abstract void onClick(View v),注意到只有一个参数。这里就没有event了!所以若想知道事件的一些信息,根据这些信息来选择要执行响应的代码,应该选用View.OnTouchListener.妈的,成吨的问题。。。有个老司机带带路就好了。。。
0 0
原创粉丝点击