Skip to content

Instantly share code, notes, and snippets.

@thaiall
Created August 7, 2017 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thaiall/bcd1c4a61fd16bf1cfb5dd32fc274d93 to your computer and use it in GitHub Desktop.
Save thaiall/bcd1c4a61fd16bf1cfb5dd32fc274d93 to your computer and use it in GitHub Desktop.
Android Studio : แสดงการใช้ ImageGetter กับ HTML Tag ที่อยู่ภายใน MainActivity.java เอง
package com.thaiall.www.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Html;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.content.res.Resources;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private class ImageGetter implements Html.ImageGetter {
public Drawable getDrawable(String source) {
int id;
id = getResources().getIdentifier(source, "drawable", getPackageName());
if (id == 0) {
// the drawable resource wasn't found in our package, maybe it is a stock android drawable?
id = getResources().getIdentifier(source, "drawable", "com.thaiall.www.myapplication");
}
if (id == 0) {
// prevent a crash if the resource still can't be found
return null;
}
else {
Drawable d;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
d = getResources().getDrawable(id, getApplicationContext().getTheme());
} else {
d = getResources().getDrawable(id);
}
//d.setBounds(0,0,d.getIntrinsicWidth(),d.getIntrinsicHeight());
d.setBounds(0,0,48 * 2,64 * 2);
return d;
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
String mData = "<b>hello</b> <img src=\"lp01\" />";
TextView htmlTextView = (TextView)findViewById(R.id.textView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
htmlTextView.setText(Html.fromHtml(mData, Html.FROM_HTML_MODE_LEGACY, new ImageGetter(), null)); // for 24 api and more
} else {
htmlTextView.setText(Html.fromHtml(mData, new ImageGetter(),null)); // or for older api
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment