在TextView中显示图像

Gau*_*wal 6 android textview

我希望在TextView中显示图像.我的图像保存在res/raw目录中.我尝试使用HTML.ImageGetter,但找不到相同的完整参考.

Gau*_*wal 14

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;
    }
 };

 }
Run Code Online (Sandbox Code Playgroud)