1、URLImageGetter类
import java.net.URL;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.text.Html.ImageGetter;
import android.view.Display;
import android.widget.TextView;
public class URLImageGetter implements ImageGetter {
TextView textView;
Context context;
public URLImageGetter(Context contxt, TextView textView) {
this.context = contxt;
this.textView = textView;
}
@Override
public Drawable getDrawable(String paramString) {
URLDrawable urlDrawable = new URLDrawable(context);
ImageGetterAsyncTask getterTask = new ImageGetterAsyncTask(urlDrawable);
getterTask.execute(paramString);
return urlDrawable;
}
public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> {
URLDrawable urlDrawable;
public ImageGetterAsyncTask(URLDrawable drawable) {
this.urlDrawable = drawable;
}
@Override
protected void onPostExecute(Drawable result) {
if (result != null) {
urlDrawable.drawable = result;
URLImageGetter.this.textView.requestLayout();
}
}
@Override
protected Drawable doInBackground(String... params) {
String source = params[0];
return fetchDrawable(source);
}
public Drawable fetchDrawable(String url) {
Drawable drawable = null;
URL Url;
try {
Url = new URL(url);
drawable = Drawable.createFromStream(Url.openStream(), "");
} catch (Exception e) {
return null;
}
// 按比例缩放图片
Rect bounds = getDefaultImageBounds(context);
int newwidth = bounds.width();
int newheight = bounds.height();
double factor = 1;
double fx = (double) drawable.getIntrinsicWidth()
/ (double) newwidth;
double fy = (double) drawable.getIntrinsicHeight()
/ (double) newheight;
factor = fx > fy ? fx : fy;
if (factor < 1)
factor = 1;
newwidth = (int) (drawable.getIntrinsicWidth() / factor);
newheight = (int) (drawable.getIntrinsicHeight() / factor);
drawable.setBounds(0, 0, newwidth, newheight);
return drawable;
}
}
// 预定图片宽高比例为 4:3
@SuppressWarnings("deprecation")
public Rect getDefaultImageBounds(Context context) {
Display display = ((Activity) context).getWindowManager()
.getDefaultDisplay();
int width = display.getWidth();
int height = (int) (width * 3 / 4);
Rect bounds = new Rect(0, 0, width, height);
return bounds;
}
}
2、URLDrawable类
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Display;
public class URLDrawable extends BitmapDrawable {
protected Drawable drawable;
@SuppressWarnings("deprecation")
public URLDrawable(Context context) {
this.setBounds(getDefaultImageBounds(context));
drawable = context.getResources().getDrawable(R.drawable.ic_launcher);
drawable.setBounds(getDefaultImageBounds(context));
}
@Override
public void draw(Canvas canvas) {
if (drawable != null) {
drawable.draw(canvas);
}
}
@SuppressWarnings("deprecation")
public Rect getDefaultImageBounds(Context context) {
Display display = ((Activity) context).getWindowManager()
.getDefaultDisplay();
int width = display.getWidth();
int height = (int) (width * 3 / 4);
Rect bounds = new Rect(0, 0, width, height);
return bounds;
}
}
3、主线程中调用方式
TextView content = (TextView)rView.findViewById(R.id.review_content);
content.setMovementMethod(LinkMovementMethod.getInstance());//加这句才能让里面的超链接生效
URLImageGetter ReviewImgGetter = new URLImageGetter(MainActivity.this, content);//实例化URLImageGetter类
content.setText(Html.fromHtml("包含有图片信息的html内容",ReviewImgGetter,null));
这样得出的图片和文字不会重叠,但是当图片尺寸小的时候,显示出来的图片周边空旷区域比较大,希望有更好的解决办法。