Android custom View combination control ---- LED digital clock

Source: Internet
Author: User

Android custom View combination control ---- LED digital clock

First

LEDView effect.

Previously, I saw a blog using two textviews to achieve this effect. So I want to use a custom control to implement an LEDView, which can be used directly.

The combination of controls, the two textviews stacked together, and then use the digital-7.ttf font to display data, so as to achieve the LED effect. The Code is as follows:

LEDView. class

 

Package ione. zy. demo; import java. io. file; import java. util. calendar; import java. util. date; import java. util. timeZone; import android. annotation. suppressLint; import android. content. context; import android. content. res. assetManager; import android. graphics. typeface; import android. OS. handler; import android. util. attributeSet; import android. view. layoutInflater; import android. view. view; import android. widget. linearLayout; import android. widget. textView; 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 () {@ Overridepublic void run () {Calendar ar calendar = Calendar ar. getInstance (TimeZone. getTimeZone (GMT + 8); final Date d = new Date (); calendar. setTime (d); timeView. setText (String. format (DATE_FORMAT, calendar. get (Calendar. HOUR), calendar. get (Calendar. MINUTE), calendar. get (Calendar. SECOND); mHandler. postDelayed (this, REFRESH_DELAY) ;};@ SuppressLint (NewApi) 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 layoutInflater = LayoutInflater. from (context); View view = layoutInflater. inflate (R. layout. ledview, this); timeView = (TextView) view. findViewById (R. id. ledview_clock_time); bgView = (TextView) view. findViewById (R. id. ledview_clock_bg); AssetManager assets = context. getAssets (); final Typeface font = Typeface. createFromAsset (assets, FONT_DIGITAL_7); timeView. setTypeface (font); // set the bgView font. setTypeface (font); // set the font} public void start () {mHandler. post (mTimeRefresher);} public void stop () {mHandler. removeCallbacks (mTimeRefresher );}}

Ledview. xml file

 

 

 
       
                
    
   
  
 

 

 

Use Demo

 

package ione.zy.demo;import android.os.Build;import android.os.Bundle;import android.view.Menu;import android.annotation.SuppressLint;import android.annotation.TargetApi;import android.app.ActionBar;import android.app.Activity;@TargetApi(Build.VERSION_CODES.HONEYCOMB)public class LEDActivity extends Activity {private LEDView ledView;@SuppressLint(NewApi)@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_led);ledView = (LEDView) findViewById(R.id.ledview);  ActionBar actionBar = getActionBar();    actionBar.setDisplayHomeAsUpEnabled(true); }@Overrideprotected void onResume() {super.onResume();ledView.start();}@Overrideprotected void onStop() {super.onStop();ledView.stop();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.activity_led, menu);return true;}}


Activity_led.xml

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.