編輯:關於 Android 程式設計
這裡也要簡單說明一下,這些小模塊並非我原創,而是當時查資料找到的,由於時間隔了比較久,原文鏈接已經記不清了,所以就不列出引用鏈接了。不過這些程式碼我都修改、完善過,也補充了一些註解,希望對大家有幫助。
文字描邊這個功能相當實用。如果是在單一背景下顯示文字,描邊能起到裝飾作用;而在複雜背景下——尤其是不同圖片背景上顯示文字時——由於文字顏色很容易和圖片背景相近,導致文字看不清、用戶體驗不佳。如果為文字加上不同顏色的描邊,讓文字輪廓是一種顏色、內部又是另一種顏色,那麼一般情況下,圖片要麼和輪廓顏色相近,要麼和內部顏色相近,這樣無論背景多麼複雜,文字都能清晰呈現。
我這裡採用的是重寫TextView的方式。
下面是相關程式碼,整體比較簡單,容易理解。
繼承的 TextView 文字描邊類如下:
public class StrokeTextView extends TextView { private TextView outlineTextView = null; public StrokeTextView(Context context) { super(context); outlineTextView = new TextView(context); init(); } public StrokeTextView(Context context, AttributeSet attrs) { super(context, attrs); outlineTextView = new TextView(context, attrs); init(); } public StrokeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); outlineTextView = new TextView(context, attrs, defStyle); init(); } public void init() { TextPaint paint = outlineTextView.getPaint(); paint.setStrokeWidth(3);// 描邊寬度 paint.setStyle(Style.STROKE); outlineTextView.setTextColor(Color.parseColor("#45c01a"));// 描邊顏色 outlineTextView.setGravity(getGravity()); } @Override public void setLayoutParams (ViewGroup.LayoutParams params) { super.setLayoutParams(params); outlineTextView.setLayoutParams(params); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 設置輪廓文字 CharSequence outlineText = outlineTextView.getText(); if (outlineText == null || !outlineText.equals(this.getText())) { outlineTextView.setText(getText()); postInvalidate(); } outlineTextView.measure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); outlineTextView.layout(left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { outlineTextView.draw(canvas); super.onDraw(canvas); } }配置檔如下:
<com.my.teststroketextview.StrokeTextView android:id="@+id/test_stroketextview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:textSize="25sp" android:textColor="@color/dark_gray" android:text="@string/hello_world" />
呼叫程式碼如下:
private StrokeTextView test_stroketextview = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); test_stroketextview = (StrokeTextView)findViewById(R.id.test_stroketextview); test_stroketextview.setText("Hello world!"); } 如果想更改文字描邊的寬度或顏色,需要修改上面的StrokeTextView類;當然,也可以把這個類設計得更靈活一些,這樣就能動態地修改描邊寬度或顏色了。
以上就是 Android 中文字描邊功能的實作範例,希望本文對大家學習 Android 開發有所幫助,也請大家多多支持本站。
Android 使用 Notification 實作普通通知欄
Notification 是在應用程式常規介面之外,向使用者展示訊息的機制。當 App 發送通知時,系統會先在狀態列顯示圖示,使用者可下拉通知抽屜查看詳細內容。 N
Android SortableTableView 可排序表格庫詳解
Sort able TableView 是適合在 Android 上呈現類試算表格式的元件,支援欄位排序、自訂儲存格與資料監聽,很適合報表、財務或列表型應用。 加入依賴
Android 修改應用程式捷徑圖示的實作步驟
更換 Android 應用程式啟動捷徑圖示,看似簡單,但常因為快取或既有安裝版本干擾,導致修改後沒有立即反映。以下整理一套可操作的流程。 準備新的圖示資源 先將新的 ic
React Native 混合開發入門:TextInput 文本輸入組件詳解
TextInput TextInput 是允許用戶輸入文本的基礎組件,它提供了一系列屬性來幫助我們處理業務邏輯,例如 onChangeText、onSubmitEdit