给大家先看一下效果吧:
几秒后(文字在向左跑动):
以上就是实现图片和文字混排、文字跑马灯的效果实现,接下来看一下代码如何实现吧:
MainActivity.java
public class Android_TextviewActivity extends Activity {
private TextView textView1;
private TextView textView2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*
* 实现在TextView中显示表情图片和文本
* */
textView1=(TextView)findViewById(R.id.textview1);
String html2="图像1<img src='image1'>图像2<img src='image2'>";
html2+="图像3<img src='image3'>图像4<a href='http://www.baidu.com'>
<img src='image4'></a>";
html2+="图像5<img src='image5'>";
CharSequence charSequence2=Html.fromHtml(html2, new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
// TODO Auto-generated method stub
//获得系统资源的信息,比如图片信息
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);
textView1.setText(charSequence2);
textView1.setMovementMethod(LinkMovementMethod.getInstance());
textView2=(TextView)findViewById(R.id.textview6);
String html3="诗圣杜甫his的合法ids分三个收视率凉快实得分是的发生的!";
textView2.setText(html3);
textView2.setMovementMethod(LinkMovementMethod.getInstance());
}
public int getResourceId(String name){
Field field;
try {
//根据资源ID的变量名称来获得Field的对象,使用反射机制来实现的
field = R.drawable.class.getField(name);
//取得并返回资源的id的字段(静态变量)的值,使用反射机制
return Integer.parseInt(field.get(null).toString());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return 0;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview1"
android:layout_margin="10dp"
android:background="#FFF"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview2"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:focusable="true"
android:background="#FFFFFF"
android:textColor="#000"
android:textSize="20dp"
android:layout_margin="10dp"
android:padding="10dp"
android:focusableInTouchMode="true"
android:singleLine="true"/>
</LinearLayout>