2 回答
TA贡献1712条经验 获得超3个赞
TextView content = mView.findViewById(R.id.content);
String myHtml = "This will display an image to the right <img src='https://i-vnexpress.vnecdn.net/2019/06/06/trump-1571-1559784498.jpg' />";
content.setText(Html.fromHtml(myHtml, Images, null));
private Html.ImageGetter Images = new Html.ImageGetter() {
public Drawable getDrawable(String source) {
Drawable drawable = null;
FetchImageUrl fiu = new FetchImageUrl(getActivity(),source);
try {
fiu.execute().get();
drawable = fiu.GetImage();
}
catch (Exception e) {
drawable = getResources().getDrawable(R.drawable.default_icon);
}
// to display image,center of screen
if(drawable!=null) {
int imgH = drawable.getIntrinsicHeight();
int imgW = drawable.getIntrinsicWidth();
int padding = 20;
int realWidth = 700; //ScreenW-(2*padding);
int realHeight = imgH * realWidth / imgW;
drawable.setBounds(padding, 0, realWidth, realHeight);
}
return drawable;
}
};
public class FetchImageUrl extends AsyncTask<String, String, Boolean> {
String imageUrl;
Context context;
protected Drawable image;
public FetchImageUrl(Context context, String url)
{
this.imageUrl = url;
image = null;
this.context = context;
}
public Drawable GetImage()
{
return image;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... args) {
try {
InputStream input_stream = (InputStream) new URL(imageUrl).getContent();
image = Drawable.createFromStream(input_stream, "src name");
return true;
}
catch (Exception e)
{
image = null;
}
return false;
}
@Override
protected void onPostExecute(Boolean result) {
}}
TA贡献1835条经验 获得超7个赞
像这样使用 -
String htmlString = "<img src='ic_launcher'><i>Welcome to<i> <b><a href='http://android-coding.blogspot.com'>Android Coding</a></b>";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView htmlTextView = new TextView(this);
setContentView(htmlTextView);
htmlTextView.setText(Html.fromHtml(htmlString, new Html.ImageGetter(){
@Override
public Drawable getDrawable(String source) {
Drawable drawable;
int dourceId =
getApplicationContext()
.getResources()
.getIdentifier(source, "drawable", getPackageName());
drawable =
getApplicationContext()
.getResources()
.getDrawable(dourceId);
drawable.setBounds(
0,
0,
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
return drawable;
}
}, null));
htmlTextView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
添加回答
举报