Android實(shí)現(xiàn)LED發(fā)光字效果
大家好,這一篇博客來教大家一個類似于LED鬧鐘顯示屏樣式的小案例,UI比較美觀,文末會提供下載相關(guān)資源地址供大家下載,首先我們來看一看這個案例的運(yùn)行效果。

正常運(yùn)行在手機(jī)中時,效果很流暢,gif上可能是由于錄制完轉(zhuǎn)碼的時候,速度調(diào)快了,所以看上去速度比較快,這都是小事情,接下來我們來看看源碼是如何實(shí)現(xiàn)的。
1.代碼很簡單,主要是利用xml布局文件的幾個屬性,并且通過設(shè)置我們特定的字體就能很容易的實(shí)現(xiàn)我們看到的效果啦,首先我們創(chuàng)建一個類LedTextView繼承自TextView。
public class LedTextView extends TextView {
private static final String FONTS_FOLDER = "fonts";
private static final String FONT_DIGITAL_7 = FONTS_FOLDER
+ File.separator + "digital-7.ttf";
public LedTextView(Context context) {
super(context);
init(context);
}
public LedTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LedTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
AssetManager assets = context.getAssets();
final Typeface font = Typeface.createFromAsset(assets,
FONT_DIGITAL_7);
setTypeface(font);
}
}
這里我們設(shè)置了我們特定的字體樣式digital-7.ttf。
2.下面我們看看布局文件是如何寫的
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:background="#000000" android:layout_height="match_parent"> <com.eloancn.ledtextview.LedTextView android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="88:88:88" android:textColor="#3300ff00" android:textSize="80sp" /> <com.eloancn.ledtextview.LedTextView android:layout_centerInParent="true" android:id="@+id/main_clock_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:shadowColor="#00ff00" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="10" android:textColor="#00ff00" android:textSize="80sp" /> </RelativeLayout>
可以看到,我們主要是在上面一層的TextView控件上設(shè)置了以下幾個屬性
android:shadowColor="#00ff00" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="10"
并且設(shè)置了指定的顏色,這樣就能實(shí)現(xiàn)LED發(fā)光字的效果。
3.下面我們再來看看MainActivity是如何實(shí)現(xiàn)的,代碼很簡單,主要是獲取當(dāng)前時間,分別截取時分秒賦給我們的textView。
public class MainActivity extends AppCompatActivity {
private static final String DATE_FORMAT = "%02d:%02d:%02d";
private static final int REFRESH_DELAY = 500;
private final Handler mHandler = new Handler();
private final Runnable mTimeRefresher = new Runnable() {
@Override
public void run() {
final Date d = new Date();
mTextView.setText(String.format(DATE_FORMAT, d.getHours(),
d.getMinutes(), d.getSeconds()));
mHandler.postDelayed(this, REFRESH_DELAY);
}
};
private TextView mTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.main_clock_time);
}
@Override
protected void onResume() {
super.onResume();
mHandler.post(mTimeRefresher);
}
@Override
protected void onStop() {
super.onStop();
mHandler.removeCallbacks(mTimeRefresher);
}
}
怎么樣,代碼是不是很簡單就實(shí)現(xiàn)了呢,大家趕快試一試吧!
字體資源下載地址:Android實(shí)現(xiàn)LED發(fā)光字效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Bitmap的加載優(yōu)化與Cache相關(guān)介紹
這篇文章主要介紹了Android中性能優(yōu)化之Bitmap的加載優(yōu)化與Cache相關(guān)內(nèi)容介紹,文中介紹的很詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-02-02
SpringBoot實(shí)現(xiàn)短信驗(yàn)證碼登錄功能(案例)
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)短信驗(yàn)證碼登錄功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-08-08
Android studio實(shí)現(xiàn)PopupWindow彈出框效果
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)PopupWindow彈出框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條
這篇文章主要為大家詳細(xì)介紹了Android中WebView加載網(wǎng)頁設(shè)置進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
Android實(shí)現(xiàn)消息提醒小紅點(diǎn)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)消息提醒小紅點(diǎn)效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
android實(shí)現(xiàn)給未簽名的apk簽名方法
下面小編就為大家?guī)硪黄猘ndroid實(shí)現(xiàn)給未簽名的apk簽名方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
一文搞懂Android RecyclerView點(diǎn)擊展開、折疊效果的實(shí)現(xiàn)代碼
雖然在日常開發(fā)中已經(jīng)多次接觸過RecycleView,但也只是用到其最基本的功能,并沒有深入研究其他內(nèi)容。接下來將抽出時間去了解RecycleView的相關(guān)內(nèi)容,這篇文章主要是介紹Android RecyclerView點(diǎn)擊展開、折疊效果的實(shí)現(xiàn)方式,一起看看吧2021-06-06
Android編程讀取Assets所有文件(遍歷每一個文件夾)并存入sdcard的方法
這篇文章主要介紹了Android編程讀取Assets所有文件(遍歷每一個文件夾)并存入sdcard的方法,涉及Android針對文件與目錄的遍歷及I/O操作相關(guān)技巧,需要的朋友可以參考下2016-02-02

