Android ImageGetter图像重叠文本

Nic*_*ick 24 android html-parsing textview spanned

我正在尝试将一块HTML加载到TextView中,包括图像,使用

URLImageParser p = new URLImageParser(articleBody, this);
Spanned htmlSpan = Html.fromHtml(parsedString, p, null);
Run Code Online (Sandbox Code Playgroud)

顺便说一句,parsedString是HTML.无论如何,它加载,但图像没有创建任何空间供他们坐下,所以他们最终重叠他们上面的文本.这是我的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;
        } 
    }
}
Run Code Online (Sandbox Code Playgroud)

}

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

Mar*_*n S 54

您可以将您的投币器c(视图)更改为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);
} 
Run Code Online (Sandbox Code Playgroud)

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

  • `container.setText(container.getText())`也可以工作(提供的容器是TextView).但是,我想知道强制TextView重做其布局的最佳方法是... (12认同)
  • setHeight不起作用:( Eclipse告诉使用setMinimumHeight,但这没有帮助 (4认同)
  • @Kondra007你需要根据Martin的指示将cointainer c(view)更改为textView,然后eclipse不会要求你使用setMinimumHeight (2认同)

Kur*_*aum -2

您需要将其加载到文本视图中是否有特殊原因?你可以直接使用WebView来代替吗?

如果您无法使用 Webview,那么最好的解决方案是不要将图像放入文本视图中。将图像放入 ImageView 中。TextView 不具备任何您需要的布局引擎功能来确定将图像和文本相互关联的位置。它们不是ViewGroups(如LinearLayout或RelativeLayout),因此没有内部布局指定功能。如果你真的不想使用 webview(以及它拥有的所有漂亮的布局引擎),你将不得不自己弄清楚如何安排单独的 TextView 和 ImageView。

  • @Nick我认为你应该选择下面的答案。因为它是解决textview内部的问题。无需使用网络视图。Webview 不适合这个目的。而且,如果在列表视图中使用网络视图,它们可能会出现问题。 (11认同)
  • WebView 造成了不合格的体验。我正在尝试编写一个好的本机应用程序。我已经显示了我的图像,我只需要它们正确地布局即可。 (3认同)