电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

剖析Android Activity側(cè)滑返回的實(shí)現(xiàn)原理

瀏覽:52日期:2022-09-17 17:43:34
簡(jiǎn)介

使用側(cè)滑Activity返回很常見,例如微信就用到了。那么它是怎么實(shí)現(xiàn)的呢。本文帶你剖析一下實(shí)現(xiàn)原理。我在github上找了一個(gè)star有2.6k的開源,我們分析他是怎么實(shí)現(xiàn)的

//star 2.6k’com.r0adkll:slidableactivity:2.0.5’Slidr使用示例

它的使用很簡(jiǎn)單,首先要設(shè)置透明的窗口背景

<style name='AppTheme' parent='Theme.AppCompat.Light.DarkActionBar'><!-- Customize your theme here. --><item name='android:textAllCaps'>false</item><item name='android:windowActionBar'>false</item><item name='windowActionBar'>false</item><item name='windowNoTitle'>true</item><item name='colorPrimary'>@color/colorPrimary</item><item name='colorPrimaryDark'>@color/colorPrimaryDark</item><item name='colorAccent'>@color/colorAccent</item><item name='android:windowIsTranslucent'>true</item><item name='android:windowBackground'>@android:color/transparent</item> </style>

然后

//setContent(View view)后Slidr.attach(this);

剖析Android Activity側(cè)滑返回的實(shí)現(xiàn)原理

下面可以從三個(gè)步驟看其原理

步驟一 重新包裹界面

Slidr.class

