繁体   English   中英

在文本视图中显示带有img html的图像

[英]Display image with img html in textview

我想在textview中显示图像。 我在res / drawable / img.png中有一张图片,这是我的代码:

String img="<img src='file:///res/drawable/img.png' />";
txt_html.append(Html.fromHtml(img)); 

但这是行不通的。

任何想法?

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.widget.TextView;

public class TextImageActivity extends Activity {
    int imageNumber = 1; //int to check which image is displayed
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView tvText = (TextView) findViewById(R.id.text);
    final String testContent = "<html><body><b>Test</b><i>Italic</i><br/>"
        + "<img src=\"icon.png\"/>This is like testing if this thing works" + "<img src=\"a.png\"/>" +
                " in a more elaborate</body></html>";
    tvText.setText(Html.fromHtml(testContent, imgGetter, null));
}

  private ImageGetter imgGetter = new ImageGetter() {

    public Drawable getDrawable(String source) {
        Drawable drawable = null;
        if(imageNumber == 1) {
        drawable = getResources().getDrawable(R.raw.icon);
        ++imageNumber;
        } else drawable = getResources().getDrawable(R.raw.a);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable
                                .getIntrinsicHeight());

        return drawable;
    }
 };

 }

要从网上下载图像并在textView中显示,我使用了以下方法:

public class MainActivity extends ActionBarActivity {

public String htmlContent;
int ScreenW,ScreenH;
Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_news);

    this.context = getBaseContext();

    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    float dpWidth = displayMetrics.widthPixels;
    float dpHeight = displayMetrics.heightPixels;

    ScreenW = (int) dpWidth;
    ScreenH = (int) dpHeight;

    try {

        ((TextView) findViewById(R.id.tv_my_content)).setText(Html.fromHtml(htmlContent, Images, null));
    }
    catch (Exception ex)
    {

    }
}

private Html.ImageGetter Images = new Html.ImageGetter() {

    public Drawable getDrawable(String source) {

        Drawable drawable = null;

        FetchImageUrl fiu = new FetchImageUrl(context,source);
        try {
            fiu.execute().get();
            drawable = fiu.GetImage();
        }
        catch (Exception e) {
            drawable = getResources().getDrawable(R.drawable.img_news);
        }
         // to display image,center of screen
        int imgH = drawable.getIntrinsicHeight();
        int imgW = drawable.getIntrinsicWidth();
        int padding =20;
        int realWidth = ScreenW-(2*padding);
        int realHeight = imgH * realWidth/imgW;
        drawable.setBounds(padding,0,realWidth ,realHeight);
        return drawable;
    }
};
}

FetchImageUrl类是:

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) {

}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM