public class MainActivity extends Activity {
public int getResourceId(String name){
try{
//根据资源的ID的变量名获得Field的对象,使用反射机制来实现的
Field field = R.drawable.class.getField(name);
//取得并返回资源的ID的字段(静态变量)的值,使用反射机制
System.out.println(Integer.parseInt(field.get(null).toString()));
return Integer.parseInt(field.get(null).toString());
} catch (Exception e){
}
return 0;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView)this.findViewById(R.id.textview);
textView.setTextColor(Color.BLACK);
textView.setBackgroundColor(Color.WHITE);
textView.setTextSize(20);
String html = "图像1<img src='image1'/>图像2<img src='image2'/><br/>";
html += "图像3<img src='image3'/>图像4<a href='http://www.baidu.com'><img src='image4'/></a>";
CharSequence charSequence = Html.fromHtml(html,new ImageGetter(){
@Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
//获得系统资源的信息,比如图片信息
Drawable drawable = getResources().getDrawable(getResourceId(source));
if(source.equals("image3")){
drawable.setBounds(0,0,drawable.getIntrinsicWidth() / 2 ,
drawable.getIntrinsicHeight() / 2);
}else{
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
}
return drawable;
}
},null);
textView.setText(charSequence);
textView.setMovementMethod(LinkMovementMethod.getInstance());
public int getResourceId(String name){
try{
//根据资源的ID的变量名获得Field的对象,使用反射机制来实现的
Field field = R.drawable.class.getField(name);
//取得并返回资源的ID的字段(静态变量)的值,使用反射机制
System.out.println(Integer.parseInt(field.get(null).toString()));
return Integer.parseInt(field.get(null).toString());
} catch (Exception e){
}
return 0;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView)this.findViewById(R.id.textview);
textView.setTextColor(Color.BLACK);
textView.setBackgroundColor(Color.WHITE);
textView.setTextSize(20);
String html = "图像1<img src='image1'/>图像2<img src='image2'/><br/>";
html += "图像3<img src='image3'/>图像4<a href='http://www.baidu.com'><img src='image4'/></a>";
CharSequence charSequence = Html.fromHtml(html,new ImageGetter(){
@Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
//获得系统资源的信息,比如图片信息
Drawable drawable = getResources().getDrawable(getResourceId(source));
if(source.equals("image3")){
drawable.setBounds(0,0,drawable.getIntrinsicWidth() / 2 ,
drawable.getIntrinsicHeight() / 2);
}else{
drawable.setBounds(0,0,drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
}
return drawable;
}
},null);
textView.setText(charSequence);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
}
效果图: