Textview photo-text display HTML [achieve]

Ado, directly on the bayonet!

/**
     * 设置HTml网页
     *
     * @param text html字符串
     * @param view textview
     */
    private void setHtml(String text, TextView view) {
        MyImageGetter myImageGetter = new MyImageGetter(context, view);
        CharSequence sequence;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            sequence = Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY, myImageGetter, null);
        } else {
            sequence = Html.fromHtml(text);
        }
        view.setText(sequence);
    }

MyImageGetter which is a self-defined class used to display html inside the picture, the need to achieve [Html.ImageGetter] This method is html which comes, I use the glide, loaded to display images.

This is my writing

/**
 * 显示html里面的图片
 */
public class MyImageGetter implements Html.ImageGetter {
    private static final String TAG = "MyImageGetter";
    private TextView textView;
    private Context context;

    public MyImageGetter(Context context, TextView textView) {
        this.textView = textView;
        this.context = context;
    }

    @Override
    public Drawable getDrawable(final String source) {
        //在getDrawable中的source就是 img标签里src的值也就是图片的路径
        Log_Ma.e(TAG, source);
        LevelListDrawable drawable = new LevelListDrawable();//等级列表图片
        SimpleTarget<Bitmap> simpleTarget = new SimpleTarget<Bitmap>() {

            @Override
            public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                if (bitmap != null) {
                    BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
                    drawable.addLevel(1, 1, bitmapDrawable);
                    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
                    drawable.setLevel(1);
                    textView.invalidate();
                    textView.setText(textView.getText());//解决图文重叠
                }
            }

            @Override
            public void onLoadFailed(@Nullable Drawable errorDrawable) {
                super.onLoadFailed(errorDrawable);

            }
        };
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.banner_place)//占位图片
                .error(R.mipmap.banner_place)//错误图片
                .fallback(R.mipmap.banner_place);

        Glide.with(context)
                .asBitmap()
                .load(source)
                .apply(options)
//                .override(400, 400)//压缩图片
                .into(simpleTarget);
        return drawable;
    }

So that you can show pictures, and then we click on the project, there are no results to display, the first so do the

He published 198 original articles · won praise 54 · views 240 000 +

Guess you like

Origin blog.csdn.net/fengyeNom1/article/details/104001683