android —— textview解析html富文本

activity 里的 

HttpUtil.sendOkHttpRequest(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String string = response.body().string();
                Gson gson = new Gson();
                final ContentJson content = gson.fromJson(string,ContentJson.class);
                final Spanned spanned = Html.fromHtml(content.getBody(),new UrlImageGetter(NewsActivity.this),null);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        newsText.setText(spanned);
                    }
                });
            }
        });

两个工具类

package com.example.zsbenweather.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;

import androidx.appcompat.widget.DrawableUtils;

import com.example.zsbenweather.gson.ContentJson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class UrlImageGetter implements Html.ImageGetter {
    Context mContext;
    public UrlImageGetter(Context context){
        mContext=context;
    }
    @Override
    public Drawable getDrawable(String source) {
        try{
            //请求获取图片
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url(source).build();
            Call call = client.newCall(request);
            Response response = call.execute();
            //加载图片
            Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
            Drawable drawable = new BitmapDrawable(bitmap);
            //调整图片大小
            DrawableUtil drawableUtil = new DrawableUtil(mContext);
            drawable = drawableUtil.utils(drawable);
            return drawable;
        }catch (IOException e){
            e.printStackTrace();
        }

        return null;
    }
}
package com.example.zsbenweather.util;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.Display;

public class DrawableUtil {
    Context mContext;
    public DrawableUtil(Context context){
        mContext = context;
    }
    public Drawable utils(Drawable drawable){
        DisplayMetrics displayMetrics = mContext.getResources().getDisplayMetrics();
        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;
        int draWidth = drawable.getIntrinsicWidth();
        int draHeight = drawable.getIntrinsicHeight();

        int resWidth = draWidth,resHeight = draHeight;
        if(draWidth<width && draHeight<height){
            resWidth = (int) (draWidth*2.5);
            resHeight = (int) (draHeight*2.5);
        }else if (draHeight > width || draHeight > height){
            int value = draWidth/width;
            resWidth = draWidth/value;
            resHeight = draHeight/value;
        }
        drawable.setBounds(0,0,resWidth,resHeight);
        return drawable;
    }
}

 

posted on 2020-03-24 22:55  zsben  阅读(1961)  评论(0编辑  收藏  举报

导航