我应该在哪里实现了Android onTouchListener? [英] Where should I implement the Android onTouchListener?

查看:151
本文介绍了我应该在哪里实现了Android onTouchListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新Android开发,并在那一刻,我必须做出的,其中控制手势输入一个决定,但我只是不知道在哪里抢触摸输入的利弊。他们给了我两个选择,可以请你向我解释的两种方式的利弊?

在查看设置监听;实现方法:

  faceView.setOnTouchListener(新OnTouchListener(){
   公共布尔onTouch(查看arg0中,MotionEvent ARG1){
     返回false;
   }
);

中查看覆盖方法

 公共布尔onTouchEvent(MotionEvent事件){
   返回false;
}


解决方案

方法1:匿名内部类

  faceView.setOnTouchListener(新OnTouchListener(){
   公共布尔onTouch(查看arg0中,MotionEvent ARG1){
     返回false;
   }
);

这是匿名的,因为它是一个没有名字的声明。没有 OnTouchListener的myTouch = ,只有声明新OnTouchListener()。这是内在的,因为它是在另一个类中,而且它是一个类型,因为它是一个接口的实现。

好了,考虑到这一点。这种方法对于开发人员更方便。功能是孤立的(一般由listenerable的声明),这是易于管理。但是这种便利是有代价的。假设你有按钮,十个onClickListeners。用这种方法,垃圾收集队列将更快填满每一个新的匿名内部类型

但有什么关系?不是真的。如果你想获得的性能每微秒你的系统,那么一定不要使用这种方法。但是总体来说,这是可行的。

方法2:单继承

 公共布尔onTouchEvent(MotionEvent事件){
   返回false;
}

假设你看了上面的,这种方法有创造和清理成本要低得多。只有1实例化额外的类,只有1对象添加到队列GC

这是我使用的方法,并且已经使用了一段时间。这也是我在谷歌的示例源$ C ​​$ C看到的。

但它并不是完美的! onTouchEvent 的实施将最终寻找这样:

 公共无效onTouch(事件e){
如果(e.equals(视图1)){
}否则如果(e.equals(视图2)){
}否则如果(e.equals(查看3)){
...}

SO

老实说,这并不重要。如果你想尽可能高的性能,使用一个静态onTouchEvent监听器,你将不必支付上述费用,但在大多数情况下,他们不贵反正。

I am new to Android development and at the moment I must make a decision of where to control the gesture input, but I just don't know the pros and cons of where to grab the touch input. I have been given two alternatives, can you please explain to me the pros and cons of either way?

Set listener on View; implement methods:

faceView.setOnTouchListener(new OnTouchListener(){
   public boolean onTouch(View arg0, MotionEvent arg1){
     return false;
   }
);

or

Override method within View

public boolean onTouchEvent(MotionEvent event){
   return false;
}

解决方案

APPROACH 1: Anonymous Inner Type.

faceView.setOnTouchListener(new OnTouchListener(){
   public boolean onTouch(View arg0, MotionEvent arg1){
     return false;
   } 
);

This is Anonymous, because it is a declaration without a name. There is no OnTouchListener mytouch =, there is only the declaration new OnTouchListener(). This is inner, because it is inside another class, and it is a type because it is an implementation of an interface.

OK, so with that in mind. This approach is more convenient for developers. Functionality is isolated (generally by the declaration of the listenerable) and this is easily managed. But this convenience comes with a cost. Assume you have buttons, with ten onClickListeners. With this approach, the garbage collection queue will fill up faster with each new anonymous inner type.

But does it matter? Not really. If you are trying to get every microsecond of performance out of your system, then sure don't use this approach. But generally, this is viable.

APPROACH 2: Single Inheritance

public boolean onTouchEvent(MotionEvent event){
   return false;
} 

Assuming you read the above, this approach has a much lower cost for creation and cleanup. There is only 1 extra class instantiated, and only 1 object added to the GC queue.

This is the approach I use, and have used for a while. It's also what I see in Google's sample source code.

But it's not perfect! The implementation of onTouchEvent will end up looking as such:

public void onTouch(Event e){
if (e.equals(View1)){
}else if (e.equals(View2)){
}else if (e.equals(View 3)){
...}

SO

Honestly, it doesn't matter. If you want the highest performance possible, use a static onTouchEvent listener, and you won't have to pay the above mentioned costs, but for the most part they are not that expensive anyways.

这篇关于我应该在哪里实现了Android onTouchListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