Xml布局
<org.sufficientlysecure.htmltextview.HtmlTextView
android:id="@+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:lineSpacingExtra="5dp"
android:padding="5dp"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/black" />
Java
txtDescription.setHtml(object.getString("content"), new HtmlHttpImageGetter(txtDescription));
来自服务器的响应
{"success":"true","Content":{"id":115,"title":"vvipprogram","content":"<img src=\"https://example.com//media//images//topvvip.jpg" alt=\"topvvip\" width=\"43%\"\/>\r\n"}}
结果截图
我想在整页中显示此图片?这该怎么做 ?请帮助我。
答案 0 :(得分:5)
您可以使用此代码从textview:
设置html图像和文本这是xml代码:
<TextView
android:id="@+id/txtDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:lineSpacingExtra="5dp"
android:padding="5dp"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
android:textColor="@color/black" />
这是Java代码:
public class ConditionOfUseActivity extends Activity implements Html.ImageGetter {
private TextView txtDescription;
private Drawable empty;
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_condition_use);
txtDescription = (TextView) findViewById(R.id.txtDescription);
String source = "this is a test of <b>ImageGetter</b> it contains " +
"two images: <br/>" +
"<img src=\"http://developer.android.com/assets/images/dac_logo.png\"><br/>and<br/>" +
"<img src=\"http://developer.android.com/assets/images/icon_search.png\">";
Spanned spanned = Html.fromHtml(object.getString("content"), ConditionOfUseActivity.this, null);
txtDescription.setText(spanned);
}
@Override
public Drawable getDrawable(String s) {
LevelListDrawable d = new LevelListDrawable();
empty = getResources().getDrawable(R.drawable.splash1);
d.addLevel(0, 0, empty);
d.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
new LoadImage().execute(s, d);
return d;
}
class LoadImage extends AsyncTask<Object, Void, Bitmap> {
private LevelListDrawable mDrawable;
@Override
protected Bitmap doInBackground(Object... params) {
String source = (String) params[0];
mDrawable = (LevelListDrawable) params[1];
Log.d(TAG, "doInBackground " + source);
try {
InputStream is = new URL(source).openStream();
return BitmapFactory.decodeStream(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null) {
BitmapDrawable d = new BitmapDrawable(bitmap);
mDrawable.addLevel(1, 1, d);
//mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
mDrawable.setBounds(0, 0, empty.getIntrinsicWidth(), empty.getIntrinsicHeight());
mDrawable.setLevel(1);
CharSequence t = txtDescription.getText();
txtDescription.setText(t);
}
}
}