html-textview icon indicating copy to clipboard operation
html-textview copied to clipboard

Using HtmlHttpImageGetter, BUT loading img URL pic too small.

Open jinmiao opened this issue 8 years ago • 12 comments

May be need Mini or Max size for UE.

Or

Add function to set BitmapDrawable scale

jinmiao avatar Dec 14 '17 12:12 jinmiao

Any update on this ?. I have the same problem

gowrojisunil avatar Dec 30 '17 06:12 gowrojisunil

anyone know to dealwith it? I have the same problem

hezhipengzipp avatar Jan 13 '18 10:01 hezhipengzipp

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

rex3du avatar Jan 29 '18 05:01 rex3du

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

dovahkiin98 avatar Apr 17 '18 10:04 dovahkiin98

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;
    }

muziling avatar Apr 20 '18 17:04 muziling

why don't use BitMap width and height?

OsbornWJ avatar May 22 '18 06:05 OsbornWJ

Have the same issue, the padding cut the image sides

Mun0n avatar May 29 '18 11:05 Mun0n

i am problame not slove.

where set scale image ? images are too small

sami-soft avatar Oct 03 '18 18:10 sami-soft

@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) {
}

kosiara avatar Oct 23 '18 12:10 kosiara

改了一下用的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<TextView> 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);
  }
}

} }

`

toutoumu avatar Nov 23 '18 01:11 toutoumu

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

qhs107 avatar Nov 26 '18 06:11 qhs107

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

qhs107 avatar Nov 26 '18 07:11 qhs107