Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Using HtmlHttpImageGetter, BUT loading img URL pic too small. #131

Open
jinmiao opened this issue Dec 14, 2017 · 12 comments
Open

Using HtmlHttpImageGetter, BUT loading img URL pic too small. #131

jinmiao opened this issue Dec 14, 2017 · 12 comments

Comments

@jinmiao
Copy link

jinmiao commented Dec 14, 2017

May be need Mini or Max size for UE.

Or

Add function to set BitmapDrawable scale

@gowrojisunil
Copy link

Any update on this ?. I have the same problem

@hezhipengzipp
Copy link

anyone know to dealwith it? I have the same problem

@rex3du
Copy link

rex3du commented Jan 29, 2018

Just use this function enableCompressImage of HtmlHttpImageGetter or use this constructor HtmlHttpImageGetter(TextView textView, String baseUrl, boolean matchParentWidth).

@dovahkiin98
Copy link

@rex3du The image gets cut by the parent's padding

@muziling
Copy link

muziling commented Apr 20, 2018

any update?

    private float getScale(Drawable drawable) {
        View container = containerReference.get();
        if (!matchParentWidth || container == null) {
            return 1f;  //here can be adjust to text size or custom text size????????
        }
        float maxWidth = container.getWidth();
        float originalDrawableWidth = drawable.getIntrinsicWidth();

        return maxWidth / originalDrawableWidth;
    }

@OsbornWJ
Copy link

why don't use BitMap width and height?

@Mun0n
Copy link

Mun0n commented May 29, 2018

Have the same issue, the padding cut the image sides

@sami-soft
Copy link

i am problame not slove.

where set scale image ? images are too small

@kosiara
Copy link

kosiara commented Oct 23, 2018

@sami-soft @Mun0n @hezhipengzipp
To fix your problem with images being too small - please use the following code sample

htmlTextView.setHtml(newValue, HtmlHttpImageGetter(this, null, true))

i.e. set HtmlHttpImageGetter constructor parameter matchParentWidth to true

public HtmlHttpImageGetter(TextView textView, String baseUrl, boolean matchParentWidth) {
}

@toutoumu
Copy link

改了一下用的Glide加载图片, 可以自定义图片匹配宽度, 或者自定义大小 大小单位为 dp

`
/*

  • Copyright (C) 2014-2016 Dominik Schürmann [email protected]
  • Copyright (C) 2013 Antarix Tandon
  • Licensed under the Apache License, Version 2.0 (the "License");
  • you may not use this file except in compliance with the License.
  • You may obtain a copy of the License at
  •  http://www.apache.org/licenses/LICENSE-2.0
    
  • Unless required by applicable law or agreed to in writing, software
  • distributed under the License is distributed on an "AS IS" BASIS,
  • WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  • See the License for the specific language governing permissions and
  • limitations under the License.
    */

package com.beijing.fragment.live;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Html.ImageGetter;
import android.view.View;
import android.widget.TextView;
import com.beijing.R;
import com.blankj.utilcode.util.SizeUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import java.lang.ref.WeakReference;
import timber.log.Timber;

public class HtmlHttpImageEmojiGetter implements ImageGetter {
private TextView container;
private int mSize;
private boolean matchParentWidth = false;
private boolean compressImage = false;
private int qualityImage = 50;

private final WeakReference containerReference;

public HtmlHttpImageEmojiGetter(TextView textView) {
this.container = textView;
this.mSize = SizeUtils.dp2px(20);
this.containerReference = new WeakReference<>(container);
}

public HtmlHttpImageEmojiGetter(TextView textView, int size) {
this.container = textView;
this.mSize = SizeUtils.dp2px(size);
this.containerReference = new WeakReference<>(container);
}

public HtmlHttpImageEmojiGetter(TextView textView, boolean matchParentWidth) {
this.container = textView;
this.mSize = SizeUtils.dp2px(20);
this.matchParentWidth = matchParentWidth;
this.containerReference = new WeakReference<>(container);
}

public void enableCompressImage(boolean enable) {
enableCompressImage(enable, 50);
}

public void enableCompressImage(boolean enable, int quality) {
compressImage = enable;
qualityImage = quality;
}

public Drawable getDrawable(String source) {
UrlDrawable urlDrawable = new UrlDrawable();
/Drawable drawable = container.getResources().getDrawable(R.drawable.ic_default_image);
if (drawable != null) {
drawable.setBounds(0,
0,
(int) (drawable.getIntrinsicWidth() * getScale(drawable)),
(int) (drawable.getIntrinsicHeight() * getScale(drawable)));
urlDrawable.drawable = drawable;
urlDrawable.setBounds(drawable.getBounds());
}
/

RequestOptions requestOptions =
  new RequestOptions().placeholder(R.drawable.ic_default_image).error(R.drawable.ic_default_empty);
if (compressImage) {
  requestOptions.encodeQuality(qualityImage);
}

// get the actual source
Glide.with(container).asDrawable().apply(requestOptions).load(source).into(new SimpleTarget<Drawable>() {
  @Override
  public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
    float scale = getScale(resource);
    resource.setBounds(0, 0, (int) (resource.getIntrinsicWidth() * scale), (int) (resource.getIntrinsicHeight() * scale));

    // change the reference of the current drawable to the result from the HTTP call
    urlDrawable.drawable = resource;

    // set the correct bound according to the result from HTTP call
    urlDrawable.setBounds(resource.getBounds());

    // redraw the image by invalidating the container
    container.invalidate();

    // re-set text to fix images overlapping text
    container.setText(container.getText());

    Timber.e("urlDrawable.invalidateSelf()");
  }
});

// return reference to URLDrawable which will asynchronously load the image specified in the src tag
return urlDrawable;

}

private float getScale(Bitmap bitmap) {
View container = containerReference.get();
if (container == null) {
return 1f;
}
// 不匹配宽度
if (!matchParentWidth) {
float originalDrawableWidth = bitmap.getWidth();
return mSize / originalDrawableWidth;
}

float maxWidth = container.getWidth();
float originalDrawableWidth = bitmap.getWidth();
return maxWidth / originalDrawableWidth;

}

private float getScale(Drawable drawable) {
View container = containerReference.get();
if (container == null) {
return 1f;
}

// 不匹配宽度
if (!matchParentWidth) {
  float originalDrawableWidth = drawable.getIntrinsicWidth();
  return mSize / originalDrawableWidth;
}

float maxWidth = container.getWidth();
float originalDrawableWidth = drawable.getIntrinsicWidth();
return maxWidth / originalDrawableWidth;

}

@SuppressWarnings("deprecation")
public class UrlDrawable extends BitmapDrawable {
protected Drawable drawable;

@Override
public void draw(Canvas canvas) {
  // override the draw to facilitate refresh function later
  if (drawable != null) {
    drawable.draw(canvas);
  }
}

}
}

`

@qhs107
Copy link

qhs107 commented Nov 26, 2018

When I rotate the screen, the picture gets bigger.
new HtmlHttpImageGetter(htmlTextView, null, true));//This is my code.

@qhs107
Copy link

qhs107 commented Nov 26, 2018

Sorry,It's my code that's wrong.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests