仿微信右滑销毁Activity
来源:互联网 发布:元数据由什么存储 编辑:程序博客网 时间:2024/05/19 21:44
版权声明:本文为博主原创文章,未经博主允许不得转载。
一直想自己实现Activity的侧滑销毁,无奈在finish activity的时候遇到了瓶颈。今天看到一个开源库,那哥们把Activity的背景设置成了透明,这才恍然大悟。这里share一下。
大体思路讲解:
- 1.获取Activity的DecorView
- 2.自定义布局,重写onInterceptTouchEvent与onTouchEvent函数对触摸事件进行处理,根据手势滑动对DecorView(内容)进行滑动
- 3.根据滑动的距离判断是销毁当前的Activity还是滑动恢复到原来位置
知识储备:
- Android事件分发,请看我这篇博文
- Activity、Window、DecorView讲解,请看这里
- Scroller详解,请看xiaanming大神的博文
贴出核心类
public class SlidingFinishLayout extends FrameLayout { private Scroller mScroller; private int mTouchSlop; private ViewGroup parentView; private int screenWidth; private int last_x; private int last_y; private int delay_x; private int delay_y; private boolean isFinish = false; private Activity mActivity; private SlidingFinishLayout.OnActivityFinishListener onActivityFinish; public SlidingFinishLayout(Context context, AttributeSet attrs) { super(context, attrs); mScroller = new Scroller(context); mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); } public void attachActivity(Activity activity) { this.mActivity = activity; } public void detachActivity() { if (this.mActivity != null) { mActivity = null; } } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (changed) { ViewGroup decor = (ViewGroup) mActivity.getWindow().getDecorView(); mActivity.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); mActivity.getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT); View decorChild = decor.findViewById(android.R.id.content); while (decorChild.getParent() != decor) { decorChild = (View) decorChild.getParent(); } parentView = decor; screenWidth = getWidth(); } } /** * 进行事件拦截 * * @param ev * @return */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { boolean isIntercept = false; switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: last_x = (int) ev.getRawX(); last_y = (int) ev.getRawY(); break; case MotionEvent.ACTION_MOVE: int current_x = (int) ev.getRawX(); int current_y = (int) ev.getRawY(); delay_x = current_x - last_x; delay_y = current_y - last_y; if ((Math.abs(delay_x) / (Math.abs(delay_y) + 1) >= 2)) { //30度角的时候才开始拦截事件 isIntercept = true; } break; case MotionEvent.ACTION_UP: break; } return isIntercept; } /** * 进行事件处理 * * @param ev * @return */ @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: last_x = (int) ev.getRawX(); last_y = (int) ev.getRawY(); break; case MotionEvent.ACTION_MOVE: int current_x = (int) ev.getRawX(); int current_y = (int) ev.getRawY(); delay_x = current_x - last_x; delay_y = current_y - last_y; if (Math.abs(delay_x) > mTouchSlop) { if ((Math.abs(delay_x) / (Math.abs(delay_y) + 1) >= 2)) { //30度角的时候才开始处理事件 parentView.scrollBy(-delay_x, 0); //开始滑动 } else { return false; } } else { return false; } last_x = current_x; last_y = current_y; break; case MotionEvent.ACTION_UP: if (Math.abs(parentView.getScrollX()) > (screenWidth / 3)) { //如果滑动距离超出屏幕三分之一,则进行滑动销毁,否则恢复原位 scrollFinish(); } else { scrollOriginal(); } break; } return true; } /** * 滑动销毁 */ private void scrollFinish() { final int delta = (screenWidth + parentView.getScrollX()); mScroller.startScroll(parentView.getScrollX(), 0, -delta + 1, 0); postInvalidate(); isFinish = true; } /** * 恢复到原始状态 */ private void scrollOriginal() { mScroller.startScroll(parentView.getScrollX(), 0, -parentView.getScrollX(), 0); postInvalidate(); isFinish = false; } /** * Activity销毁的时候通知回调 * * @param onActivityFinish */ public void setOnActivityFinishListener(SlidingFinishLayout.OnActivityFinishListener onActivityFinish) { this.onActivityFinish = onActivityFinish; } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { parentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); postInvalidate(); if (mScroller.isFinished() && isFinish) { onActivityFinish.onActivityFinish(); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
在希望右滑销毁的Activity中使用
public class SlideFinishActivity extends AppCompatActivity implements SlidingFinishLayout.OnActivityFinishListener { SlidingFinishLayout sf_test; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_slide_finish); sf_test = (SlidingFinishLayout) findViewById(R.id.sf_test); sf_test.setOnActivityFinishListener(this); //设置销毁的监听器 sf_test.attachActivity(this); } @Override protected void onDestroy() { super.onDestroy(); sf_test.detachActivity(); } @Override public void onActivityFinish() { finish(); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
注意,该Activity的Style文件应该加入下面代码才可以生效 Android:windowIsTranslucent。本代码是把Activity设置成透明状态
收工。
项目源码:https://github.com/WillSprint/SlideFinish
推荐一个侧滑库,本博客中的一些思想来源于该作者
https://github.com/Jude95/SwipeBackHelper
0 0
- 仿微信右滑销毁Activity
- 仿微信右滑销毁Activity
- 销毁 Activity
- activity销毁指定activity
- activity退出,销毁线程
- Activity销毁与重建
- 销毁全部Activity
- 手势滑动销毁Activity
- activity之销毁
- activity之销毁(二)
- 销毁一个Activity
- Activity 启动与销毁
- 销毁一个Activity
- activity销毁处理
- Android-销毁activity
- Android之Activity销毁
- 手势滑动销毁Activity
- 销毁远程的Activity
- [C#] 継承されているディレクトリのアクセス権を変更、削除する
- Glide踩坑记
- C/C++求职笔记
- Play Framework 工程目录结构
- Spring-Batch简要概述
- 仿微信右滑销毁Activity
- linux 系统中调用执行脚本
- 【MVC】 action方法接收浏览器参数方式
- linux多线程编程
- session共享
- git 版本控制
- CTS 问题 第三篇
- [C#] ディレクトリのアクセス権を削除する
- MDK5新建stm32l0xx(Cortex-M0)工程步骤