本文參加第二屆Google大學生暑期博客分享大賽
TextView是最經常使用的Android控件之一,顯示文字時的首選,然而不少人都不知足於,TextView默認的顯示效果,
html
但願加入諸如跑馬燈,文字陰影的效果,下面我就來介紹下Android中文字跑馬燈和文字陰影的簡單實現方法。
java
一,文字跑馬燈
android
一樣的,先上效果圖,
web
實現起來很是簡單,TextView中已經提供了多種顯示接口,能夠在文字顯示不下時,以各類方式進行顯示
ide
例如
函數
前置省略號
後置省略號字體
主要的區別在Xml中android:ellipsize 屬性的不一樣
動畫
android:ellipsize
spa
設置當文字過長時,該控件該如何顯示。有以下值設置:」start」—–省略號顯示在開頭;」end」——省略號顯示在結尾;」middle」—-省略號顯示在中間;」marquee」 ——以跑馬燈的方式顯示(動畫橫向移動)
code
由此只須要在Xml文件中設置TextView的ellipsize屬性爲marquee便可
如
<com.widget.ScrollForeverTextView
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="28dip" android:singleLine="true"
android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever"
android:textColor="#ffd0eeee" android:text="超出七個字會有跑馬燈效果"
android:focusable="true" android:id="@+id/channel">
</com.widget.ScrollForeverTextView>
這裏你必定發現了,筆者自定義了一個TextView對他進行了顯示。緣由是TextView只會在當得到焦點時纔對文字顯示效果進行相應,咱們要實現一直顯示跑馬燈,須要對其進行自定義,方法很簡單,覆蓋TextView的isFocused()函數,讓它一直返回true就好了
import android.content.Context; import android.util.AttributeSet; import android.widget.TextView; /** * 單行文本跑馬燈控件 * * @author admin * */ public class ScrollForeverTextView extends TextView { public ScrollForeverTextView(Context context) { super(context); // TODO Auto-generated constructor stub } public ScrollForeverTextView(Context context, AttributeSet attrs) { super(context, attrs); } public ScrollForeverTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean isFocused() { return true; } }而後再將這個自定義的TextView顯示屏幕上就大功告成了。
android:shadowColor
指定文本陰影的顏色,須要與shadowRadius一塊兒使用。
android:shadowDx
設置陰影橫向座標開始位置。
android:shadowDy
設置陰影縱向座標開始位置。
android:shadowRadius
設置陰影的半徑。設置爲0.1就變成字體的顏色了,通常設置爲3.0的效果比較好。
如此,將這個TextView顯示出來,漂亮的帶有陰影的字體就出現了。 下一期我來說解Android中各類讓人抓狂的詭異Bug