mobile/android/thirdparty/com/squareup/picasso/DeferredRequestCreator.java

branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
equal deleted inserted replaced
-1:000000000000 0:a2e4504b2c1c
1 /*
2 * Copyright (C) 2013 Square, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.squareup.picasso;
17
18 import android.view.ViewTreeObserver;
19 import android.widget.ImageView;
20 import java.lang.ref.WeakReference;
21
22 class DeferredRequestCreator implements ViewTreeObserver.OnPreDrawListener {
23
24 final RequestCreator creator;
25 final WeakReference<ImageView> target;
26 Callback callback;
27
28 DeferredRequestCreator(RequestCreator creator, ImageView target, Callback callback) {
29 this.creator = creator;
30 this.target = new WeakReference<ImageView>(target);
31 this.callback = callback;
32 target.getViewTreeObserver().addOnPreDrawListener(this);
33 }
34
35 @Override public boolean onPreDraw() {
36 ImageView target = this.target.get();
37 if (target == null) {
38 return true;
39 }
40 ViewTreeObserver vto = target.getViewTreeObserver();
41 if (!vto.isAlive()) {
42 return true;
43 }
44
45 int width = target.getMeasuredWidth();
46 int height = target.getMeasuredHeight();
47
48 if (width <= 0 || height <= 0) {
49 return true;
50 }
51
52 vto.removeOnPreDrawListener(this);
53
54 this.creator.unfit().resize(width, height).into(target, callback);
55 return true;
56 }
57
58 void cancel() {
59 callback = null;
60 ImageView target = this.target.get();
61 if (target == null) {
62 return;
63 }
64 ViewTreeObserver vto = target.getViewTreeObserver();
65 if (!vto.isAlive()) {
66 return;
67 }
68 vto.removeOnPreDrawListener(this);
69 }
70 }

mercurial