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

正常運(yùn)行在手機(jī)中時(shí),效果很流暢,gif上可能是由于錄制完轉(zhuǎn)碼的時(shí)候,速度調(diào)快了,所以看上去速度比較快,這都是小事情,接下來(lái)我們來(lái)看看源碼是如何實(shí)現(xiàn)的。
1.代碼很簡(jiǎn)單,主要是利用xml布局文件的幾個(gè)屬性,并且通過(guò)設(shè)置我們特定的字體就能很容易的實(shí)現(xiàn)我們看到的效果啦,首先我們創(chuàng)建一個(gè)類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);
}
}
這里我們?cè)O(shè)置了我們特定的字體樣式digital-7.ttf。
2.下面我們看看布局文件是如何寫(xiě)的
<?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è)置了以下幾個(gè)屬性
android:shadowColor="#00ff00" android:shadowDx="0" android:shadowDy="0" android:shadowRadius="10"
并且設(shè)置了指定的顏色,這樣就能實(shí)現(xiàn)LED發(fā)光字的效果。
3.下面我們?cè)賮?lái)看看MainActivity是如何實(shí)現(xiàn)的,代碼很簡(jiǎn)單,主要是獲取當(dāng)前時(shí)間,分別截取時(shí)分秒賦給我們的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);
}
}
怎么樣,代碼是不是很簡(jiǎn)單就實(shí)現(xiàn)了呢,大家趕快試一試吧!
字體資源下載地址:Android實(shí)現(xiàn)LED發(fā)光字效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程四大組件之Activity用法實(shí)例分析
這篇文章主要介紹了Android編程四大組件之Activity用法,實(shí)例分析了Activity的創(chuàng)建,生命周期,內(nèi)存管理及啟動(dòng)模式等,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-01-01
Android ListView實(shí)現(xiàn)ImageLoader圖片加載的方法
這篇文章主要介紹了Android ListView實(shí)現(xiàn)ImageLoader圖片加載的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了開(kāi)源框架Imageloader的功能、使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07
Flutter實(shí)現(xiàn)增強(qiáng)版的頁(yè)面懸浮按鈕的示例代碼
Flutter?自帶的?FloatingActionButton?為我們提供了一個(gè)懸浮在頂部的按鈕,這個(gè)按鈕始終在最頂層,因此可以做一些快捷的操作。本文就來(lái)和大家詳細(xì)聊聊2023-01-01
Android中Progress的簡(jiǎn)單實(shí)例
這篇文章主要介紹了Android中Progress的簡(jiǎn)單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android APK文件在電腦(PC虛擬機(jī))上面運(yùn)行方法
APK是Android系統(tǒng)的發(fā)布的工程包,很多時(shí)候我們想在電腦上而非Android手機(jī)上面運(yùn)行它,需要的朋友可以了解下2012-12-12
Kotlin中的密封類和密封接口及其應(yīng)用場(chǎng)景
在Kotlin中,密封類和密封接口是用于表示受限類型層次結(jié)構(gòu)的特殊類和接口。密封類和密封接口可以在一定程度上限制類型的繼承層次,使編譯器能夠更好地檢測(cè)代碼中的錯(cuò)誤,并增強(qiáng)代碼的可讀性和可維護(hù)性2023-05-05
Android開(kāi)發(fā)之開(kāi)發(fā)者頭條(二)實(shí)現(xiàn)左滑菜單
本文給大家介紹Android開(kāi)發(fā)之開(kāi)發(fā)者頭條(二)實(shí)現(xiàn)左滑菜單,主要用android自帶的DrawerLayout控件實(shí)現(xiàn)的此功能,具體實(shí)現(xiàn)過(guò)程請(qǐng)參考下本文2016-04-04
有關(guān)微博content的封裝實(shí)現(xiàn)詳解
本文將詳細(xì)介紹關(guān)于微博content的封裝實(shí)現(xiàn),需要的朋友可以參考下2012-11-11
Android 自定義精美界面包含選項(xiàng)菜單 上下文菜單及監(jiān)聽(tīng)詳解流程
這篇文章主要介紹了一個(gè)Android實(shí)例小項(xiàng)目,它包含了選項(xiàng)菜單、上下文菜單及其對(duì)應(yīng)的監(jiān)聽(tīng)事件,它很小,但這部分功能在Android開(kāi)發(fā)中很常見(jiàn),需要的朋友來(lái)看看吧2021-11-11

