Android實(shí)現(xiàn)左滑關(guān)閉窗口
前言
目前市場(chǎng)很多的APP都帶有窗口滑動(dòng)切換關(guān)閉,這種切換,使得用戶操作比較爽,而且覺得功能點(diǎn)上也比較大氣,在此就是自己總結(jié)了一個(gè)簡易的方法,直接替換在基礎(chǔ)窗口里面,使用安卓最基礎(chǔ)的方法進(jìn)行實(shí)現(xiàn);
需求說明
1、首先是明確從哪里滑動(dòng):一般習(xí)慣都是從左邊緣開始滑動(dòng)
2、手指在滑動(dòng)的時(shí)候頁面進(jìn)行移動(dòng)
3、松開手指之后,要判斷是否滑出關(guān)閉,還是恢復(fù)以前狀態(tài);
實(shí)現(xiàn)的代碼
一、繼承一個(gè)幀布局,重寫方法:
public class ArActSlidLayout extends FrameLayout { // 頁面邊緣陰影的寬度默認(rèn)值 private static final int SHADOW_WIDTH = 16; private Activity mActivity; private Scroller mScroller; //安卓自帶的一個(gè)滑動(dòng)計(jì)算的類,只做計(jì)算,不參與邏輯; // 頁面邊緣的陰影圖 private Drawable mLeftShadow;? // 頁面邊緣陰影的寬度 private int mShadowWidth; private int mInterceptDownX; ?//手指按下,攔截的x值 private int mLastInterceptX;//記錄最后一次坐標(biāo) private int mLastInterceptY; private int mTouchDownX; //消費(fèi)的x值 private int mLastTouchX; private int mLastTouchY; private boolean isConsumed = false;是否可以滑動(dòng) public ArActSlidLayout(Context context) { ? ? this(context, null); } public ArActSlidLayout(Context context, AttributeSet attrs) { ? ? this(context, attrs, 0); } public ArActSlidLayout(Context context, AttributeSet attrs, int defStyleAttr) { ? ? super(context, attrs, defStyleAttr); ? ? initView(context); } private void initView(Context context) { ? ? mScroller = new Scroller(context); ? ? mLeftShadow = getResources().getDrawable(R.drawable.left_shadow);//得到陰影的圖形 ? ? int density = (int) getResources().getDisplayMetrics().density; ? ? mShadowWidth = SHADOW_WIDTH * density;//得到實(shí)際像素的寬度; } /** ?* 綁定Activity ?*/ public void bindActivity(Activity activity) { ? ? mActivity = activity; ? ? ViewGroup decorView = (ViewGroup) mActivity.getWindow().getDecorView(); ? ? View child = decorView.getChildAt(0); ? ? decorView.removeView(child); ? ? addView(child); ? ? decorView.addView(this);//把整個(gè)布局添加的窗口的ViewGroup中; } @Override public boolean onInterceptTouchEvent(MotionEvent ev) {//進(jìn)行事件的是否攔截 ? ? boolean intercept = false; ? ? int x = (int) ev.getX(); ? ? int y = (int) ev.getY(); ? ? switch (ev.getAction()) { ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? intercept = false; ? ? ? ? ? ? mInterceptDownX = x; ? ? ? ? ? ? mLastInterceptX = x; ? ? ? ? ? ? mLastInterceptY = y; ? ? ? ? ? ? break; ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? int deltaX = x - mLastInterceptX; ? ? ? ? ? ? int deltaY = y - mLastInterceptY; ? ? ? ? ? ? // 手指處于屏幕邊緣,且橫向滑動(dòng)距離大于縱向滑動(dòng)距離時(shí),攔截事件 ? ? ? ? ? ? if (mInterceptDownX < (getWidth() / 10) && Math.abs(deltaX) > Math.abs(deltaY)) { ? ? ? ? ? ? ? ? intercept = true;//滿足這個(gè)條件進(jìn)行攔截; ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? intercept = false; ? ? ? ? ? ? } ? ? ? ? ? ? mLastInterceptX = x; ? ? ? ? ? ? mLastInterceptY = y; ? ? ? ? ? ? break; ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? intercept = false; ? ? ? ? ? ? mInterceptDownX = mLastInterceptX = mLastInterceptY = 0;//恢復(fù)數(shù)據(jù); ? ? ? ? ? ? break; ? ? } ? ? //Log.e("event", " Intercep ?" + " ?x: ?" + x + " ?y: ?" + y); ? ? return intercept; } @Override public boolean onTouchEvent(MotionEvent ev) {//事件的消費(fèi),具體邏輯的編寫; ? ? int x = (int) ev.getX(); ? ? int y = (int) ev.getY(); ? ? switch (ev.getAction()) { ? ? ? ? case MotionEvent.ACTION_DOWN: ? ? ? ? ? ? mTouchDownX = x; ? ? ? ? ? ? mLastTouchX = x; ? ? ? ? ? ? mLastTouchY = y; ? ? ? ? ? ? //Log.e("event", " onTouchEventDOWN ?" + " ?x: ?" + x + " ?y: ?" + y); ? ? ? ? ? ? break; ? ? ? ? case MotionEvent.ACTION_MOVE: ? ? ? ? ? ? int deltaX = x - mLastTouchX; ? ? ? ? ? ? int deltaY = y - mLastTouchY; ? ? ? ? ? ? if (!isConsumed && mTouchDownX < (getWidth() / 10) && Math.abs(deltaX) > Math.abs(deltaY)) { ? ? ? ? ? ? ? ? isConsumed = true; //移動(dòng)的條件 ? ? ? ? ? ? } ? ? ? ? ? ? if (isConsumed) { ? ? ? ? ? ? ? ? int rightMovedX = mLastTouchX - (int) ev.getX(); ? ? ? ? ? ? ? ? // 左側(cè)即將滑出屏幕 ? ? ? ? ? ? ? ? if (getScrollX() + rightMovedX >= 0) { ? ? ? ? ? ? ? ? ? ? ?//移動(dòng)到某一點(diǎn) ? ? ? ? ? ? ? ? ? ? scrollTo(0, 0); ? ? ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? ? ? //相當(dāng)于累加的移動(dòng) ? ? ? ? ? ? ? ? ? ? scrollBy(rightMovedX, 0); //手指滑動(dòng)移動(dòng)整個(gè)屏幕;負(fù)數(shù):代表向右移動(dòng) ,反之,像做 ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ?// Log.e("event", " onTouchEventMOVE ?" + " ?x: ?" + x + " ?y: ?" + y + " ScrollX: ?" + getScrollX() + " rightMovedX: " + rightMovedX); ? ? ? ? ? ? } ? ? ? ? ? ? mLastTouchX = x; ? ? ? ? ? ? mLastTouchY = y; ? ? ? ? ? ? break; ? ? ? ? case MotionEvent.ACTION_UP: ? ? ? ? ? ? isConsumed = false; ? ? ? ? ? ? mTouchDownX = mLastTouchX = mLastTouchY = 0; //消除數(shù)據(jù); ? ? ? ? ? ? // 根據(jù)手指釋放時(shí)的位置決定回彈還是關(guān)閉 ? ? ? ? ? ? if (-getScrollX() < getWidth() / 3) { ? ? ? ? ? ? ? ? scrollBack(); ? ? ? ? ? ? } else { ? ? ? ? ? ? ? ? scrollClose(); ? ? ? ? ? ? } ? ? ? ? ? ? //Log.e("event", " onTouchEventUP ?" + " ?x: ?" + x + " ?y: ?" + y + " ScrollX: ?" + getScrollX()); ? ? ? ? ? ? break; ? ? } ? ? //Log.e("event"," onTouchEventAll ?" + " ?getRawX(): ?"+ev.getRawX() +" ?y: ?" +ev.getRawY()); ? ? return true; } /** ?* 滑動(dòng)返回 ?*/ private void scrollBack() { ? ? int startX = getScrollX(); ? ? int dx = -getScrollX(); ? ? mScroller.startScroll(startX, 0, dx, 0, 300); ? ? invalidate(); } /** ?* 滑動(dòng)關(guān)閉 ?*/ private void scrollClose() { ? ? int startX = getScrollX(); ? ? int dx = -getScrollX() - getWidth(); ? ? //Log.e("event", " ?scrollClose: ?dx " + dx + " ?getWidth " + getWidth()); ? ? mScroller.startScroll(startX, 0, dx, 0, 300); ? ? invalidate(); } @Override public void computeScroll() { ? ? if (mScroller.computeScrollOffset()) { ? ? ? ? scrollTo(mScroller.getCurrX(), 0); ? ? ? ? //Log.e("event", " ?computeScroll: ?" + mScroller.getCurrX()); ? ? ? ? postInvalidate(); ? ? } else if (-getScrollX() >= getWidth()) { ? ? ? ? mActivity.finish(); ? ? ? ? //Log.e("event", " ?computeScroll: finish ? " + getScrollX()); ? ? } } @Override protected void dispatchDraw(Canvas canvas) { ? ? super.dispatchDraw(canvas); ? ? drawShadow(canvas); } /** ?* 繪制邊緣的陰影 ?*/ private void drawShadow(Canvas canvas) { ? ? mLeftShadow.setBounds(0, 0, mShadowWidth, getHeight()); ? ? canvas.save(); ? ? canvas.translate(-mShadowWidth, 0); ? ? mLeftShadow.draw(canvas); ? ? canvas.restore(); ?} }
二、邊緣的陰影l(fā)eft_shadow.xml:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <!--顏色漸變范圍--> <gradient ? ? android:endColor="#50000000" ? ? android:startColor="#00000000" /> </shape>
三、在baseActivity里面添加代碼:
public class ArBaseFragActivity extends BaseMMCFragmentActivity { ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? super.onCreate(savedInstanceState); ? ? if (setEnableSliding()) { ? ? ? ? ArActSlidLayout rootView = new ArActSlidLayout(this); ? ? ? ? rootView.bindActivity(this);//綁定需要窗口的布局: ? ? ?} ? } ?protected boolean setEnableSliding() {//默認(rèn)返回是需要的,只需繼承的時(shí)候重寫次代碼 true : 需要;false :不需要; ? ? return true; ? } }
四、總結(jié):這種實(shí)現(xiàn)還有不少的方法,我這里只是推薦了自己覺得不錯(cuò)的方法,有需要的童鞋們直接copy項(xiàng)目中去用就行了,里面涉及到很多安卓自帶的方法,還需要你們自己消化理解,我在實(shí)現(xiàn)代碼中也進(jìn)行了注釋,可以幫助你們理解消化,有什么不足的地方,還請(qǐng)你們多多指教,使得我后期改進(jìn)更多。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android PopupWindow全屏詳細(xì)介紹及實(shí)例代碼
這篇文章主要介紹了 Android PopupWindow全屏詳細(xì)介紹及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-12-12Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能
本篇文章主要介紹了Android中AlarmManager+Notification實(shí)現(xiàn)定時(shí)通知提醒功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-10-10Android中EditText光標(biāo)在4.0中的bug及解決方法
這篇文章主要介紹了Android中EditText光標(biāo)在4.0中的bug及解決方法,簡單分析了Android4.0版本中EditText光標(biāo)消息的原因及相應(yīng)的解決方法,需要的朋友可以參考下2016-01-01Android實(shí)現(xiàn)截圖和分享功能的代碼
截圖和分享功能大家都玩過,下面通過本文給大家介紹Android實(shí)現(xiàn)截圖和分享功能,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07Android自定義View實(shí)現(xiàn)九宮格圖形解鎖(Kotlin版)
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)九宮格圖形解鎖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新
對(duì)于ListView數(shù)據(jù)的刷新大家都知道,改變Adapter的數(shù)據(jù)源,然后調(diào)用Adapter的notifyDateSetChanged()方法即可。通過本篇文章給大家詳細(xì)介紹Android開發(fā)之ListView實(shí)現(xiàn)Item局部刷新,感興趣的朋友一起學(xué)習(xí)吧2015-10-10Android關(guān)于Glide的使用(高斯模糊、加載監(jiān)聽、圓角圖片)
這篇文章主要為大家詳細(xì)介紹了Android關(guān)于Glide的使用,內(nèi)容豐富,高斯模糊、加載監(jiān)聽、圓角圖片希望大家可以掌握,感興趣的小伙伴們可以參考一下2016-11-11Android實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換
這篇文章主要介紹了Android實(shí)現(xiàn)WIFI和GPRS網(wǎng)絡(luò)的切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11