1717import android .widget .FrameLayout ;
1818import android .widget .ImageView ;
1919
20+ import java .lang .ref .WeakReference ;
2021import java .util .concurrent .ExecutorService ;
2122import java .util .concurrent .Executors ;
2223
3334public class ECardFlowLayout extends FrameLayout {
3435
3536 private static final int SWITCH_ANIM_TIME = 300 ;
36- private static final int MSG_JUDGE_RESET = 100 ;
37+ private static final int MSG_JUDGE_RESET = 0x1 ;
3738
3839 private Context mContext ;
3940 private ExecutorService mThreadPool ;
40- private Handler mHandler ;
41+ private MyHandler mHandler ;
4142 private NotifyRunnable mNotifyRunnable ;
4243
4344 private ImageView mBgImage ;
@@ -79,6 +80,22 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
7980 initViewPager ();
8081 }
8182
83+ private void init () {
84+ mThreadPool = Executors .newCachedThreadPool ();
85+ mNotifyRunnable = new NotifyRunnable ();
86+ mHandler = new MyHandler (this );
87+ mBlurImage = new ImageView (mContext );
88+ initImageView (mBlurImage );
89+ mBgImage = new ImageView (mContext );
90+ initImageView (mBgImage );
91+ }
92+
93+ private void initImageView (ImageView image ) {
94+ image .setLayoutParams (new ViewGroup .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
95+ image .setScaleType (ImageView .ScaleType .CENTER_CROP );
96+ addView (image );
97+ }
98+
8299 private void initViewPager () {
83100 for (int i = 0 ; i < getChildCount (); i ++) {
84101 if (getChildAt (i ) instanceof ViewPager ) {
@@ -125,31 +142,6 @@ public void onPageScrollStateChanged(int state) {
125142 });
126143 }
127144
128- private void init () {
129- mThreadPool = Executors .newCachedThreadPool ();
130- mNotifyRunnable = new NotifyRunnable ();
131- mHandler = new Handler (new Handler .Callback () {
132- @ Override
133- public boolean handleMessage (Message msg ) {
134- if (msg .arg1 == MSG_JUDGE_RESET ) {
135- judgeReset ();
136- return true ;
137- }
138- return false ;
139- }
140- });
141- mBlurImage = new ImageView (mContext );
142- initImageView (mBlurImage );
143- mBgImage = new ImageView (mContext );
144- initImageView (mBgImage );
145- }
146-
147- private void initImageView (ImageView image ) {
148- image .setLayoutParams (new ViewGroup .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .MATCH_PARENT ));
149- image .setScaleType (ImageView .ScaleType .CENTER_CROP );
150- addView (image );
151- }
152-
153145 private void updateNextRes (final int position ) {
154146 mCurPosition = position ;
155147 recycleBitmap (lastBp );
@@ -284,30 +276,12 @@ public void run() {
284276 }, mSwitchAnimTime );
285277 }
286278
287- private class NotifyRunnable implements Runnable {
288-
289- private int targetPosition ;
290- private boolean isNext ;
291-
292- @ Override
293- public void run () {
294- if (isNext ) {
295- updateNextRes (targetPosition );
296- } else {
297- updateLastRes (targetPosition );
298- }
299- }
300-
301- void setTarget (int targetPosition , boolean isNext ) {
302- this .targetPosition = targetPosition ;
303- this .isNext = isNext ;
304- }
305- }
306-
307279 private void sendMsg () {
308280 Message msg = new Message ();
309- msg .arg1 = MSG_JUDGE_RESET ;
310- mHandler .sendMessage (msg );
281+ msg .what = MSG_JUDGE_RESET ;
282+ if (mHandler != null ) {
283+ mHandler .sendMessage (msg );
284+ }
311285 }
312286
313287 private void judgeReset () {
@@ -380,6 +354,7 @@ public void onDestroy() {
380354 if (!mThreadPool .isShutdown ()) {
381355 mThreadPool .shutdown ();
382356 }
357+ mHandler = null ;
383358 recycleBitmap (curBp );
384359 recycleBitmap (lastBp );
385360 recycleBitmap (nextBp );
@@ -389,4 +364,43 @@ public void onDestroy() {
389364 recycleBitmap (nextBlurBp );
390365 }
391366 }
367+
368+ private class NotifyRunnable implements Runnable {
369+
370+ private int targetPosition ;
371+ private boolean isNext ;
372+
373+ @ Override
374+ public void run () {
375+ if (isNext ) {
376+ updateNextRes (targetPosition );
377+ } else {
378+ updateLastRes (targetPosition );
379+ }
380+ }
381+
382+ void setTarget (int targetPosition , boolean isNext ) {
383+ this .targetPosition = targetPosition ;
384+ this .isNext = isNext ;
385+ }
386+ }
387+
388+ private static class MyHandler extends Handler {
389+
390+ WeakReference <ECardFlowLayout > mLayout ;
391+
392+ MyHandler (ECardFlowLayout layout ) {
393+ mLayout = new WeakReference <>(layout );
394+ }
395+
396+ @ Override
397+ public void handleMessage (Message msg ) {
398+ ECardFlowLayout layout = mLayout .get ();
399+ switch (msg .what ) {
400+ case MSG_JUDGE_RESET :
401+ layout .judgeReset ();
402+ break ;
403+ }
404+ }
405+ }
392406}
0 commit comments