1. 程式人生 > >【圖片處理】Glide框架一行程式碼實現居中裁剪圖片

【圖片處理】Glide框架一行程式碼實現居中裁剪圖片

子執行緒裡面裁剪圖片(540px*540px),獲取到Bitmap

//glide框架居中裁剪圖片 540*540px(該方法是耗時操作)
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        bitmap =   Glide.with(getActivity()).load(realRoomImageUrl).asBitmap().centerCrop().into(540,540).get();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (ExecutionException e) {
                        e.printStackTrace();
                    }
                    mHandler.sendEmptyMessage(100);
                }
            }).start();

handler裡面顯示
 private static class MyHandler extends Handler {
            private WeakReference<RoomTestBeforeFragment> activityWeakReference;

            public MyHandler(RoomTestBeforeFragment fragment) {
                activityWeakReference = new WeakReference<RoomTestBeforeFragment>(fragment);
            }

            @Override
            public void handleMessage(Message msg) {
                RoomTestBeforeFragment fragment = activityWeakReference.get();
                if (fragment != null) {
                    switch (msg.what) {
                        case 100:
                            fragment.ivShow.setImageBitmap(fragment.bitmap);
                            break;
                        default:
                            break;

                    }
                }
            }
        }