如何分别监听多个view的触摸事件? Android 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Button也是View,Button实例能分别对监听做处理,View应该也可以,另外,最好把代码 贴出来 public class CreatView { public static final String Tag = "CreatView"; private Context mContext; protected static CreatView mCreatView; private int[] location; public ImageView mImageView; public ImageView mImageView1; public ImageView mImageView2; public ImageView mImageView3; public WindowManager mWm; public WindowManager.LayoutParams layoutParams; public CreatView(Context context) { this.mContext = context; mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); layoutParams = new WindowManager.LayoutParams(); } public synchronized static CreatView getCreatView(Context context) { Log.d(Tag, "getCreatView"); if (mCreatView == null) { mCreatView = new CreatView(context); } return mCreatView; } public void setLocation(int[] location) { this.location = location; } private View createNewView() { Log.d(Tag, "createNewView"); ImageView mImageView = new ImageView(mContext); mImageView.setImageDrawable(mContext.getResources().getDrawable( R.drawable.apple)); mImageView.setLayoutParams(new LinearLayout.LayoutParams(40, 40)); return mImageView; } public void addViewToScreen() { Log.d(Tag, "addViewToScreen"); mImageView = (ImageView)createNewView(); mImageView1 = (ImageView)createNewView(); mImageView2 = (ImageView)createNewView(); mImageView3 = (ImageView)createNewView(); mImageView.setOnTouchListener(new TouchListener()); mImageView1.setOnTouchListener(new TouchListener()); mImageView2.setOnTouchListener(new TouchListener()); mImageView3.setOnTouchListener(new TouchListener()); layoutParams.gravity = Gravity.LEFT | Gravity.TOP; layoutParams.x = location[0] + 30; //偏移量x layoutParams.y = location[1]; //偏移量y layoutParams.width = 40; layoutParams.height = 40; layoutParams.alpha = 1.0f; mWm.addView(mImageView, layoutParams); mWm.addView(mImageView1, layoutParams); mWm.addView(mImageView2, layoutParams); mWm.addView(mImageView3, layoutParams); } class TouchListener implements OnTouchListener { int lastX; int lastY; int screenWidth; int screenHeight; public TouchListener() { DisplayMetrics dm = mContext.getResources().getDisplayMetrics(); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels; Log.d(Tag, "screen width =" + screenWidth + ",screen height=" + screenHeight); } @Override public boolean onTouch(View v, MotionEvent event) { Log.d(Tag, "TouchListener -- onTouch"); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); Log.d(Tag, "down x=" + lastX + ", y=" + lastY); break; case MotionEvent.ACTION_MOVE: int dx = (int) event.getRawX() - lastX; int dy = (int) event.getRawY() - lastY; Log.d(Tag, "move dx=" + dx + ", dy=" + dy); int left = v.getLeft() + dx; int top = v.getTop() + dy; int right = v.getRight() + dx; int bottom = v.getBottom() + dy; Log.d(Tag, "view left=" + left + ", top=" + top + ", right=" + right + ",bottom=" + bottom); // set bound if (left < 0) { left = 0; right = left + v.getWidth(); } if (right > screenWidth) { right = screenWidth; left = right - v.getWidth(); } if (top < 0) { top = 0; bottom = top + v.getHeight(); } if (bottom > screenHeight) { bottom = screenHeight; top = bottom - v.getHeight(); } v.layout(left, top, right, bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); layoutParams.x = lastX; layoutParams.y = lastY-30; mWm.updateViewLayout(v, layoutParams); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: break; } return true; } }}代码在这里了,目前就是只能移动那个中间的view,是不是要设置监听范围? 如何实现按menu键,弹出顶部+底部菜单(类似于qq阅读器菜单效果) android context 问题 , 第一次发 ,狂散100 android局部绘制.. 关于UI界面问题,同时使用横屏UI和竖屏UI Android Activity继承问题 android 模拟器启动错误信息 自动测试需要:非adb,跨进程模拟点击 flex actionscript 写了一半的工程能接着用android写么 安卓入门 安卓如何获取手指对屏幕施加的压力 android for C++ndk调用接口函数adk停止运行//接口函数的格式是什么? 请问关于TextView的问题
public static final String Tag = "CreatView";
private Context mContext;
protected static CreatView mCreatView;
private int[] location;
public ImageView mImageView;
public ImageView mImageView1;
public ImageView mImageView2;
public ImageView mImageView3;
public WindowManager mWm;
public WindowManager.LayoutParams layoutParams;
public CreatView(Context context) {
this.mContext = context;
mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
layoutParams = new WindowManager.LayoutParams();
}
public synchronized static CreatView getCreatView(Context context) {
Log.d(Tag, "getCreatView");
if (mCreatView == null) {
mCreatView = new CreatView(context);
}
return mCreatView;
}
public void setLocation(int[] location) {
this.location = location;
}
private View createNewView() {
Log.d(Tag, "createNewView");
ImageView mImageView = new ImageView(mContext);
mImageView.setImageDrawable(mContext.getResources().getDrawable(
R.drawable.apple));
mImageView.setLayoutParams(new LinearLayout.LayoutParams(40, 40));
return mImageView;
}
public void addViewToScreen() {
Log.d(Tag, "addViewToScreen");
mImageView = (ImageView)createNewView();
mImageView1 = (ImageView)createNewView();
mImageView2 = (ImageView)createNewView();
mImageView3 = (ImageView)createNewView();
mImageView.setOnTouchListener(new TouchListener());
mImageView1.setOnTouchListener(new TouchListener());
mImageView2.setOnTouchListener(new TouchListener());
mImageView3.setOnTouchListener(new TouchListener());
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
layoutParams.x = location[0] + 30; //偏移量x
layoutParams.y = location[1]; //偏移量y
layoutParams.width = 40;
layoutParams.height = 40;
layoutParams.alpha = 1.0f;
mWm.addView(mImageView, layoutParams);
mWm.addView(mImageView1, layoutParams);
mWm.addView(mImageView2, layoutParams);
mWm.addView(mImageView3, layoutParams);
}
class TouchListener implements OnTouchListener {
int lastX;
int lastY;
int screenWidth;
int screenHeight;
public TouchListener() {
DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
Log.d(Tag, "screen width =" + screenWidth + ",screen height="
+ screenHeight);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d(Tag, "TouchListener -- onTouch");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
Log.d(Tag, "down x=" + lastX + ", y=" + lastY);
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX() - lastX;
int dy = (int) event.getRawY() - lastY;
Log.d(Tag, "move dx=" + dx + ", dy=" + dy);
int left = v.getLeft() + dx;
int top = v.getTop() + dy;
int right = v.getRight() + dx;
int bottom = v.getBottom() + dy;
Log.d(Tag, "view left=" + left + ", top=" + top + ", right="
+ right + ",bottom=" + bottom);
// set bound
if (left < 0) {
left = 0;
right = left + v.getWidth();
}
if (right > screenWidth) {
right = screenWidth;
left = right - v.getWidth();
}
if (top < 0) {
top = 0;
bottom = top + v.getHeight();
}
if (bottom > screenHeight) {
bottom = screenHeight;
top = bottom - v.getHeight();
}
v.layout(left, top, right, bottom);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
layoutParams.x = lastX;
layoutParams.y = lastY-30;
mWm.updateViewLayout(v, layoutParams);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
}
}
代码在这里了,目前就是只能移动那个中间的view,是不是要设置监听范围?