Android 开发实例:图片拖动的实现(java)
2010-02-03 16:32:00 来源:WEB开发网// 得到图片的大小,位置,在text view 2 上显示
paramsIv_1 = (LayoutParams) iv_1.getLayoutParams();
picHeight = pic.getIntrinsicHeight();
picWidth = pic.getIntrinsicWidth();
picX = paramsIv_1.x;
picY = paramsIv_1.y;
tv_2 = (TextView) this.findViewById(R.id.TextInfo_2);
String str2 = "Anjoy: picWidth = " + picWidth + ", picHeight = "
+ picHeight + ", picX = " + picX + ", picY = " + picY;
tv_2.setText(str2);
tv_2.setLines(2);
// 得到text view 3对象
tv_3 = (TextView) this.findViewById(R.id.TextInfo_3);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
String str1 = "abc", str2 = "abc", str3 = "abc";
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// if语句 判断当鼠标点下时,是否击中图片
if (x >= picX && x <= picX + picWidth && y >= picY
&& y <= picY + picHeight) {
clickPic = true;
} else
clickPic = false;
str1 = "Anjoy: x = " + (int) x + ": y = " + (int) y;
tv_3.setText(str1);
break;
case MotionEvent.ACTION_UP:
// 经测试,Action_Move的终点即为up时所在的坐标,
// 所以当鼠标up时,无需update图片位置
// paramsIv_1 = (LayoutParams) iv_1.getLayoutParams();
// picX = paramsIv_1.x;
// picY = paramsIv_1.y;
// tv_2 = (TextView) this.findViewById(R.id.TextInfo_2);
// str2 = "Anjoy: picWidth = " + picWidth + ", picHeight = "
// + picHeight + ", picX = " + picX + ", picY = " + picY;
// tv_2.setText(str2);
// Log.i("up", str2);
break;
case MotionEvent.ACTION_MOVE:
// 当Action_Down击中图片时,才执行move的动作
赞助商链接