Created
June 19, 2016 11:03
-
-
Save maoruibin/b6fb3de5a6fc0b301ae896837b3514ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
int x = (int) event.getX(); | |
int y = (int) event.getY(); | |
switch (event.getAction()){ | |
case MotionEvent.ACTION_DOWN: | |
//记录触摸到坐标 | |
lastX = x; | |
lastY = y; | |
break; | |
case MotionEvent.ACTION_MOVE: | |
//记录偏移量坐标 | |
int offsetX = x - lastX; | |
int offsetY = y - lastY; | |
//在当前的left top right bottom 上加上偏移量 | |
/* 方法一: | |
mImageView.layout(mImageView.getLeft()+offsetX,mImageView.getTop()+offsetY, | |
mImageView.getRight()+offsetX,mImageView.getBottom()+offsetY);*/ | |
Log.i("ACTION_MOVE","----x "+offsetX+"---y "+offsetY); | |
/* | |
方法二: | |
mImageView.offsetLeftAndRight(offsetX); | |
mImageView.offsetTopAndBottom(offsetY); | |
*/ | |
/* | |
方法三: | |
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) mImageView.getLayoutParams(); | |
layoutParams.leftMargin += offsetX; | |
layoutParams.topMargin += offsetY; | |
mImageView.setLayoutParams(layoutParams); | |
*/ | |
/* 方法四: | |
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mImageView.getLayoutParams(); | |
Log.i("params"," left:"+mImageView.getLeft()+" top:"+mImageView.getTop()); | |
layoutParams.leftMargin += offsetX; | |
layoutParams.topMargin += offsetY; | |
mImageView.setLayoutParams(layoutParams);*/ | |
/*方法五 | |
这个需要使用View所在的ViewGroup中来使用scrollBy方法 | |
((View)mImageView.getParent()).scrollBy(-offsetX,-offsetY); | |
*/ | |
//重新设置初始坐标 | |
lastX = x; | |
lastY = y; | |
break; | |
} | |
return super.onTouchEvent(event); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment