zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    Vue请求参数转换(qs的使用---对象序列化)
    Vue中使用async/await解决异步请求问题
    数据结构:set
    数据结构:map (不是数组的map方法)
    数组方法-reduce 和 ES6扩展运算符
    数据分析相关
    Hadoop委任和解除节点
    Oozie调度Sqoop报错
    Oozie调度Sqoop的两种方式
    MySql数据表直接到Hive表操作
  • 原文地址:https://www.cnblogs.com/zsben991126/p/12562878.html
Copyright © 2011-2022 走看看