44import android .animation .AnimatorListenerAdapter ;
55import android .animation .ObjectAnimator ;
66import android .animation .ValueAnimator ;
7+ import android .annotation .SuppressLint ;
78import android .content .Context ;
89import android .content .res .TypedArray ;
910import android .graphics .Bitmap ;
2021import android .support .v4 .content .ContextCompat ;
2122import android .support .v4 .widget .CircularProgressDrawable ;
2223import android .util .AttributeSet ;
24+ import android .view .MotionEvent ;
2325import android .view .View ;
2426
2527
4648@ SuppressWarnings ({"UnusedReturnValue,SameParameterValue" , "unused" })
4749public class LoadingButton extends DrawableTextView {
4850 private int curStatus = STATE .IDE ; //当前的状态
51+
4952 interface STATE {
5053 int IDE = 0 ;
5154 int SHRINKING = 1 ;
@@ -134,6 +137,8 @@ private void init(Context context, AttributeSet attrs) {
134137
135138 }
136139
140+
141+
137142 /**
138143 * 设置收缩动画,主要用来收缩和恢复布局的宽度,动画开始前会保存一些收缩前的参数(文字,其他Drawable等)
139144 */
@@ -240,9 +245,6 @@ private void restoreStatus() {
240245 getLayoutParams ().width = mRootViewSizeSaved [0 ];
241246 getLayoutParams ().height = mRootViewSizeSaved [1 ];
242247 requestLayout ();
243- if (disableClickOnLoading ) {
244- super .setEnabled (true );
245- }
246248
247249 addOnLayoutChangeListener (new OnLayoutChangeListener () {
248250 @ Override
@@ -256,6 +258,21 @@ public void onLayoutChange(View v, int left, int top, int right, int bottom, int
256258 }
257259
258260
261+ /**
262+ * 如果disableClickOnLoading==true,点击会无效
263+ */
264+ @ SuppressLint ("ClickableViewAccessibility" )
265+ @ Override
266+ public boolean onTouchEvent (MotionEvent event ) {
267+ if (event .getAction () == MotionEvent .ACTION_UP ) {
268+ //disable click
269+ if (disableClickOnLoading && curStatus != STATE .IDE )
270+ return true ;
271+ }
272+ return super .onTouchEvent (event );
273+ }
274+
275+
259276 /**
260277 * 开始收缩或恢复
261278 *
@@ -348,11 +365,6 @@ private void cancelAllRunning(boolean withAnim) {
348365 * shrink -> startLoading
349366 */
350367 public void start () {
351- //disable click
352- if (disableClickOnLoading ) {
353- super .setEnabled (false );
354- }
355-
356368 //cancel last loading
357369 if (curStatus == STATE .SHRINKING || curStatus == STATE .LOADING )
358370 isCancel = true ;
0 commit comments