Skip to content
This repository was archived by the owner on Jan 8, 2022. It is now read-only.

Commit 5103ce7

Browse files
committed
修复一个设计问题
1 parent 0e16b99 commit 5103ce7

File tree

6 files changed

+77
-8
lines changed

6 files changed

+77
-8
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.xiasuhuei321.loadingdialog.view;
2+
3+
import android.view.View;
4+
5+
/**
6+
* Created by Luo_xiasuhuei321@163.com on 2016/11/6.
7+
* desc:
8+
*/
9+
public interface FinishDrawListener {
10+
/**
11+
* 分发绘制完成事件
12+
*
13+
* @param v 绘制完成的View
14+
*/
15+
void dispatchFinishEvent(View v);
16+
}

LoadingDialog/src/main/java/com/xiasuhuei321/loadingdialog/view/LoadingDialog.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* Created by Luo on 2016/9/23.
2222
* desc:加载等待的Dialog
2323
*/
24-
public class LoadingDialog {
24+
public class LoadingDialog implements FinishDrawListener {
2525
private Context mContext;
2626

2727
private LVCircularRing mLoadingView;
@@ -41,6 +41,7 @@ public class LoadingDialog {
4141
private int speed = 1;
4242
private long time = 1000;
4343

44+
4445
public enum Speed {
4546
SPEED_ONE,
4647
SPEED_TWO
@@ -76,6 +77,7 @@ private void initView(View view) {
7677
loadingText = (TextView) view.findViewById(R.id.loading_text);
7778
mSuccessView = (RightDiaView) view.findViewById(R.id.rdv_right);
7879
mFailedView = (WrongDiaView) view.findViewById(R.id.wv_wrong);
80+
7981
initData();
8082
}
8183

@@ -84,6 +86,18 @@ private void initData() {
8486
viewList.add(mLoadingView);
8587
viewList.add(mSuccessView);
8688
viewList.add(mFailedView);
89+
90+
mSuccessView.setOnDrawFinishListener(this);
91+
mFailedView.setOnDrawFinishListener(this);
92+
}
93+
94+
@Override
95+
public void dispatchFinishEvent(View v) {
96+
if (v instanceof WrongDiaView) {
97+
h.sendEmptyMessageDelayed(2, time);
98+
} else {
99+
h.sendEmptyMessageDelayed(1, time);
100+
}
87101
}
88102

89103
private void hideAll() {
@@ -186,7 +200,6 @@ public void loadSuccess() {
186200
mSuccessView.setDrawDynamic(openSuccessAnim);
187201
mSuccessView.setVisibility(View.VISIBLE);
188202
loadingText.setText(loadSuccessStr);
189-
h.sendEmptyMessageDelayed(1, time);
190203
}
191204

192205
/**
@@ -198,7 +211,6 @@ public void loadFailed() {
198211
mFailedView.setDrawDynamic(openFailedAnim);
199212
mFailedView.setVisibility(View.VISIBLE);
200213
loadingText.setText(loadFailedStr);
201-
h.sendEmptyMessageDelayed(2, time);
202214
}
203215

204216
/**

LoadingDialog/src/main/java/com/xiasuhuei321/loadingdialog/view/RightDiaView.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
public class RightDiaView extends View {
1717
private final String TAG = this.getClass().getSimpleName();
1818

19+
private FinishDrawListener listener;
20+
1921
private Context mContext;
2022
private int mWidth = 0;
2123
private float mPadding = 0f;
@@ -85,9 +87,13 @@ protected void onDraw(Canvas canvas) {
8587
drawDynamic(canvas);
8688
else {
8789
drawStatic(canvas);
90+
if (listener != null)
91+
listener.dispatchFinishEvent(this);
8892
}
8993
}
9094

95+
private int count = 0;
96+
9197
private void drawDynamic(Canvas canvas) {
9298
if (progress < 100)
9399
progress += speed;
@@ -125,6 +131,13 @@ private void drawDynamic(Canvas canvas) {
125131
}
126132

127133
if (line2_x > radius && progress >= 100 && line1_x != radius / 3) {
134+
//1.只分发一次绘制完成的事件
135+
//2.只在最后一次绘制时分发
136+
if (count == 0 && times == 0 && listener != null) {
137+
listener.dispatchFinishEvent(this);
138+
count++;
139+
}
140+
128141
times--;
129142
if (times >= 0) {
130143
reDraw();
@@ -196,5 +209,8 @@ protected void setDrawColor(int color) {
196209
mPaint.setColor(color);
197210
}
198211

212+
public void setOnDrawFinishListener(FinishDrawListener f) {
213+
this.listener = f;
214+
}
199215
}
200216

LoadingDialog/src/main/java/com/xiasuhuei321/loadingdialog/view/SizeUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
* desc:
88
*/
99

10-
class SizeUtils {
10+
public class SizeUtils {
1111
/**
1212
* dp转px
1313
*/
14-
static int dip2px(Context context, float dpValue) {
14+
public static int dip2px(Context context, float dpValue) {
1515
final float scale = context.getResources().getDisplayMetrics().density;
1616
return (int) (dpValue * scale + 0.5f);
1717
}

LoadingDialog/src/main/java/com/xiasuhuei321/loadingdialog/view/WrongDiaView.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
public class WrongDiaView extends View {
1717
private final String TAG = this.getClass().getSimpleName();
1818

19+
private FinishDrawListener listener;
20+
1921
private Context mContext;
2022
private int mWidth = 0;
2123
private float mPadding = 0f;
@@ -31,6 +33,7 @@ public class WrongDiaView extends View {
3133
private int times = 0;
3234
private boolean drawEveryTime = true;
3335
private int speed = 1;
36+
private int count = 0;
3437
// private int color;
3538

3639
public WrongDiaView(Context context) {
@@ -102,6 +105,8 @@ protected void onDraw(Canvas canvas) {
102105
drawDynamic(canvas);
103106
else {
104107
drawStatic(canvas);
108+
if (listener != null)
109+
listener.dispatchFinishEvent(this);
105110
}
106111
}
107112

@@ -139,6 +144,12 @@ private void drawDynamic(Canvas canvas) {
139144
line2_startX + line2_x, line1_start + line2_y, mPaint);
140145

141146
if ((line2_startX - line2_y) < line1_start) {
147+
//1.只分发一次绘制完成的事件
148+
//2.只在最后一次绘制时分发
149+
if (count == 0 && times == 0 && listener != null) {
150+
listener.dispatchFinishEvent(this);
151+
count++;
152+
}
142153
times--;
143154
if (times >= 0) {
144155
reDraw();
@@ -206,4 +217,8 @@ protected void setSpeed(int speed) {
206217
protected void setDrawColor(int color) {
207218
mPaint.setColor(color);
208219
}
220+
221+
public void setOnDrawFinishListener(FinishDrawListener f) {
222+
this.listener = f;
223+
}
209224
}

app/src/main/java/com/xiasuhuei321/sample/MainActivity.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public void onClick(View v) {
110110
.setLoadSpeed(speed)
111111
.setRepeatCount(repeatTime)
112112
.setDrawColor(color)
113+
.setShowTime(5000)//延时5秒自动关闭,默认1秒
113114
.show();
114115
h.sendEmptyMessageDelayed(LOAD_FAILED, delayedTime);
115116
saveForThesePeopleWhoDoNotCallCloseAndUseInterceptBackMethod(intercept_back_event);
@@ -145,6 +146,11 @@ public void onClick(View v) {
145146
}
146147
}
147148

149+
/**
150+
* 本来是想借此给拦截了back导致无法返回用户界面的情况,给出一种解决方法,
151+
* 已写入api中,你可以调用setShowTime()来指定反馈dialog展示的时长(自完全绘制完毕开始计算),
152+
* 但是需要注意的是loading界面仍然可能导致用户无法与你的界面交互。
153+
*/
148154
private void saveForThesePeopleWhoDoNotCallCloseAndUseInterceptBackMethod(boolean intercept_back_event) {
149155
if (intercept_back_event) {
150156
Toast.makeText(this, "don't be worried,this dialog will be closed " +
@@ -170,7 +176,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
170176
case R.id.speed:
171177
speed = speed == LoadingDialog.Speed.SPEED_ONE ?
172178
LoadingDialog.Speed.SPEED_TWO : LoadingDialog.Speed.SPEED_ONE;
173-
int speedInfo = 1;
179+
int speedInfo;
174180
if (speed == LoadingDialog.Speed.SPEED_ONE) {
175181
speedInfo = 1;
176182
} else {
@@ -181,14 +187,18 @@ public boolean onOptionsItemSelected(MenuItem item) {
181187

182188
case R.id.intercept:
183189
intercept_back_event = !intercept_back_event;
184-
Toast.makeText(this, "now the dialog will intercept the back event", Toast.LENGTH_SHORT).show();
190+
if (intercept_back_event)
191+
Toast.makeText(this, "now the dialog will intercept the back event", Toast.LENGTH_SHORT).show();
192+
else{
193+
Toast.makeText(this, "now the dialog will accept the back event", Toast.LENGTH_SHORT).show();
194+
}
185195
break;
186196
case R.id.repeat:
187197
repeatTime = repeatTime == 0 ? 1 : 0;
188198
Toast.makeText(this, "now the loading callback will be draw:" + (repeatTime + 1) + " times", Toast.LENGTH_SHORT).show();
189199
break;
190200
case R.id.color:
191-
color = color == Color.WHITE ? Color.BLUE : Color.WHITE;
201+
color = color == Color.argb(100, 255, 255, 255) ? Color.BLUE : Color.argb(100, 255, 255, 255);
192202
Toast.makeText(this, "now the color is:" + color, Toast.LENGTH_SHORT).show();
193203
break;
194204
}

0 commit comments

Comments
 (0)