Skip to content

Commit 5f92bb5

Browse files
committed
修正评论区图片大小
1 parent a891a21 commit 5f92bb5

1 file changed

Lines changed: 38 additions & 30 deletions

File tree

diycode-app/src/main/java/com/gcssloop/diycode/base/glide/GlideImageGetter.java

100644100755
Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*
16-
* Last modified 2017-03-11 22:23:57
16+
* Last modified 2017-03-31 14:20:42
1717
*
1818
* GitHub: https://github.com/GcsSloop
1919
* Website: http://www.gcssloop.com
@@ -36,6 +36,7 @@
3636
import com.bumptech.glide.request.animation.GlideAnimation;
3737
import com.bumptech.glide.request.target.ViewTarget;
3838
import com.gcssloop.diycode.R;
39+
import com.gcssloop.diycode_sdk.log.Logger;
3940

4041
import java.util.HashSet;
4142
import java.util.Set;
@@ -51,15 +52,6 @@ public final class GlideImageGetter implements Html.ImageGetter, Drawable.Callba
5152

5253
private final Set<ImageGetterViewTarget> mTargets;
5354

54-
public GlideImageGetter(Context context, TextView textView) {
55-
this.mContext = context;
56-
this.mTextView = textView;
57-
58-
clear(); // Cancel all previous request
59-
mTargets = new HashSet<>();
60-
mTextView.setTag(R.id.drawable_callback_tag, this);
61-
}
62-
6355
public static GlideImageGetter get(View view) {
6456
return (GlideImageGetter) view.getTag(R.id.drawable_callback_tag);
6557
}
@@ -69,22 +61,32 @@ public void clear() {
6961
if (prev == null) return;
7062

7163
for (ImageGetterViewTarget target : prev.mTargets) {
72-
System.out.println("Cleared!");
7364
Glide.clear(target);
7465
}
7566
}
7667

68+
public GlideImageGetter(Context context, TextView textView) {
69+
this.mContext = context;
70+
this.mTextView = textView;
71+
72+
clear();
73+
mTargets = new HashSet<>();
74+
mTextView.setTag(R.id.drawable_callback_tag, this);
75+
}
76+
7777
@Override
7878
public Drawable getDrawable(String url) {
7979
final UrlDrawable urlDrawable = new UrlDrawable();
8080

81-
// System.out.println("Downloading from: " + url);
81+
Logger.i("Downloading from: " + url);
8282
Glide.with(mContext)
8383
.load(url)
8484
.diskCacheStrategy(DiskCacheStrategy.ALL)
8585
.into(new ImageGetterViewTarget(mTextView, urlDrawable));
8686

87+
8788
return urlDrawable;
89+
8890
}
8991

9092
@Override
@@ -105,40 +107,44 @@ public void unscheduleDrawable(Drawable who, Runnable what) {
105107
private class ImageGetterViewTarget extends ViewTarget<TextView, GlideDrawable> {
106108

107109
private final UrlDrawable mDrawable;
108-
private Request request;
109110

110111
private ImageGetterViewTarget(TextView view, UrlDrawable drawable) {
111112
super(view);
112-
mTargets.add(this); // Add ViewTarget into Set
113+
mTargets.add(this);
113114
this.mDrawable = drawable;
114115
}
115116

116117
@Override
117118
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
118-
// Resize images - Scale image proportionally to fit TextView width
119-
float width;
120-
float height;
121-
if (resource.getIntrinsicWidth() >= getView().getWidth()) {
122-
float downScale = (float) resource.getIntrinsicWidth() / getView().getWidth();
123-
width = (float) resource.getIntrinsicWidth() / (float) downScale;
124-
height = (float) resource.getIntrinsicHeight() / (float) downScale;
119+
Rect rect;
120+
if (resource.getIntrinsicWidth() > 100) {
121+
float width;
122+
float height;
123+
Logger.i("Image width is " + resource.getIntrinsicWidth());
124+
Logger.i("View width is " + view.getWidth());
125+
if (resource.getIntrinsicWidth() >= getView().getWidth()) {
126+
float downScale = (float) resource.getIntrinsicWidth() / getView().getWidth();
127+
width = (float) resource.getIntrinsicWidth() / (float) downScale;
128+
height = (float) resource.getIntrinsicHeight() / (float) downScale;
129+
} else {
130+
float multiplier = (float) getView().getWidth() / resource.getIntrinsicWidth();
131+
width = (float) resource.getIntrinsicWidth() * (float) multiplier;
132+
height = (float) resource.getIntrinsicHeight() * (float) multiplier;
133+
}
134+
Logger.i("New Image width is " + width);
135+
136+
137+
rect = new Rect(0, 0, Math.round(width), Math.round(height));
125138
} else {
126-
float multiplier = (float) getView().getWidth() / resource.getIntrinsicWidth();
127-
width = (float) resource.getIntrinsicWidth() * (float) multiplier;
128-
height = (float) resource.getIntrinsicHeight() * (float) multiplier;
139+
rect = new Rect(0, 0, resource.getIntrinsicWidth() * 2, resource.getIntrinsicHeight() * 2);
129140
}
130-
Rect rect = new Rect(0, 0, Math.round(width), Math.round(height));
131-
132141
resource.setBounds(rect);
133142

134143
mDrawable.setBounds(rect);
135144
mDrawable.setDrawable(resource);
136145

137-
if (resource.isAnimated()) {
138-
// set callback to drawable in order to
139-
// signal its container to be redrawn
140-
// to show the animated GIF
141146

147+
if (resource.isAnimated()) {
142148
mDrawable.setCallback(get(getView()));
143149
resource.setLoopCount(GlideDrawable.LOOP_FOREVER);
144150
resource.start();
@@ -148,6 +154,8 @@ public void onResourceReady(GlideDrawable resource, GlideAnimation<? super Glide
148154
getView().invalidate();
149155
}
150156

157+
private Request request;
158+
151159
@Override
152160
public Request getRequest() {
153161
return request;

0 commit comments

Comments
 (0)