Android ImageView图片拖动效果

直接用了 Android GPS 定位 3 详细地址周边地图图片 中的工程修改的,通过ImageView.setOnTouchListener方法实现及对ImageView的矩阵进行平移变换来实现的。先把获取到的图的尺寸改大些。

String sURL = "http://maps.googleapis.com/maps/api/staticmap?center="
					+ location.getLatitude()
					+ ","
					+ location.getLongitude()
					+ "&zoom=15&size=1500x1500&sensor=false";

之后再设置监听

addressImg.setOnTouchListener(touchListen);

对矩阵进行平移变换

private OnTouchListener touchListen = new OnTouchListener() {

		public boolean onTouch(View v, MotionEvent event) {

			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				startX = event.getX();
				startY = event.getY();
				flag = true;
				break;
			case MotionEvent.ACTION_MOVE:
				if (flag) {
					//通过平移ImageView的矩阵来实现拖动效果
					matrix.set(addressImg.getImageMatrix());

					Log.d(TAG,
							"" + (event.getX() - startX) + ","
									+ (event.getY() - startY));
					matrix.postTranslate((event.getX() - startX) / 15,
							(event.getY() - startY) / 15);
					//除了15纯粹是为了降低图片拖动时的速度
					addressImg.setScaleType(ScaleType.MATRIX);
					addressImg.setImageMatrix(matrix);
				}
				break;
			}
			return true;
		}
	};

你可能感兴趣的:(android)