Android TextView实现图文混排

TextView加载 不可否认有些抄的嫌疑 不够确实成功了

textView.setText(Html.fromHtml(html, imgGetter, null));
Html.ImageGetter imgGetter;

就这些解决了问题。

以前觉得图文混排会把图片给毁了,拉坏了,特别是用WebView掌握不好,就没谱了,现在终于解决了。哈哈哈!!!太高兴了。不说别的先复制粘贴代码,写好注释,感觉这样解决很简单。 等标签已经可以对文字和图片进行排版了

public class MainActivity extends AppCompatActivity {
    private TextView textView;
    private Html.ImageGetter imgGetter;
    private OkHttpClient httpOk;
    private String html=null;
//下边的就是Html的便签。
//    String html="TextView使用HTML斜体

"
// +"

超链接HTML入门颜色2

标题1

标题2

标题3

大于>小于<

" + // "下面是网络图片

"+""+"\"; ";
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); httpOk=new OkHttpClient(); textView=(TextView)findViewById(R.id.textView); imgGetter=new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { //这里分割了 http://localhost:8080/img/ddd.jpg String[] slipe=source.split("/"); String str=Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+slipe[slipe.length-1]; //判断是否有这个图片 Drawable drawable=Drawable.createFromPath(str); if(drawable!=null){ //如果有那么就返回图片 Log.i("tag","tag............"+"正确"); //别忘了设定图片的大小 drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); return drawable; }else { //没有就下载图片 Log.i("tag", "tag............" + "错误"); new AsynTask().execute(slipe[slipe.length-1]); } return drawable; } }; //加这句才能让里面的超链接生效 textView.setMovementMethod(LinkMovementMethod.getInstance()); final Request request = new Request.Builder() .url("http://192.168.1.105:8080/qking") .build(); new Thread(new Runnable() { @Override public void run() { try { final Response response=httpOk.newCall(request).execute(); if(response.isSuccessful()){ runOnUiThread(new Runnable() { @Override public void run() { try { //这是让Html重新加载的时候还有文字的缓存。 html=response.body().string(); textView.setText(Html.fromHtml(html, imgGetter, null)); } catch (IOException e) { e.printStackTrace(); } } }); } } catch (IOException e) { e.printStackTrace(); } } }).start(); } class AsynTask extends AsyncTask{ @Override protected Void doInBackground(String... params) { String string= Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+params[0]; Log.i("Tag",string); File file=new File(string); if(!file.isFile()){ try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } try { //请求图片 URL url=new URL("http://192.168.1.105:8080/img/"+params[0]); Bitmap bitmap= BitmapFactory.decodeStream(url.openStream()); //保存到file里边 FileOutputStream fileOutputStream=new FileOutputStream(file); //生成Png图片 bitmap.compress(Bitmap.CompressFormat.PNG,100,fileOutputStream); fileOutputStream.flush(); fileOutputStream.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); Log.i("tag", "确定吗。。。。。。。。。。。。。。。。。。。。"); //如何下载完成再访问一次。 textView.setText(Html.fromHtml(html, imgGetter, null)); } } }

你可能感兴趣的:(android,android,textview)