Skip to content

Commit 183de48

Browse files
committed
修复拖拽回弹类在一些手机无法接收点击事件的问题
1 parent 49485ae commit 183de48

File tree

9 files changed

+64
-57
lines changed

9 files changed

+64
-57
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 超级 Toast
1+
# 悬浮窗框架
22

33
> 码云地址:[Gitee](https://gitee.com/getActivity/XToast)
44
@@ -14,7 +14,8 @@
1414

1515
```groovy
1616
dependencies {
17-
implementation 'com.hjq:xtoast:6.0'
17+
// 悬浮窗框架:https://github.com/getActivity/XToast
18+
implementation 'com.hjq:xtoast:6.2'
1819
}
1920
```
2021

@@ -65,7 +66,7 @@ new XToast(XToastActivity.this)
6566

6667
#### 作者的其他开源项目
6768

68-
* 安卓架构[AndroidProject](https://github.com/getActivity/AndroidProject)
69+
* 安卓技术中台[AndroidProject](https://github.com/getActivity/AndroidProject)
6970

7071
* 网络框架:[EasyHttp](https://github.com/getActivity/EasyHttp)
7172

XToast.apk

-21.6 KB
Binary file not shown.

app/build.gradle

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ android {
55

66
defaultConfig {
77
applicationId "com.hjq.xtoast.demo"
8-
minSdkVersion 14
8+
minSdkVersion 16
99
targetSdkVersion 28
10-
versionCode 60
11-
versionName "6.0"
10+
versionCode 62
11+
versionName "6.2"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {
@@ -28,11 +28,11 @@ dependencies {
2828
implementation 'com.android.support:design:28.0.0'
2929

3030
// 权限请求框架:https://github.com/getActivity/XXPermissions
31-
implementation 'com.hjq:xxpermissions:8.8'
31+
implementation 'com.hjq:xxpermissions:9.0'
3232
// 标题栏框架:https://github.com/getActivity/TitleBar
33-
implementation 'com.hjq:titlebar:8.0'
33+
implementation 'com.hjq:titlebar:8.2'
3434
// 吐司工具类:https://github.com/getActivity/ToastUtils
3535
implementation 'com.hjq:toast:8.8'
3636
// 内存泄漏捕捉:https://github.com/square/leakcanary
37-
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
37+
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.5'
3838
}

app/src/main/java/com/hjq/xtoast/demo/MainActivity.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.hjq.xtoast.demo;
22

3-
import android.content.Intent;
43
import android.os.Bundle;
54
import android.support.design.widget.Snackbar;
65
import android.support.v7.app.AppCompatActivity;
@@ -33,6 +32,8 @@ public class MainActivity extends AppCompatActivity {
3332
protected void onCreate(Bundle savedInstanceState) {
3433
super.onCreate(savedInstanceState);
3534
setContentView(R.layout.activity_main);
35+
// 这里需要先初始化 ToastUtils,实际开发中这句代码应当放在 Application.onCreate 方法中
36+
ToastUtils.init(getApplication());
3637
}
3738

3839
public void show1(View v) {
@@ -128,7 +129,6 @@ public void show6(View v) {
128129

129130
@Override
130131
public void hasPermission(List<String> granted, boolean all) {
131-
132132
// 传入 Application 表示这个是一个全局的 Toast
133133
new XToast(getApplication())
134134
.setView(R.layout.toast_phone)
@@ -141,10 +141,11 @@ public void hasPermission(List<String> granted, boolean all) {
141141

142142
@Override
143143
public void onClick(XToast toast, ImageView view) {
144+
ToastUtils.show("我被点击了");
144145
// 点击后跳转到拨打电话界面
145-
Intent intent = new Intent(Intent.ACTION_DIAL);
146-
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
147-
toast.startActivity(intent);
146+
// Intent intent = new Intent(Intent.ACTION_DIAL);
147+
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
148+
// toast.startActivity(intent);
148149
// 安卓 10 在后台跳转 Activity 需要额外适配
149150
// https://developer.android.google.cn/about/versions/10/privacy/changes#background-activity-starts
150151
}
@@ -165,14 +166,14 @@ public void noPermission(List<String> denied, boolean never) {
165166
}
166167

167168
public void show7(View v) {
168-
// 这里需要先初始化 ToastUtils,实际开发中这句代码应当放在 Application.onCreate 方法中
169-
ToastUtils.init(getApplication());
170169
// 将 ToastUtils 中的 View 转移给 XToast 来显示
171-
new XToast(MainActivity.this)
170+
new XToast(this)
172171
.setDuration(1000)
173172
.setView(ToastUtils.getToast().getView())
174173
.setAnimStyle(android.R.style.Animation_Translucent)
175174
.setText(android.R.id.message, "就问你溜不溜")
175+
.setGravity(Gravity.BOTTOM)
176+
.setYOffset(100)
176177
.show();
177178
}
178179
}

library/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ android {
66

77
defaultConfig {
88
minSdkVersion 14
9-
versionCode 60
10-
versionName "6.0"
9+
versionCode 62
10+
versionName "6.2"
1111
}
1212
}
1313

1414
publish {
1515
userOrg = 'getactivity'
1616
groupId = 'com.hjq'
1717
artifactId = 'xtoast'
18-
version = '6.0'
18+
version = '6.2'
1919
description = 'This is a simple and easy to use suspension window frame'
2020
website = "https://github.com/getActivity/XToast"
2121
}

library/src/main/java/com/hjq/xtoast/ToastDismissRunnable.java renamed to library/src/main/java/com/hjq/xtoast/CancelRunnable.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
package com.hjq.xtoast;
2-
3-
import java.lang.ref.WeakReference;
4-
5-
/**
6-
* author : Android 轮子哥
7-
* github : https://github.com/getActivity/XToast
8-
* time : 2019/01/04
9-
* desc : Toast 定时销毁任务
10-
*/
11-
final class ToastDismissRunnable extends WeakReference<XToast>
12-
implements Runnable {
13-
14-
ToastDismissRunnable(XToast toast) {
15-
super(toast);
16-
}
17-
18-
@Override
19-
public void run() {
20-
XToast toast = get();
21-
if (toast != null && toast.isShow()) {
22-
toast.cancel();
23-
}
24-
}
1+
package com.hjq.xtoast;
2+
3+
import java.lang.ref.WeakReference;
4+
5+
/**
6+
* author : Android 轮子哥
7+
* github : https://github.com/getActivity/XToast
8+
* time : 2019/01/04
9+
* desc : Toast 定时销毁任务
10+
*/
11+
final class CancelRunnable extends WeakReference<XToast> implements Runnable {
12+
13+
CancelRunnable(XToast toast) {
14+
super(toast);
15+
}
16+
17+
@Override
18+
public void run() {
19+
XToast toast = get();
20+
if (toast != null && toast.isShow()) {
21+
toast.cancel();
22+
}
23+
}
2524
}

library/src/main/java/com/hjq/xtoast/XToast.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public X setDuration(int duration) {
253253
if (isShow()) {
254254
if (mDuration != 0) {
255255
removeCallbacks();
256-
postDelayed(new ToastDismissRunnable(this), mDuration);
256+
postDelayed(new CancelRunnable(this), mDuration);
257257
}
258258
}
259259
return (X) this;
@@ -390,7 +390,7 @@ public X show() {
390390
mShow = true;
391391
// 如果当前限定了显示时长
392392
if (mDuration != 0) {
393-
postDelayed(new ToastDismissRunnable(this), mDuration);
393+
postDelayed(new CancelRunnable(this), mDuration);
394394
}
395395
// 如果设置了拖拽规则
396396
if (mDraggable != null) {

library/src/main/java/com/hjq/xtoast/draggable/BaseDraggable.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ protected int getStatusBarHeight() {
5757
return frame.top;
5858
}
5959

60+
/**
61+
* 更新悬浮窗的位置
62+
*
63+
* @param x x 坐标
64+
* @param y y 坐标
65+
*/
6066
protected void updateLocation(float x, float y) {
6167
updateLocation((int) x, (int) y);
6268
}
@@ -90,6 +96,6 @@ protected void updateLocation(int x, int y) {
9096
* @param upY 手指抬起时的 y 坐标
9197
*/
9298
protected boolean isTouchMove(float downX, float upX, float downY, float upY) {
93-
return downX != upX || downY != upY;
99+
return ((int) downX) != ((int) upX) || ((int) (downY)) != ((int) upY);
94100
}
95101
}

library/src/main/java/com/hjq/xtoast/draggable/SpringDraggable.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,33 @@ public class SpringDraggable extends BaseDraggable {
2222
@SuppressLint("ClickableViewAccessibility")
2323
@Override
2424
public boolean onTouch(View v, MotionEvent event) {
25-
int rawMoveX;
26-
int rawMoveY;
25+
float rawMoveX;
26+
float rawMoveY;
2727

2828
switch (event.getAction()) {
2929
case MotionEvent.ACTION_DOWN:
3030
// 记录按下的位置(相对 View 的坐标)
31-
mViewDownX = (int) event.getX();
32-
mViewDownY = (int) event.getY();
31+
mViewDownX = event.getX();
32+
mViewDownY = event.getY();
3333
break;
3434
case MotionEvent.ACTION_MOVE:
3535
// 记录移动的位置(相对屏幕的坐标)
36-
rawMoveX = (int) event.getRawX();
37-
rawMoveY = (int) (event.getRawY() - getStatusBarHeight());
36+
rawMoveX = event.getRawX();
37+
rawMoveY = event.getRawY() - getStatusBarHeight();
3838
// 更新移动的位置
3939
updateLocation(rawMoveX - mViewDownX, rawMoveY - mViewDownY);
4040
break;
4141
case MotionEvent.ACTION_UP:
4242
// 记录移动的位置(相对屏幕的坐标)
43-
rawMoveX = (int) event.getRawX();
44-
rawMoveY = (int) (event.getRawY() - getStatusBarHeight());
43+
rawMoveX = event.getRawX();
44+
rawMoveY = event.getRawY() - getStatusBarHeight();
4545
// 获取当前屏幕的宽度
4646
int screenWidth = getScreenWidth();
4747
// 自动回弹吸附
4848
final float rawFinalX;
49-
if (rawMoveX < screenWidth / 2) {
49+
if (rawMoveX < screenWidth / 2f) {
5050
// 回弹到屏幕左边
51-
rawFinalX = 0;
51+
rawFinalX = 0f;
5252
} else {
5353
// 回弹到屏幕右边
5454
rawFinalX = screenWidth;

0 commit comments

Comments
 (0)