亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Android 自定義LineLayout實現滿屏任意拖動功能的示例代碼

瀏覽:56日期:2022-09-23 17:40:47

1.前言

在開發中,會有需求實現控件在屏幕隨意拖動,這就需要自定義View,然后在OnTouchEvent事件中,處理MotionEvent.ACTION_MOVE事件,然后通過坐標點傳值給onlayout方法,來實現控件的任意拖動,具體代碼如下:

import android.content.Context;import android.util.AttributeSet;import android.view.Display;import android.view.MotionEvent;import android.view.WindowManager;import android.widget.LinearLayout;public class DragLineLayout extends LinearLayout { private int mWidth; private int mHeight; private int mScreenWidth; private int mScreenHeight; private Context mContext; private onLocationListener mLocationListener;/*listen to the Rect */ //是否拖動 private boolean isDrag = false; public boolean isDrag() { return isDrag; } public DragView(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = getMeasuredWidth(); mHeight = getMeasuredHeight(); mScreenWidth = getScreenWidth(mContext); mScreenHeight = getScreenHeight(mContext) - getStatusBarHeight(); } public int getStatusBarHeight() { int resourceId = mContext.getResources().getIdentifier('status_bar_height', 'dimen', 'android'); return mContext.getResources().getDimensionPixelSize(resourceId); } public int getScreenWidth(Context context) { WindowManager manager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); return display.getWidth(); } public int getScreenHeight(Context context) { WindowManager manager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); return display.getHeight(); } private float mDownX; private float mDownY; @Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); if (this.isEnabled()) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: isDrag = false; mDownX = event.getX(); mDownY = event.getY(); break; case MotionEvent.ACTION_MOVE: final float mXdistance = event.getX() - mDownX; final float mYdistance = event.getY() - mDownY; int l, r, t, b; //當水平或者垂直滑動距離大于10,才算是拖動事件 if (Math.abs(mXdistance) > 10 || Math.abs(mYdistance) > 10) { isDrag = true; l = (int) (getLeft() + mXdistance); r = l + mWidth; t = (int) (getTop() + mYdistance); b = t + mHeight; //邊界判斷,不讓布局滑出界面 if (l < 0) { l = 0; r = l + mWidth; } else if (r > mScreenWidth) { r = mScreenWidth; l = r - mWidth; } if (t < 0) { t = 0; b = t + mHeight; } else if (b > mScreenHeight) { b = mScreenHeight; t = b - mHeight; } //回調移動后的坐標點 if(mLocationListener!=null){ mLocationListener.locationRect((l+r)/2,(t+b)/2); } this.layout(l, t, r, b); } break; case MotionEvent.ACTION_UP: setPressed(false); break; case MotionEvent.ACTION_CANCEL: setPressed(false); break; } return true; } return false; } public void setLocationListener(onLocationListener LocationListener) { this.mLocationListener = LocationListener; } public interface onLocationListener { void locationRect(float locationX, float locationY); }}

2.在代碼中的運用

<com.xinrui.guestservice.view.DragLineLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='@dimen/dp_200' android:layout_height='@dimen/dp_110' android:orientation='vertical'> <RelativeLayout android:layout_width='match_parent' android:layout_height='@dimen/dp_50'> <EditText android: android:layout_width='match_parent' android:layout_height='@dimen/dp_50' android:background='@drawable/edit_bg' /> </RelativeLayout> <RelativeLayout android:layout_width='match_parent' android:layout_height='@dimen/dp_55' android:layout_marginTop='@dimen/margin_5' android:background='@drawable/paint_bg'> <TextView android: android:layout_width='@dimen/dp_50' android:layout_height='@dimen/dp_50' android:layout_alignParentLeft='true' android:layout_alignParentTop='true' android:layout_marginTop='@dimen/margin_5' android:background='@drawable/main_selector_write' android:clickable='true' /> <TextView android: android:layout_width='@dimen/dp_50' android:layout_height='@dimen/dp_50' android:layout_alignParentTop='true' android:layout_marginLeft='@dimen/dp_10' android:layout_marginTop='@dimen/margin_5' android:layout_toRightOf='@id/paint_typeface' android:background='@drawable/main_selector_write' android:clickable='true' /> </RelativeLayout></com.xinrui.guestservice.view.DragLineLayout>

3.這樣就可以在Activity 加載這個xml 來實現任意拖動功能

總結

到此這篇關于Android 自定義LineLayout實現滿屏任意拖動功能的示例代碼的文章就介紹到這了,更多相關Android 自定義LineLayout實現滿屏任意拖動內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
主站蜘蛛池模板: 国产大长吊 | 国产一级特黄老妇女大片免费 | 毛片免费观看 | 色屁屁www影院免费观看入口 | 尤物国产视频 | 国产免费做爰午夜视频 | 午夜免费福利视频 | 一次性a爱片 | 黄色片视频在线免费观看 | 亚洲黄色免费网站 | 亚洲精品入口一区二区在线播放 | 男女午夜特黄毛片免费 | 亚洲视频手机在线 | 黄色网址免费在线观看 | 国产又色又爽黄的网站免费 | 免费爱爱视频网站 | 6080伦理久久亚洲精品 | 成人福利在线视频 | 91av在线国产| 免费特黄视频 | 国产欧美日韩在线观看一区二区三区 | xvideos最新亚洲入口 | 婷婷色九月综合激情丁香 | 宅女深夜福利视频在线 | 亚洲精品一区二区三区四 | 538精品视频 | 免费美女黄色 | 欧美在线观看网站 | 欧美亚洲国产精品久久蜜芽 | 一级毛片高清免费播放 | 国产jjzzjjzz视频全部 | 中文字幕在线播放不卡 | 新香蕉视频 | 超清波多野结衣精品一区 | 91国内精品久久久久影院优播 | 一级毛片私人影院 | 女人洗澡一级毛片一级毛片 | 午夜精品一区二区三区在线视 | 2048国产精品原创综合在线 | 午夜影院在线播放 | 亚洲色图图片 |