如果我点击按钮然后它移动1,2个像素(执行ontouch())但没有点击(onclick())...但如果我准确点击然后它点击但是它太难以执行onclick() finctionality ....有没有办法执行onclick功能轻松... ontouch()功能正常工作。
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
overlayedButton = new Button(this);
overlayedButton.setWidth(50);
overlayedButton.setHeight(50);
overlayedButton.setText("butn");
overlayedButton.setOnClickListener(this);
overlayedButton.setOnTouchListener(this);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(70, 70, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH, PixelFormat.TRANSLUCENT);
params.gravity = Gravity.LEFT | Gravity.TOP ;
params.x = 0;
params.y = 0;
wm.addView(overlayedButton, params);
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
overlayedButton.setBackgroundResource(R.drawable.appicon_icon_hovered);
float x = event.getRawX();
float y = event.getRawY();
moving = false;
int[] location = new int[2];
overlayedButton.getLocationOnScreen(location);
originalXPos = location[0];
originalYPos = location[1];
offsetX = originalXPos - x;
offsetY = originalYPos - y;
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
int[] topLeftLocationOnScreen = new int[2];
topLeftView.getLocationOnScreen(topLeftLocationOnScreen);
System.out.println("topLeftY="+topLeftLocationOnScreen[1]);
System.out.println("originalY="+originalYPos);
float x = event.getRawX();
float y = event.getRawY();
golparams = (LayoutParams) overlayedButton.getLayoutParams();
int newX = (int) (offsetX + x);
int newY = (int) (offsetY + y);
if (Math.abs(newX - originalXPos) < 1 && Math.abs(newY - originalYPos) < 1 && !moving) {
return false;
}
golparams.x = newX - (topLeftLocationOnScreen[0]);
golparams.y = newY - (topLeftLocationOnScreen[1]);
wm.updateViewLayout(overlayedButton, golparams);
moving = true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
if (moving) {
del=new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try{
if(!Thread.currentThread().isInterrupted())
{
del.sleep(2000);
}
// if(Thread.currentThread().isInterrupted())
// {
// del.sleep(2000);
// }
}catch(InterruptedException e){}
handler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
overlayedButton.setBackgroundResource(R.drawable.appicon_icon);
}
});
}
});
del.start();
return true;
}
if(moving&& originalXPos-event.getRawX()<5 ||originalYPos-event.getRawY()<5)
{
onClick(overlayedButton);
}
}
return false;
}
@Override
public void onClick(View v) {
WindowManager wm1 = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
if(v==overlayedButton)
{
// do something
}