package org.anjoy.activity;
import *.*;
// 图片拖动示例
public class MoveImageViewSample extends Activity {
private int screenWidth, screenHeight;
// ImageView 图片显示区域
private ImageView iv_1;
// TextView 显示屏幕,图片位置等信息
private TextView tv_1, tv_2, tv_3;
// 图片的位置参数对象,
// 通过Drawable对象的getLayoutParams()方法取得,可得到图片的位置信息
// 通过设置LayoutParams方法的x,y坐标,并传给图片,可以改变图片的位置,如:
// setLayoutParams(paramsIv_1)
private AbsoluteLayout.LayoutParams paramsIv_1;
// 记录图片的长宽,与左上角一点的坐标
private int picWidth, picHeight, picX, picY;
// 在onTouchEvent方法中设置,标记是否击中图片
private boolean clickPic;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 全屏显示
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 加载layout
setContentView(R.layout.moveimageviewsample);
// 得到屏幕分辨率,并在text view 1 上显示
tv_1 = (TextView) this.findViewById(R.id.TextInfo_1);
screenWidth = AnjoyUtil.getScreenWidth(this);
screenHeight = AnjoyUtil.getScreenHight(this);
tv_1.setText("Anjoy Screen information" + screenHeight + ":"
+ screenWidth);
// 加载图片,在image view 1上显示
Drawable pic = getResources().getDrawable(R.drawable.icon);
iv_1 = (ImageView) this.findViewById(R.id.ImageView01);
iv_1.setImageDrawable(pic);
// iv_1.setBackgroundColor(Color.LTGRAY);
iv_1.setScaleType(ScaleType.FIT_CENTER);
// 得到图片的大小,位置,在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的动作
// 否则跳过
if (clickPic) {
// picWidth与picHeight分别除以2,定位击中点为图片的中心
picX = paramsIv_1.x = (int) (x - picWidth / 2);
// 限制图片在屏幕坚向的移动范围
if (y >= 350)
y = 350;
picY = paramsIv_1.y = (int) (y - picHeight / 2);
iv_1.setLayoutParams(paramsIv_1);
str3 = "Anjoy: picWidth = " + picWidth + ", picHeight = "
+ picHeight + ", picX = " + picX + ", picY = " + picY;
Log.i("move", str3);
tv_3.setText(str3);
}
break;
}
return super.onTouchEvent(event);
}
}