Common custom UI components-1: timeview

Source: Internet
Author: User
Zookeeper

Recently I have been working on a set-top box project, which of the following Frequently Used components will be recorded here.

Decompile the youku video TV .. I also hope that you will learn more about others' code for your own use.

Of course there are other methods, such as listening to the broadcast sent by the system. Wait until you have time to paste it.


: Time in the upper right corner

Http://img.blog.csdn.net/20140630144648750? Watermark/2/text/plain/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma ==/ dissolve/70/gravity/center


Timeview. Java


Package com. Youku. TV. widget;

Import Android. content. context;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. util. attributeset;
Import Android. View. layoutinflater;
Import Android. View. view;
Import Android. widget. linearlayout;
Import Android. widget. textview;
Import java. util. calendar;
Import java. util. date;


Public class timeview extends linearlayout {
Class clockhandler extends handler {

Private void post ()
{
Sendmessagedelayed (obtainmessage (0), 1000 * (60-calendar. getinstance (). Get (13 )));
}

Public void handlemessage (message ){
Super. handlemessage (Message );
If (! Mstopped ){
Updateclock ();
Post ();
}
}

Public void startscheduleupdate (){
Mstopped = false;
Post ();
}

Public void stopscheduleupdate (){
Mstopped = true;
Removemessages (0 );
}

Private Boolean mstopped;
Final timeview this $0;

Clockhandler (){
Super ();
This $0 = timeview. This;
}
}


Public timeview (context, attributeset ){
Super (context, attributeset );
Mcontext = getcontext ();
(Layoutinflater) Context. getsystemservice ("layout_inflater"). Inflate (R. layout. time_simple, this );
Mhour = (textview) findviewbyid (R. Id. system_hour );
Mminute = (textview) findviewbyid (R. Id. system_minute );
Mclockupdater = new clockhandler ();
}


Protected void onattachedtowindow (){
Super. onattachedtowindow ();
Updateclock ();
Mclockupdater. startscheduleupdate ();
}

Protected void ondetachedfromwindow (){
Super. ondetachedfromwindow ();
Mclockupdater. stopscheduleupdate ();
}

Protected void onvisibilitychanged (view, int I)
{
Super. onvisibilitychanged (view, I );
}

Void updateclock ()
{
Calendar calendar = calendar. getinstance ();
Calendar. settime (new date ());
Int K = calendar. Get (5 );
Int L = calendar. Get (11 );
Int I1 = calendar. Get (12 );
If (mhour! = NULL)
{
Textview textview2 = mhour;
Object aobj2 [] = new object [1];
Aobj2 [0] = integer. valueof (L );
Textview2.settext (string. Format ("% 2D:", aobj2 ));
}
If (mminute! = NULL)
If (I1 <10)
{
Textview textview1 = mminute;
Object aobj1 [] = new object [1];
Aobj1 [0] = integer. valueof (I1 );
Textview1.settext (string. Format (0% D ", aobj1 ));
} Else
{
Textview = mminute;
Object aobj [] = new object [1];
Aobj [0] = integer. valueof (I1 );
Textview. settext (string. Format ("% 2D", aobj ));
}
Invalidate ();
}

Private Static final string tag = "timeview ";
Private clockhandler mclockupdater;
Private context mcontext;
Private textview mhour;
Private textview mminute;

}

Time_simple.xml layout File


<? XML version = "1.0" encoding = "UTF-8"?>
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content">

<Textview
Android: Id = "@ ID/system_minute"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignparentright = "true"
Android: text = "01"
Android: textcolor = "@ color/timecolor"
Android: textsize = "@ dimen/px42"/>

<Textview
Android: Id = "@ ID/system_hour"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_toleftof = "@ ID/system_minute"
Android: text = "55"
Android: textcolor = "@ color/timecolor"
Android: textsize = "@ dimen/px42"/>

</Relativelayout>


Zookeeper

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.