Android ImageGetter图像重叠文字

我试图加载一个HTML块到一个TextView,包括图像,使用

URLImageParser p = new URLImageParser(articleBody, this); Spanned htmlSpan = Html.fromHtml(parsedString, p, null); 

parsedString是HTML,顺便说一下。 无论如何,它会加载,但图像没有创build任何空间让他们坐下来,所以他们最终重叠上面的文字。 这是我的URLImageParser文件:

 public class URLImageParser implements Html.ImageGetter { Context c; View container; /*** * Construct the URLImageParser which will execute AsyncTask and refresh the container * @param t * @param c */ public URLImageParser(View t, Context c) { this.c = c; this.container = t; } public Drawable getDrawable(String source) { URLDrawable urlDrawable = new URLDrawable(); // get the actual source ImageGetterAsyncTask asyncTask = new ImageGetterAsyncTask( urlDrawable); asyncTask.execute(source); // return reference to URLDrawable where I will change with actual image from // the src tag return urlDrawable; } public class ImageGetterAsyncTask extends AsyncTask<String, Void, Drawable> { URLDrawable urlDrawable; public ImageGetterAsyncTask(URLDrawable d) { this.urlDrawable = d; } @Override protected Drawable doInBackground(String... params) { String source = params[0]; return fetchDrawable(source); } @Override protected void onPostExecute(Drawable result) { // set the correct bound according to the result from HTTP call Log.d("height",""+result.getIntrinsicHeight()); Log.d("width",""+result.getIntrinsicWidth()); urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 0+result.getIntrinsicHeight()); // change the reference of the current drawable to the result // from the HTTP call urlDrawable.drawable = result; // redraw the image by invalidating the container URLImageParser.this.container.invalidate(); } /*** * Get the Drawable from URL * @param urlString * @return */ public Drawable fetchDrawable(String urlString) { try { URL aURL = new URL(urlString); final URLConnection conn = aURL.openConnection(); conn.connect(); final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream()); final Bitmap bm = BitmapFactory.decodeStream(bis); Drawable drawable = new BitmapDrawable(bm); drawable.setBounds(0,0,bm.getWidth(),bm.getHeight()); return drawable; } catch (Exception e) { return null; } } } 

}

有任何想法吗? 万分感谢。

Solutions Collecting From Web of "Android ImageGetter图像重叠文字"

是否有一个特殊的原因,你需要加载到文本视图? 你可以使用WebView吗?

如果你不能使用Webviews,那么最好的解决办法是不要把图像放在你的文本视图中。 把图像放在ImageView中。 TextViews没有任何布局引擎的function,你需要弄清楚在哪里把图像和文本相互关联。 他们不是ViewGroups(如LinearLayout或RelativeLayout),因此没有内部布局指定function。 如果你真的不想使用webview(以及所有的布局引擎),你将不得不弄清楚如何自己安排个别的TextViews和ImageViews。

您可以将您的cointainer c(view)更改为textView,然后使onPostExecute如下所示:

 @Override protected void onPostExecute(Drawable result) { // set the correct bound according to the result from HTTP call Log.d("height",""+result.getIntrinsicHeight()); Log.d("width",""+result.getIntrinsicWidth()); urlDrawable.setBounds(0, 0, 0+result.getIntrinsicWidth(), 0+result.getIntrinsicHeight()); // change the reference of the current drawable to the result // from the HTTP call urlDrawable.drawable = result; // redraw the image by invalidating the container URLImageParser.this.container.invalidate(); // For ICS URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() + result.getIntrinsicHeight())); // Pre ICS URLImageParser.this.textView.setEllipsize(null); } 

这将首先绘制图像,然后立即将TextView的高度设置为绘图的高度+ TextView的高度

我没有足够的声望投票给Martin S,但他的回答是非常有用的。如果TextView在加载之前显示了默认图像,我们可以像这样更改setHeight()方法:

  URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight() + result.getIntrinsicHeight()-mDefaultDrawable.getInstrinsicHeight())); 

可能是我们不需要从View到TextView的容器。

  @Override protected void onPostExecute(Drawable result) { // set the correct bound according to the result from HTTP call urlDrawable.setBounds(0, 0, 0 + result.getIntrinsicWidth(), 0 + result.getIntrinsicHeight()); // change the reference of the current drawable to the result // from the HTTP call urlDrawable.drawable = result; // redraw the image by invalidating the container URLImageParser.this.container.setMinimumHeight((URLImageParser.this.container.getHeight()+ result.getIntrinsicHeight())); URLImageParser.this.container.requestLayout(); URLImageParser.this.container.invalidate(); } 

我发现这些解决scheme有一个有趣的行为:如果图像加载速度快,textview还没有渲染(例如,我用caching,所以第二次调用是相当快的Okhttp),文本视图大小为0。

为了解决这个问题,我把ImageGetter从AsyncTask转换回来,而是启动一个AsyncTask,为我的TextView创buildSpanned,然后设置文本。

有了这个解决scheme,每次载入图像时都不需要调整TextView的大小。

 new AsyncTask<TextView, Void, Spanned>() { TextView tv; @Override protected Spanned doInBackground(TextView... params) { tv = params[0]; return Html.fromHtml(feedEntry.getContent(), new HttpImageGetter(getActivity(), HttpLoaderImpl.getInstance(getActivity())), new Html.TagHandler() { @Override public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) { //do nothing... } }); } @Override protected void onPostExecute(final Spanned result) { new Handler(Looper.getMainLooper()).post(new Runnable() { @Override public void run() { tv.setText(result); } }); } }.execute(textView);