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中常用的三個(gè)Dialog彈窗總結(jié)解析
自己雖然一直使用過dialog,但是一直都是復(fù)制、粘貼;不清楚dialog的具體用途,這次趁著有時(shí)間,總結(jié)一下具體用法,感興趣的朋友跟著小編來看看吧2021-10-10
利用Kotlin的方式如何處理網(wǎng)絡(luò)異常詳解
這篇文章主要 給大家介紹了關(guān)于利用Kotlin的方式如何處理網(wǎng)絡(luò)異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-07-07
Android自定義textview實(shí)現(xiàn)豎直滾動(dòng)跑馬燈效果
這篇文章主要為大家詳細(xì)介紹了Android自定義textview實(shí)現(xiàn)豎直滾動(dòng)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android Handler 機(jī)制實(shí)現(xiàn)原理分析
本文主要介紹 Android Handle機(jī)制實(shí)現(xiàn)的原理,這里整理了詳細(xì)的關(guān)于Handler的資料以及工作流程和實(shí)際應(yīng)用,有興趣的小伙伴可以參考下2016-08-08
Android基礎(chǔ)之Fragment與Activity交互詳解
以下小編就為大家介紹一下Fragment跟Activity之間的關(guān)系。需要的朋友可以過來參考下2013-07-07
Android編程開發(fā)之多點(diǎn)觸摸(Multitouch)實(shí)現(xiàn)方法
這篇文章主要介紹了Android編程開發(fā)之多點(diǎn)觸摸(Multitouch)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式詳細(xì)分析了Android多點(diǎn)觸摸的相關(guān)實(shí)現(xiàn)步驟與操作技巧,需要的朋友可以參考下2016-08-08
Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄
通常手機(jī)通訊錄都會(huì)有索引欄,這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄,現(xiàn)在分享給大家。2016-10-10
Android測(cè)試中Appium的一些錯(cuò)誤解決技巧
今天小編就為大家分享一篇關(guān)于Android測(cè)試中Appium的一些錯(cuò)誤解決技巧的文章,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10
Android 中使用 dlib+opencv 實(shí)現(xiàn)動(dòng)態(tài)人臉檢測(cè)功能
完成 Android 相機(jī)預(yù)覽功能以后,在此基礎(chǔ)上我使用 dlib 與 opencv 庫(kù)做了一個(gè)關(guān)于人臉檢測(cè)的 demo。接下來通過本文給大家介紹Android 中使用 dlib+opencv 實(shí)現(xiàn)動(dòng)態(tài)人臉檢測(cè)功能 ,需要的朋友可以參考下2018-11-11

