小編這次要給大家分享的是Android中如何實(shí)現(xiàn)自定義LineLayout實(shí)現(xiàn)滿屏任意拖動功能,文章內(nèi)容豐富,感興趣的小伙伴可以來了解一下,希望大家閱讀完這篇文章之后能夠有所收獲。
公司專注于為企業(yè)提供網(wǎng)站建設(shè)、做網(wǎng)站、微信公眾號開發(fā)、商城網(wǎng)站定制開發(fā),微信小程序,軟件按需求定制設(shè)計等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。憑借多年豐富的經(jīng)驗(yàn),我們會仔細(xì)了解各客戶的需求而做出多方面的分析、設(shè)計、整合,為客戶設(shè)計出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù)。
1.前言
在開發(fā)中,會有需求實(shí)現(xiàn)控件在屏幕隨意拖動,這就需要自定義View,然后在OnTouchEvent事件中,處理MotionEvent.ACTION_MOVE事件,然后通過坐標(biāo)點(diǎn)傳值給onlayout方法,來實(shí)現(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; //當(dāng)水平或者垂直滑動距離大于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)移動后的坐標(biāo)點(diǎn) 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.在代碼中的運(yùn)用
3.這樣就可以在Activity 加載這個xml 來實(shí)現(xiàn)任意拖動功能
看完這篇關(guān)于Android中如何實(shí)現(xiàn)自定義LineLayout實(shí)現(xiàn)滿屏任意拖動功能的文章,如果覺得文章內(nèi)容寫得不錯的話,可以把它分享出去給更多人看到。