国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

1.前言

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

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; } //回調(diào)移動后的坐標點 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 來實現(xiàn)任意拖動功能

總結(jié)

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

標簽: Android
相關(guān)文章:
主站蜘蛛池模板: 久久爱wwwww| 国产精品亚洲欧美日韩一区在线 | 欧美三级 欧美一级 | 成人在线免费观看 | 亚洲免费视频在线 | 国产在线不卡免费播放 | 久久成人午夜 | 99久久免费精品国产免费高清 | 久草在线色站 | 日韩欧美一级毛片视频免费 | 特黄特黄 | 手机福利在线 | 欧美日本一区二区三区生 | 久久成| 中文字幕亚洲天堂 | 国产免费黄色网址 | 最近手机中文字幕1 | 欧美日韩亚洲高清不卡一区二区三区 | 一级黄色毛片免费看 | 大学生久久香蕉国产线观看 | 免费国产成人手机在线观看 | 美女很黄很黄是免费的·无遮挡网站 | 91在线 | 亚洲 | 国产精品视频一区二区猎奇 | 国产精品二区在线 | 久久精品二三区 | 欧美激情特级黄aa毛片 | 最新亚洲情黄在线网站无广告 | 欧美在线观看一区二区 | 男人扒开腿躁女人j | 国产日韩精品视频一区二区三区 | 日本一级毛片高清免费观看视频 | 国产精品亚洲精品一区二区三区 | 手机免费看毛片 | 国产精品国产三级国产普通 | 免看一级一片一在线看 | 久久99国产精品久久99果冻传媒 | 亚洲碰碰 | 国产一区二区三区免费观看 | 成人羞羞视频国产 | 久草最新网址 |