传送门 ☞ Android兵器谱 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229
玄铁剑
此剑剑身长愈三尺,两边剑锋均为钝口,剑尖圆圆似是半球,通体深黑,却隐隐透出红光。剑以玄铁制成,共重八八六十四斤。
本节我们学习如何利用Android平台“玄铁剑”TextView来显示表情图像和文字,下面给出该情景的案例:
一、案例技术要点
1.java.lang.reflect.Field:利用Reflect相关技术获取资源文件夹下表情图片ID。
2.android.text.Html.fromHtml(...):提供包含Html标签格式的文本内容。其中的第二个参数new ImageGetter() {...}:表示该字符串内容中可以包含图片资源。
3.android.text.method.LinkMovementMethod:提供超链接功能。TextView需要此功能时引入该类的实例即可。
4.drawable.getIntrinsicWidth():获取图片的实际宽度;drawable.getIntrinsicHeight():获取图片的实际高度。
二、案例代码陈列
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.textview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".TextViewMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
strings.xml
<resources>
<string name="app_name">TextView显示表情图像和文字</string>
</resources>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#FFF" />
</LinearLayout>
TextViewMainActivity.java
package com.android.textview;
import java.lang.reflect.Field;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
/**
* TextView案例二:显示表情图像和文字
* @author lynnli1229
*/
public class TextViewMainActivity extends Activity {
private TextView textView;
public int getResourceId(String name) {
try {
// 根据资源ID的变量名获取Field对象
Field field = R.drawable.class.getField(name);
// 获取并返回资源ID的字段(静态变量)的值
return Integer.parseInt(field.get(null).toString());
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) findViewById(R.id.tv);
textView.setTextColor(Color.BLACK);
textView.setBackgroundColor(Color.WHITE);
textView.setTextSize(20);
String html = "图像1<img src='image1' />图像2<img src='image2' />图像3<img src='image3' /><p>";
html += "图像4<a href='http://www.baidu.com'><img src='image4' /></a>图像5<img src='image5' />";
CharSequence charSequence = Html.fromHtml(html, new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
// 获取系统资源信息
Drawable drawable = getResources().getDrawable(getResourceId(source));
//50%压缩处理第三个图片
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());
}
}
友情提示:提供五张表情图片(QQ表情)存放于drawable-hdpi文件夹下。
三、案例效果展示