Show Image in TextView

前端 未结 3 1534
花落未央
花落未央 2021-02-09 20:14

I wish to show Images in TextView. My Images are saved in res/raw directory. I tried using HTML.ImageGetter but could not find a complete reference for same.

3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-09 20:21

    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 = "TestItalic
    " + "This is like testing if this thing works" + "" + " in a more elaborate"; 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; } }; }

提交回复
热议问题