长按图片如何拖动到指定位置(解决)

  • Post author:
  • Post category:其他



长按图片如何拖动到指定位置

点击事件或长按事件调用

private LinearLayout showImage1_layout;
//相框一,得到其长宽用来判断图片移动时是否到你想要显示移动图片的位置  	
private LinearLayout showImage2_layout;
	private ImageView showImg1;
//用来显示移动到里面的图片
	private ImageView showImg2;
	private ImageView moveImage;
//popupwindow里要显示的图片
	private LayoutInflater inflater;
//用来得到pop的view  并设置图片
	private PopupWindow myImagePopupWindow;
	private int popupWidth;
//pop的宽和高    用于更新pop的位置
	private int popupHeight;

private void TouchImage(View v) {
		View popView = inflater.inflate(R.layout.match_test_popup, null);
		moveImage = (ImageView) popView.findViewById(R.id.popup_img);
		// if (myImagePopupWindow == null) {
		myImagePopupWindow = new PopupWindow(popView, 150, 150, true);
		// }
		// else {
		// myImagePopupWindow.update();
		// }
		if (myImagePopupWindow.isShowing()) {
			myImagePopupWindow.dismiss();
		}
		// View popView = inflater.inflate(R.layout.match_test_popup, null);

		moveImage.setBackgroundDrawable(v.getBackground());
	//	moveImage.setImageDrawable(v.getBackground());
		popupWidth = myImagePopupWindow.getWidth();
		popupHeight = myImagePopupWindow.getHeight();

		int[] location = new int[2];
		v.getLocationOnScreen(location);
		// 根据长按住的图片初始popupWindow的位置
		myImagePopupWindow.showAtLocation(
				inflater.inflate(R.layout.match_test_compare, null),
				Gravity.NO_GRAVITY, location[0], location[1]);

		// moveImage.setFocusable(true);
		moveImage.setFocusableInTouchMode(true);
		moveImage.requestFocus();
		// moveImage.requestFocusFromTouch();

		moveImage.setOnTouchListener(movingEventListener);
	}


 

 

 

@SuppressLint("NewApi")
	private OnTouchListener movingEventListener = new OnTouchListener() {
		int lastX, lastY;

		@SuppressLint("NewApi")
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				break;
			case MotionEvent.ACTION_MOVE:
				// 根据移动坐标刷新popupWindow的位置
				int dx = (int) event.getRawX();
				int dy = (int) event.getRawY();
				myImagePopupWindow.update(dx - popupWidth / 2, dy - popupHeight
						/ 2, -1, -1);
				break;
			case MotionEvent.ACTION_UP:
				lastX = (int) event.getRawX();
				lastY = (int) event.getRawY();

				int[] location = new int[2];
				showImage1_layout.getLocationOnScreen(location);
				int[] location1 = new int[2];
				showImage2_layout.getLocationOnScreen(location1);

				// 若移动图片到指定的位置,则showImage显示移动的图片
				if ((lastY - (location[1] + showImage1_layout.getHeight()) < 0)
						&& (lastX
								- (location[0] + showImage1_layout.getWidth()) < 0)) {
					// showImg1.setImageDrawable(moveImage.getDrawable());
					showImg1.setBackgroundDrawable(moveImage.getBackground());
				}
				if ((lastY - (location[1] + showImage2_layout.getHeight()) < 0)
						&& ((location[0] + showImage2_layout.getWidth() - lastX) < 0)) {
					// showImg2.setImageDrawable(moveImage.getDrawable());
					showImg2.setBackgroundDrawable(moveImage.getBackground());

				}
				myImagePopupWindow.dismiss();

				break;
			}
			return true;
		}
	};



版权声明:本文为dongshuai1991原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。