編輯:關於 Android 程式設計

先前看到有人的做法是使用兩個 TextView 疊放來模擬 LED 效果,於是便想用自定義控制項的方式封裝成一個可直接複用的 LEDView。
採組合控制項(Composite Control)的做法:把兩個 TextView 上下疊放,一個顯示時間、一個作為未點亮的底色,再搭配 digital-7.ttf 這款七段顯示器風格字型,就能得到 LED 的視覺效果。上層字色為亮綠色並加上外光暈,下層則用低透明度的綠色,營造「未亮的段」的錯覺。
public class LEDView extends LinearLayout {
private TextView timeView;
private TextView bgView;
private static final String FONT_DIGITAL_7 = "fonts" + File.separator + "digital-7.ttf";
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() {
Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
calendar.setTime(new Date());
timeView.setText(String.format(DATE_FORMAT,
calendar.get(Calendar.HOUR),
calendar.get(Calendar.MINUTE),
calendar.get(Calendar.SECOND)));
mHandler.postDelayed(this, REFRESH_DELAY);
}
};
public LEDView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public LEDView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public LEDView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.ledview, this);
timeView = view.findViewById(R.id.ledview_clock_time);
bgView = view.findViewById(R.id.ledview_clock_bg);
Typeface font = Typeface.createFromAsset(context.getAssets(), FONT_DIGITAL_7);
timeView.setTypeface(font);
bgView.setTypeface(font);
}
public void start() {
mHandler.post(mTimeRefresher);
}
public void stop() {
mHandler.removeCallbacks(mTimeRefresher);
}
}
<RelativeLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/ledview_clock_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#00ff00"
android:shadowColor="#00ff00"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="10"
android:textSize="80sp" />
<TextView
android:id="@+id/ledview_clock_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/default_time"
android:textColor="#3300ff00"
android:textSize="80sp" />
</RelativeLayout>
兩個 TextView 都置中重疊:下層是半透明綠字(透明度 0x33),上層是全亮綠字加上 shadowRadius 製造光暈。兩者字型、字級完全一致才能精準對齊。
public class LEDActivity extends Activity {
private LEDView ledView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_led);
ledView = findViewById(R.id.ledview);
}
@Override
protected void onResume() {
super.onResume();
ledView.start();
}
@Override
protected void onStop() {
super.onStop();
ledView.stop();
}
}
在 activity_led.xml 中直接當成普通控制項使用即可:
<ione.zy.demo.LEDView
android:id="@+id/ledview"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
TimeZone.getDefault(),否則非該時區的使用者會看到錯誤時間。Handler(Looper.getMainLooper()) 並在 stop 中一併 removeCallbacks。
手機QQ透明頭像怎麼做?兩種方法讓頭像「消失」
透明頭像的效果是聊天列表裡只剩名字、看不到頭像方塊,視覺上乾淨很多。原理並不神秘:一張全透明的 PNG 圖片,上傳之後自然什麼都不顯示。難點在於「去哪裡拿到這張圖」。方法
藍牙低功耗(BLE)技術全面解析
藍牙低能耗(BLE)是一項低成本、短距離且具備良好互操作性的無線技術,工作於免授權的 2.4GHz ISM 射頻頻段。從設計之初,它的目標就非常明確——成為一項超低功耗(
ListView(一):控件架構與 ArrayAdapter 徹底拆解
在 Android 的日常開發裡,ListView 幾乎是繞不開的一個名單。它以縱向列表的形式鋪排資料,並能依資料量自動決定可視範圍——手機裡的通訊錄就是最典型的例子。即
Android 源碼中的適配器模式:從 ListView 的 Adapter 說起
模式的定義 適配器模式把一個類的介面轉換成客戶端所期待的另外一種介面,讓原本因為介面不匹配而無法協同工作的兩個類能夠一起運作。 什麼時候會用到 用電源接口當例子最直觀: