效果图
代码
/** * 根据手指拖动的当前位置,自动贴边的View */ public class DragView extends ImageView implements View.OnTouchListener{ private int screenWidth; private int screenHeight; private Context mContext; private int lastX, lastY; private int left ,top; private ViewGroup.MarginLayoutParams layoutParams; private int startX; private int endX; private boolean isMoved = false; private onDragViewClickListener mLister; public interface onDragViewClickListener{ void onDragViewClick(); } public void setOnDragViewClickListener(onDragViewClickListener listener){ this.mLister = listener; } public DragView(Context context) { this(context,null); } public DragView(Context context, AttributeSet attrs) { super(context, attrs); mCOntext= context; DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); screenWidth = displayMetrics.widthPixels; screenHeight = displayMetrics.heightPixels-getStatusBarHeight(); init(); } public void init(){ setOnTouchListener(this); post(new Runnable() { @Override public void run() { layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams(); layoutParams.topMargin = screenHeight - getHeight(); layoutParams.leftMargin = screenWidth - getWidth(); setLayoutParams(layoutParams); } }); } @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); startX = lastX; break; case MotionEvent.ACTION_MOVE: isMoved = true; int dx = (int) event.getRawX() - lastX; int dy = (int) event.getRawY() - lastY; left = v.getLeft() + dx; top = v.getTop() + dy; int right = v.getRight() + dx; int bottom = v.getBottom() + dy; // 设置不能出界 if (left <0) { left = 0; right = left + v.getWidth(); } if (right > screenWidth) { right = screenWidth; left = right - v.getWidth(); } if (top <0) { top = 0; bottom = top + v.getHeight(); } if (bottom > screenHeight) { bottom = screenHeight; top = bottom - v.getHeight(); } v.layout(left, top, right, bottom ); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: //只有滑动改变上边距时,抬起才进行设置 if (isMoved) { layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams(); layoutParams.topMargin = top; setLayoutParams(layoutParams); } endX = (int) event.getRawX(); //滑动距离比较小,当作点击事件处理 if (Math.abs(startX - endX) <6) { return false; } if (left +v.getWidth()/20) { result = getResources().getDimensionPixelSize(resourceId); } return result; } }
以上所述是小编给大家介绍的Android实现跟随手指拖动并自动贴边的View样式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!