当我根据用户在Android中滚动时向上和向下滚动时旋转视图,它在OnScrollChanged
监听器上完美地工作,但是当我向上滚动时,向下滚动总是在执行。
这是代码
iv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
x = (int) event.getRawX();
y = (int) event.getRawY();
///// working only down side
// rotate(120);
Log.d("MainActivity", "before x scroll " + x);
Log.d("MainActivity", "before y scroll " + y);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)
view.getLayoutParams();
int x2= (int) event.getX();
int y2= (int) event.getY();
Log.d("MainActivity", "scroll ACTION_DOWN x scroll " + x2);
Log.d("MainActivity", "scroll ACTION_DOWN y scroll " + y2);
xDelta = x - lParams.leftMargin;
yDelta = y - lParams.topMargin;
Log.d("MainActivity", "ACTION_DOWN x scroll " + xDelta);
Log.d("MainActivity", "ACTION_DOWN y scroll " + yDelta);
rotateDown();
break;
case MotionEvent.ACTION_UP:
Log.d("MainActivity", "ACTION_UP ");
int x3= (int) event.getX();
int y3= (int) event.getY();
Log.d("MainActivity", "scroll ACTION_UP x scroll " + x3);
Log.d("MainActivity", "scroll ACTION_UP y scroll " + y3);
RelativeLayout.LayoutParams uParams = (RelativeLayout.LayoutParams)
view.getLayoutParams();
Toast.makeText(MainActivity.this,
"thanks for new location!", Toast.LENGTH_SHORT)
.show();
rotateUp();
break;
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
.getLayoutParams();
int windowWidth = getWindowManager().getDefaultDisplay().getWidth();
int windowHeightr = getWindowManager().getDefaultDisplay().getHeight();
layoutParams.leftMargin = x - xDelta;
layoutParams.topMargin = y - yDelta;
layoutParams.rightMargin = 0;
layoutParams.bottomMargin = 0;
if (layoutParams.leftMargin < iv.getWidth()) {
// Log.d("MainActivity", "left bound reached");
layoutParams.leftMargin = 10;
}
if (layoutParams.topMargin < iv.getHeight()) {
// Log.d("MainActivity", "top bound reached");
layoutParams.topMargin = 10;
}
boolean collision = checkCollision(iv, bot_iv);
Log.d("MainActivity", "collision :" + collision);
if (collision) {
iv.setVisibility(View.INVISIBLE);
} else {
Log.d("MainActivity", "not collided");
}
Log.d("MainActivity", "ACTION_MOVE x scroll " + layoutParams.leftMargin);
Log.d("MainActivity", "ACTION_MOVE y scroll " + layoutParams.topMargin);
view.setLayoutParams(layoutParams);
break;
}
mainLayout.invalidate();
return true;
}
});