public static SlidrInterface attach(final Activity activity, final int statusBarColor1, final int statusBarColor2){//0 創(chuàng)建滑動(dòng)嵌套界面SliderPanelfinal SliderPanel panel = initSliderPanel(activity, null);//7 Set the panel slide listener for when it becomes closed or opened// 監(jiān)聽回調(diào)panel.setOnPanelSlideListener(new SliderPanel.OnPanelSlideListener() {... //open close等});// Return the lock interfacereturn initInterface(panel); }private static SliderPanel initSliderPanel(final Activity activity, final SlidrConfig config) {//3 獲取decorviewViewGroup decorView = (ViewGroup)activity.getWindow().getDecorView();//4 獲取我們布局的內(nèi)容并刪除View oldScreen = decorView.getChildAt(0);decorView.removeViewAt(0);//5 Setup the slider panel and attach it to the decor// 建立滑動(dòng)嵌套視圖SliderPanel并且添加到DecorView中SliderPanel panel = new SliderPanel(activity, oldScreen, config);panel.setId(R.id.slidable_panel);oldScreen.setId(R.id.slidable_content);//6 把我們的界面布局添加到SliderPanel,并且把SliderPanel添加到decorView中panel.addView(oldScreen);decorView.addView(panel, 0);return panel;}

步驟二 使用ViewDragHelper.class處理滑動(dòng)手勢(shì)

SliderPanel.class

private void init(){ ... //1 ViewDragHelper創(chuàng)建 mDragHelper = ViewDragHelper.create(this, mConfig.getSensitivity(), callback); mDragHelper.setMinVelocity(minVel); mDragHelper.setEdgeTrackingEnabled(mEdgePosition); //2 Setup the dimmer view 添加用于指示滑動(dòng)過程的View到底層 mDimView = new View(getContext()); mDimView.setBackgroundColor(mConfig.getScrimColor()); mDimView.setAlpha(mConfig.getScrimStartAlpha()); addView(mDimView);}

步驟三 在ViewDragHelper.Callback中處理我們的界面的拖動(dòng)

我們首先明確ViewDragHelper僅僅是處理ParentView與它子View的關(guān)系,不會(huì)一直遍歷到最頂層的View。ViewDragHelper的捕獲capture是這樣實(shí)現(xiàn)的

@Nullable public View findTopChildUnder(int x, int y) {final int childCount = mParentView.getChildCount();for (int i = childCount - 1; i >= 0; i--) { final View child = mParentView.getChildAt(mCallback.getOrderedChildIndex(i)); if (x >= child.getLeft() && x < child.getRight() && y >= child.getTop() && y < child.getBottom()) {return child; }}return null; }

重點(diǎn)在SliderPanel.class的ViewDragHelper.Callback callback的實(shí)現(xiàn),作者實(shí)現(xiàn)實(shí)現(xiàn)了很多個(gè)方向的滑動(dòng)處理mLeftCallback、mRightCallback、mTopCallback、mBottomCallback、mVerticalCallback、mHorizontalCallback, 我們?nèi)LeftCallback來分析

private ViewDragHelper.Callback mLeftCallback = new ViewDragHelper.Callback() { //捕獲View @Override public boolean tryCaptureView(View child, int pointerId) {boolean edgeCase = !mConfig.isEdgeOnly() || mDragHelper.isEdgeTouched(mEdgePosition, pointerId);//像前面說的,我們的內(nèi)容是最上層子View,mDecorView這里指的是我們的contentViewreturn child.getId() == mDecorView.getId() && edgeCase; } //拖動(dòng), 最終是通過view.offsetLeftAndRight(offset)實(shí)現(xiàn)移動(dòng) @Override public int clampViewPositionHorizontal(View child, int left, int dx) {return clamp(left, 0, mScreenWidth); } //滑動(dòng)范圍 @Override public int getViewHorizontalDragRange(View child) {return mScreenWidth; } //釋放處理,判斷是滾回屏幕 @Override public void onViewReleased(View releasedChild, float xvel, float yvel) {super.onViewReleased(releasedChild, xvel, yvel);int left = releasedChild.getLeft();int settleLeft = 0;int leftThreshold = (int) (getWidth() * mConfig.getDistanceThreshold());boolean isVerticalSwiping = Math.abs(yvel) > mConfig.getVelocityThreshold();if(xvel > 0){ if(Math.abs(xvel) > mConfig.getVelocityThreshold() && !isVerticalSwiping){settleLeft = mScreenWidth; }else if(left > leftThreshold){settleLeft = mScreenWidth; }}else if(xvel == 0){ if(left > leftThreshold){settleLeft = mScreenWidth; }}//滾動(dòng)到left=0(正常布局) 或者 滾動(dòng)到left=mScreenWidth(滾出屏幕)關(guān)閉ActivitymDragHelper.settleCapturedViewAt(settleLeft, releasedChild.getTop());invalidate(); } //轉(zhuǎn)換位置百分比,確定指示層的透明度 @Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {super.onViewPositionChanged(changedView, left, top, dx, dy);float percent = 1f - ((float)left / (float)mScreenWidth);if(mListener != null) mListener.onSlideChange(percent);// Update the dimmer alphaapplyScrim(percent); } //回調(diào)到Slidr處理Activity狀態(tài) @Override public void onViewDragStateChanged(int state) {super.onViewDragStateChanged(state);if(mListener != null) mListener.onStateChanged(state);switch (state){ case ViewDragHelper.STATE_IDLE:if(mDecorView.getLeft() == 0){ // State Open if(mListener != null) mListener.onOpened();}else{ // State Closed 這里回調(diào)到Slidr處理activity.finish() if(mListener != null) mListener.onClosed();}break; case ViewDragHelper.STATE_DRAGGING:break; case ViewDragHelper.STATE_SETTLING:break;} }};

對(duì)于mDragHelper.settleCapturedViewAt(settleLeft, releasedChild.getTop());內(nèi)部是使用Scroller.class輔助滾動(dòng),所以要在SliderPanel中重寫View.computeScroll()

@Overridepublic void computeScroll() { super.computeScroll(); if(mDragHelper.continueSettling(true)){ViewCompat.postInvalidateOnAnimation(this); }}總結(jié)

整體方案如下圖所示

剖析Android Activity側(cè)滑返回的實(shí)現(xiàn)原理

總體來看原理并不復(fù)雜, 就是通過ViewDragHelper對(duì)View進(jìn)行拖動(dòng)。

以上就是Android Activity側(cè)滑返回的實(shí)現(xiàn)原理的詳細(xì)內(nèi)容,更多關(guān)于Activity側(cè)滑返回的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 国际线缆连接网 - 连接器_线缆线束加工行业门户网站 | 润东方环保空调,冷风机,厂房车间降温设备-20年深圳环保空调生产厂家 | 硫化罐-电加热蒸汽硫化罐生产厂家-山东鑫泰鑫智能装备有限公司 | 联系我们-腾龙公司上分客服微信19116098882 | 专业广州网站建设,微信小程序开发,一物一码和NFC应用开发、物联网、外贸商城、定制系统和APP开发【致茂网络】 | 重庆磨床过滤机,重庆纸带过滤机,机床伸缩钣金,重庆机床钣金护罩-重庆达鸿兴精密机械制造有限公司 | 临沂招聘网_人才市场_招聘信息_求职招聘找工作请认准【马头商标】 | 工业洗衣机_工业洗涤设备_上海力净工业洗衣机厂家-洗涤设备首页 bkzzy在职研究生网 - 在职研究生招生信息咨询平台 | DDoS安全防护官网-领先的DDoS安全防护服务商 | 低温等离子清洗机(双气路进口)-嘉润万丰 | 网带通过式抛丸机,,网带式打砂机,吊钩式,抛丸机,中山抛丸机生产厂家,江门抛丸机,佛山吊钩式,东莞抛丸机,中山市泰达自动化设备有限公司 | 安全阀_弹簧式安全阀_美标安全阀_工业冷冻安全阀厂家-中国·阿司米阀门有限公司 | 天品互联-北京APP开发公司-小程序开发制作-软件开发 | 口信网(kousing.com) - 行业资讯_行业展会_行业培训_行业资料 | 北京租车公司_汽车/客车/班车/大巴车租赁_商务会议/展会用车/旅游大巴出租_北京桐顺创业租车公司 | 手持气象站_便携式气象站_农业气象站_负氧离子监测站-山东万象环境 | 卸料器-卸灰阀-卸料阀-瑞安市天蓝环保设备有限公司 | 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 排烟防火阀-消防排烟风机-正压送风口-厂家-价格-哪家好-德州鑫港旺通风设备有限公司 | 换链神器官网-友情链接交换、购买交易于一体的站长平台 | 番茄畅听邀请码怎么输入 - Dianw8.com | 海峰资讯 - 专注装饰公司营销型网站建设和网络营销培训 | 回转炉,外热式回转窑,回转窑炉-淄博圣元窑炉工程有限公司 | 【中联邦】增稠剂_增稠粉_水性增稠剂_涂料增稠剂_工业增稠剂生产厂家 | 视觉检测设备_自动化检测设备_CCD视觉检测机_外观缺陷检测-瑞智光电 | 脱硝喷枪-氨水喷枪-尿素喷枪-河北思凯淋环保科技有限公司 | 众品地板网-地板品牌招商_地板装修设计_地板门户的首选网络媒体。 | 天津散热器_天津暖气片_天津安尼威尔散热器制造有限公司 | 昆山新莱洁净应用材料股份有限公司-卫生级蝶阀,无菌取样阀,不锈钢隔膜阀,换向阀,离心泵 | 台湾阳明固态继电器-奥托尼克斯光电传感器-接近开关-温控器-光纤传感器-编码器一级代理商江苏用之宜电气 | 不锈钢轴流风机,不锈钢电机-许昌光维防爆电机有限公司(原许昌光维特种电机技术有限公司) | 多功能干燥机,过滤洗涤干燥三合一设备-无锡市张华医药设备有限公司 | 陶瓷砂磨机,盘式砂磨机,棒销式砂磨机-无锡市少宏粉体科技有限公司 | 精密钢管,冷拔精密无缝钢管,精密钢管厂,精密钢管制造厂家,精密钢管生产厂家,山东精密钢管厂家 | 贵州自考_贵州自学考试网| 螺杆泵_中成泵业 | 耐磨陶瓷,耐磨陶瓷管道_厂家-淄博拓创陶瓷科技 | 磁力轮,磁力联轴器,磁齿轮,钕铁硼磁铁-北京磁运达厂家 | 深圳市人通智能科技有限公司 | 西安中国国际旅行社(西安国旅) |