您好,登錄后才能下訂單哦!
這篇文章主要介紹怎么使用Android實現圖片浮動隨意拖拽效果,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
本文實例為大家分享了Android實現圖片浮動拖拽效果的具體代碼,供大家參考,具體內容如下
實現步驟
1.先自定義一個浮動工具類
public class MoveImage extends ImageView { /** * * 浮動工具類 * */ private int lastX = 0; private int lastY = 0; private int dx; private int dy; private float movex = 0; private float movey = 0; private int screenWidth; private int screenHeight; public MoveImage(Context context, AttributeSet attrs) { super(context, attrs); screenWidth = ScreenUtils.getWidth(context); screenHeight = ScreenUtils.getHeight(context); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); movex = lastX; movey = lastY; break; case MotionEvent.ACTION_MOVE: dx = (int) event.getRawX() - lastX; dy = (int) event.getRawY() - lastY; int left = getLeft() + dx; int top = getTop() + dy; int right = getRight() + dx; int bottom = getBottom() + dy; if (left < 0) { left = 0; right = left + getWidth(); } if (right > screenWidth) { right = screenWidth; left = right - getWidth(); } if (top < 0) { top = 0; bottom = top + getHeight(); } if (bottom > screenHeight) { bottom = screenHeight; top = bottom - getHeight(); } layout(left, top, right, bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: //避免滑出觸發點擊事件 if ((int) (event.getRawX() - movex) != 0 || (int) (event.getRawY() - movey) != 0) { return true; } break; default: break; } return super.onTouchEvent(event); } }
2.xml布局的引用
<com.zjtd.bzcommunity.text.MoveImage android:id="@+id/iv_phone_bar" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginBottom="70dp" android:src="@drawable/my_qiandao_icon" />
3.activity的實現
private MoveImage iv_phone_bar;//簽到圖片 iv_phone_bar = (MoveImage) view.findViewById(R.id.iv_phone_bar); iv_phone_bar.setOnClickListener(this); // 簽到 case R.id.iv_phone_bar: startActivity(new Intent(getActivity(), SignAcitvity.class));//點擊跳轉頁面 break;
以上是“怎么使用Android實現圖片浮動隨意拖拽效果”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。