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

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

点击事件或长按事件调用

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;
		}
	};


发布了20 篇原创文章 · 获赞 9 · 访问量 3万+
展开阅读全文

ViewDragHelper做拖动,View会跳回到原位置,why?

10-29

使用ViewDragHelper做拖动,当动态addView的时候,原本已经拖动到另一位置的View会回到原位置,why? 我有一个简单的VHLayout,继承自FrameLayout,里面通过setDragableView方法可以指定哪个子View可以拖动 ``` import android.content.Context; import android.support.v4.view.MotionEventCompat; import android.support.v4.widget.ViewDragHelper; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.FrameLayout; public class VHLayout extends FrameLayout { private View mView; private ViewDragHelper mDragHelper; public VHLayout(Context context, AttributeSet attrs) { super(context, attrs); mDragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() { // 返回ture则表示可以捕获该view;可以根据传入的view参数决定哪些可以拖动 @Override public boolean tryCaptureView(View child, int pointerId) { return child == mView; } // 控制水平拖动的边界 @Override public int clampViewPositionHorizontal(View child, int left, int dx) { final int leftBound = getPaddingLeft(); final int rightBound = getWidth() - child.getWidth() - leftBound; final int newLeft = Math.min(Math.max(left, leftBound), rightBound); return newLeft; } // 控制垂直拖动的边界 @Override public int clampViewPositionVertical(View child, int top, int dy) { final int topBound = getPaddingTop(); final int bottomBound = getHeight() - child.getHeight() - topBound; final int newTop = Math.min(Math.max(top, topBound), bottomBound); return newTop; } @Override public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); } // 解决ViewDragHelper中添加button,在控制button拖动的同时又不影响button的点击事件! @Override public int getViewHorizontalDragRange(View child) { return mView == child ? child.getWidth() : 0; } @Override public int getViewVerticalDragRange(View child) { return mView == child ? child.getHeight() : 0; } @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { super.onViewPositionChanged(changedView, left, top, dx, dy); changedView.layout(left, top, left + changedView.getMeasuredWidth(), top + changedView.getMeasuredHeight()); } }); } @Override protected void onFinishInflate() { super.onFinishInflate(); } @Override public boolean onInterceptTouchEvent(MotionEvent event) { final int action = MotionEventCompat.getActionMasked(event); if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { mDragHelper.cancel(); return false; } return mDragHelper.shouldInterceptTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { try { mDragHelper.processTouchEvent(event); } catch (Exception e) { // TODO: handle exception } return true; } public void setDragableView(View view) { mView = view; } } ``` 拖动一切没问题 但是当我实际使用的时候却有问题了,如下,我在布局里的一个叫container的LinearLayout动态添加一个View(addView),就出现所说的现象。事实上,只要弹出输入法,也会出现这种回到原位置的问题。怎么解决的? 使用方式: ``` public void addFloatView(int bottomMargin){ FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT); params.gravity=Gravity.RIGHT|Gravity.BOTTOM;//让加入的View出现在右下角 params.bottomMargin=bottomMargin; ImageView iv=new ImageView(context); iv.setImageResource(R.drawable.ic_scan); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub onFloatViewClick(); } }); VHLayout rootView=(VHLayout)findViewById(R.id.rootView); rootView.setDragableView(iv); rootView.addView(iv,params); } ``` 布局: ``` <com.mb.goods.widget.VHLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/rootView" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <include layout="@layout/actionbar_comm" /> </LinearLayout> </com.mb.goods.widget.VHLayout> ``` 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览