在分析这个Touch事件的时候,用的代码附在这里,希望能给大家帮助。
MainActivity:
package cn.yj3g.TL24_Event_test; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class TL24_Event_testActivity extends Activity { private LinearLayout outLayout; private LinearLayout innerLayout; private ImageView imageView; private TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); outLayout = (LinearLayout) findViewById(R.id.out_layout); innerLayout = (LinearLayout) findViewById(R.id.inner_layout); imageView = (ImageView) findViewById(R.id.i_view); textView = (TextView) findViewById(R.id.t_view); textView.setText(R.string.text2); outLayout.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.v("TAG", "outLayout down"); break; case MotionEvent.ACTION_MOVE: Log.v("TAG", "outLayout move"); break; case MotionEvent.ACTION_UP: Log.v("TAG", "outLayout up"); break; default: break; } return false; } }); innerLayout.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.v("TAG", "innerLayout down"); //return true; break; case MotionEvent.ACTION_MOVE: Log.v("TAG", "innerLayout move"); //return true; break; case MotionEvent.ACTION_UP: Log.v("TAG", "innerLayout up"); break; default: break; } return false; } }); imageView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.v("TAG", "imageView down"); Log.v("TAG", "imageView x="+event.getX()+" rawX="+event.getRawX()); //return true; //消费了 break; case MotionEvent.ACTION_MOVE: Log.v("TAG", "imageView move"); break; case MotionEvent.ACTION_UP: Log.v("TAG", "imageView up"); break; //return true; default: break; } return false; } }); textView.setOnTouchListener(new View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.v("TAG", "textView down"); break; case MotionEvent.ACTION_MOVE: Log.v("TAG", "textView move"); break; case MotionEvent.ACTION_UP: Log.v("TAG", "textView up"); //return true; break; default: break; } return false; } }); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.v("TAG", "activity dispatch down"); return true; //break; case MotionEvent.ACTION_MOVE: Log.v("TAG", "activity dispatch move"); break; case MotionEvent.ACTION_UP: Log.v("TAG", "activity dispatch up"); break; default: break; } return super.dispatchTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: Log.v("TAG", "activity down"); break; case MotionEvent.ACTION_MOVE: Log.v("TAG", "activity move"); break; case MotionEvent.ACTION_UP: Log.v("TAG", "activity up"); break; default: break; } return super.onTouchEvent(event); } }附上代码下载地址(优快云资源,不要积分):
/*我个乖乖,上传上去了,N久不显示。看不到下载链接,下次再补上吧。。。
补上下载链接:http://download.youkuaiyun.com/detail/badboy1110/3635521