Simple tutorial for Android-65th gun (custom control for digital LCD clock Demo)

Source: Internet
Author: User
Tags xml attribute

Simple tutorial for Android-65th gun (custom control for digital LCD clock Demo)
Next we will study how to implement a digital LCD clock, which is essentially a special effect.
First, create a layout file:

 
The XML Attribute of TextView in Android contains several attributes of shadow.
ShadowDX, shadowDy, and shadowRadius indicate the horizontal and vertical offsets of shadows and the radius of shadows. The following figure shows the content of these parameters.
ShadowDx and shadowDy can be seen from the following three figures.
DX = 20, Dy = 0

DX = 0, DY = 20

DX = 20, DY = 20

What is shadowRadius from the following three images?
R = 3

R = 10

R = 40

Through these effects, you can intuitively see the meaning of each attribute.
The following is a custom control:
Package com. yayun. leddemo; 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; // font private static final String DATE_FORMAT = % 02d: % 02d: % 02d; // Date Format private static final int REFRESH_DELAY = 500; // refresh delay private final Handler mHandler = new Handler (); private final Runnable mTimeRefresher = new Runnable () {@ Overridepublic void run () {Calendar calendar = Calendar. getInstance (TimeZone. getTimeZone (GMT + 8); // Time Zone 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 (); // font manager class final Typeface font = Typeface. createFromAsset (assets, FONT_DIGITAL_7); timeView. setTypeface (font); // set the bgView font. setTypeface (font);} public void start () {mHandler. post (mTimeRefresher);} public void stop () {mHandler. removeCallbacks (mTimeRefresher );}}

Create an Activity and call this control:

 

    
    
  
 

Package com. yayun. leddemo; import android. annotation. suppressLint; import android. annotation. targetApi; import android. app. actionBar; import android. app. activity; import android. OS. build; import android. OS. bundle; import android. view. menu; @ 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 (); // call start} @ Overrideprotected void onStop () {super. onStop (); ledView. stop (); // pause} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. activity_led, menu); return true ;}}

The running instance is as follows:

 

The recording display is incorrect. If you do not know why, you can run it on your own to view the effect.

 

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.