Android 可拖拽ImageView
作者:互联网
DragButton 可移动的VIew
import android.content.Context; import android.support.v7.widget.AppCompatTextView; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.ViewGroup; /** * 可拖拽的view */ public class DragButton extends AppCompatTextView { private int myWidth; private int myHeight; //父控件的宽高 private int parentWidth; private int parentHeight; //是否拖动 private boolean isDrag = false; /** * 父控件 */ private ViewGroup parentView; public boolean isDrag() { return isDrag; } public DragButton(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); myWidth = getMeasuredWidth(); myHeight = getMeasuredHeight(); if (parentHeight == 0 || parentWidth == 0) { parentView = (ViewGroup) getParent(); parentView.post(new Runnable() { @Override public void run() { parentWidth = parentView.getMeasuredWidth(); parentHeight = parentView.getMeasuredHeight(); } }); } } private float downX; private float downY; @Override public boolean onTouchEvent(MotionEvent event) { if (this.isEnabled()) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: isDrag = false; downX = event.getX(); downY = event.getY(); break; case MotionEvent.ACTION_MOVE: final float xDistance = event.getX() - downX; final float yDistance = event.getY() - downY; int l, r, t, b; //当水平或者垂直滑动距离大于10,才算拖动事件 if (Math.abs(xDistance) > 10 || Math.abs(yDistance) > 10) { isDrag = true; //在父控件中左边的位置 = 当前在父控件中的位置 - 内边距 + 拖动的距离 l = (int) (getLeft() - parentView.getPaddingLeft() + xDistance); //在父控件中的右边位置 = 在左边的位置 + 自己的宽 r = l + myWidth; t = (int) (getTop() - parentView.getPaddingTop() + yDistance); b = t + myHeight; if (l < 0) { //如果左边的距离超出父控件的左边有效范围,那么这个值会是负数, // 为了保证在父控件中的拖动,所以设为父控件的左边的起点(包括内边距) l = parentView.getPaddingLeft(); r = l + myWidth; } else if (r > (parentWidth - parentView.getPaddingRight())) { r = parentWidth - parentView.getPaddingRight(); l = r - myWidth; } if (t < 0) { t = parentView.getPaddingTop(); b = t + myHeight; } else if (b > parentHeight - parentView.getPaddingBottom()) { b = parentHeight - parentView.getPaddingBottom(); t = b - myHeight; } this.layout(l, t, r, b); } break; case MotionEvent.ACTION_UP: if (isDrag) { //如果是拖动,就拦截点击事件 return false; } break; } return super.onTouchEvent(event); } return false; } }
DragButtonUtil 管理可拖拽View工具类
import android.app.Activity; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.FrameLayout; import com.deppon.pdam.express.accept.R; /** * 管理DragButton工具类 */ public class DragButtonUtil { private static DragButton dragButton; public static DragButton getDragButton(){ return dragButton; } public static void createDragButton(Activity activity,int gravity){ FrameLayout frameLayout = new FrameLayout(activity); dragButton = new DragButton(activity,null); FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((int)activity.getResources().getDimension(R.dimen.x269),(int)activity.getResources().getDimension(R.dimen.y299)); //Gravity.BOTTOM | Gravity.RIGHT lp.gravity = gravity; lp.bottomMargin=139; dragButton.setLayoutParams(lp); frameLayout.addView(dragButton); ViewGroup.LayoutParams params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT); activity.getWindow().addContentView(frameLayout,params); } /** * 创建属于该activity的DragButton * @param activity * @return */ public static void createDragButton(Activity activity, int x, int y,int gravity){ FrameLayout frameLayout = new FrameLayout(activity); dragButton = new DragButton(activity,null); FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(x,y); //Gravity.BOTTOM | Gravity.RIGHT lp.gravity = gravity; lp.bottomMargin=139; dragButton.setLayoutParams(lp); frameLayout.addView(dragButton); ViewGroup.LayoutParams params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT); activity.getWindow().addContentView(frameLayout,params); } public static void hidedragButton(){ dragButton.setVisibility(View.GONE); } public static void showDragButton(){ dragButton.setVisibility(View.VISIBLE); } public static void destoryDragButton(){ dragButton=null; } }
使用案例
//传入显示的Activity 默认右下角 DragButtonUtil.createDragButton(getActivity(), Gravity.BOTTOM | Gravity.RIGHT); //获取控件 DragButton scanV= DragButtonUtil.getDragButton(); //设置控件图片 scanV.setBackgroundResource(R.mipmap.fab_saomiao); scanV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("LOG",“点击了”); } });
标签:控件,int,public,parentView,LayoutParams,activity,ImageView,Android,拖拽 来源: https://www.cnblogs.com/lingsuiwang/p/14822161.html