标签:
可是我们还须要解决显示的字体问题,让它看起来更像是一个真实的LED数字时钟。
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"; private void init(Context context) { AssetManager assets = context. getAssets(); final Typeface font = Typeface . createFromAsset( assets, FONT_DIGITAL_7); setTypeface (font );// 设置字体 } 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); } }
<merge xmlns:android ="http://schemas.android.com/apk/res/android" > <com.vectoryi.hack011.view.LedTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/default_time" android:textColor="#3300ff00" android:textSize="80sp" /> <com.vectoryi.hack011.view.LedTextView android:id="@+id/main_clock_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:textColor="#00ff00" android:textSize="80sp" /> </merge>
试想一下,当你<include />这个布局main.xml时,假设main.xml是这种:
<<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <com.vectoryi.hack011.view.LedTextView android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:layout_gravity ="center" android:text ="@string/default_time" android:textColor ="#3300ff00" android:textSize ="80sp" /> <com.vectoryi.hack011.view.LedTextView android:id ="@+id/main_clock_time" android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:layout_gravity ="center" android:textColor ="#00ff00" android:textSize ="80sp" /> </FrameLayout>
public class MainActivity extends Activity { private static final String DATE_FORMAT = "%02d:%02d:%02d" ; private static final int REFRESH_DELAY = 500; // 因为Android不同意其它线程操作UI,所以在此创建了一个Handler private final Handler mHandler = new Handler(); //用来实时更新LedTextView显示的线程 private final Runnable mTimeRefresher = new Runnable() { @Override public void run() { Calendar calendar = Calendar. getInstance (TimeZone . getTimeZone( "GMT+8")); // 设置时区 final Date d = new Date(); calendar .setTime (d ); mTextView. setText( String. format (DATE_FORMAT , calendar .get (Calendar .HOUR ), calendar. get( Calendar. MINUTE), calendar .get (Calendar .SECOND ))); mHandler. postDelayed (this , REFRESH_DELAY ); } }; private TextView mTextView ; @Override public void onCreate(Bundle savedInstanceState ) { super .onCreate (savedInstanceState ); setContentView (R .layout .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 ); } }
<com.vectoryi.hack011.view.LedTextView 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" />
最后附上GitHub相关资料(https://github.com/VectorYi/LedTextView.git)和源代码下载链接(源代码点我).
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/5153743.html