Android 自定義可拖拽View界面渲染刷新后不會(huì)自動(dòng)回到起始位置
以自定義ImageView為例:
/** * 可拖拽ImageView * Created by admin on 2017/2/21. */ public class FloatingImageView extends ImageView{ public FloatingImageView(Context context) { super(context); } public FloatingImageView(Context context, AttributeSet attrs) { super(context, attrs); } public FloatingImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public FloatingImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } int startX; int startY; int left; int top; int[] temp = new int[]{ 0, 0 }; @Override public boolean onTouchEvent(MotionEvent event) { boolean isMove = false; int x = (int) event.getRawX(); int y = (int) event.getRawY(); switch (event.getAction()){ case MotionEvent.ACTION_DOWN: // touch down so check if the startX = x; startY = y; temp[0] = (int) event.getX(); temp[1] = y - getTop(); break; case MotionEvent.ACTION_MOVE: // touch drag with the ball left = x - temp[0]; top = y - temp[1]; if(left < 0){//控制左邊界不超出 left = 0; } layout(left, top, left + getWidth(),top + getHeight());//自由拖拽 break; case MotionEvent.ACTION_UP: if (Math.abs(x - startX) > 2 || Math.abs(y - startY) > 2){//判斷是否移動(dòng),再一定范圍內(nèi)不算是移動(dòng),解決觸發(fā)事件沖突 //將最后拖拽的位置定下來,否則頁(yè)面刷新渲染后按鈕會(huì)自動(dòng)回到初始位置 //注意父容器 RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams(); lp.setMargins(left, top,0,0); setLayoutParams(lp); //確定是拖拽 isMove = true; } break; } return isMove ? true : super.onTouchEvent(event); } }
以上所述是小編給大家介紹的Android 自定義可拖拽View界面渲染刷新后不會(huì)自動(dòng)回到起始位置,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- android ListView和GridView拖拽移位實(shí)現(xiàn)代碼
- Android自定義ListView實(shí)現(xiàn)仿QQ可拖拽列表功能
- android RecyclerView側(cè)滑菜單,滑動(dòng)刪除,長(zhǎng)按拖拽,下拉刷新上拉加載
- Android利用RecyclerView實(shí)現(xiàn)全選、置頂和拖拽功能示例
- Android中在GridView網(wǎng)格視圖上實(shí)現(xiàn)item拖拽交換的方法
- Android使用RecycleView實(shí)現(xiàn)拖拽交換item位置
- Android自定義View實(shí)現(xiàn)可以拖拽的GridView
- Android ReboundScrollView仿IOS拖拽回彈效果
- Android使用CardView作為RecyclerView的Item并實(shí)現(xiàn)拖拽和左滑刪除
- Android自定義View實(shí)現(xiàn)拖拽效果
相關(guān)文章
Android PhotoView使用步驟實(shí)例詳解
這篇文章主要介紹了Android PhotoView使用步驟實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06Android清空編輯框內(nèi)容功能的實(shí)現(xiàn)實(shí)例代碼
本篇文章主要介紹了Android清空編輯框數(shù)據(jù)功能的實(shí)現(xiàn)實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03淺析Android App的相對(duì)布局RelativeLayout
這篇文章主要介紹了Android App的相對(duì)布局RelativeLayout,文中舉了一個(gè)登錄界面的XML布局例子,非常直觀,需要的朋友可以參考下2016-04-04Android 通過SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)數(shù)據(jù)存儲(chǔ)管理
SQLiteOpenHelper 是Android 提供的一個(gè)抽象工具類,負(fù)責(zé)管理數(shù)據(jù)庫(kù)的創(chuàng)建、升級(jí)工作。本文主要介紹了如何使用SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)對(duì)數(shù)據(jù)進(jìn)行存儲(chǔ)管理,感興趣的可以了解一下2021-11-11Android開發(fā)ListView中下拉刷新上拉加載及帶列的橫向滾動(dòng)實(shí)現(xiàn)方法
這篇文章主要介紹了Android開發(fā)ListView中下拉刷新上拉加載及帶列的橫向滾動(dòng)實(shí)現(xiàn)方法的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07android使用Rxjava實(shí)現(xiàn)倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了android使用Rxjava實(shí)現(xiàn)倒計(jì)時(shí)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android冷啟動(dòng)實(shí)現(xiàn)app秒開的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android冷啟動(dòng)實(shí)現(xiàn)app秒開的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08