Android launcher Research (4) ---- development of desktop application shortcuts!

Source: Internet
Author: User

Hello everyone, today I want to share with you the development of the launcher desktop shortcut icon. We all know that the shortcut icon consists of two parts: the application icon and the Application name. In fact, the shortcut icon in launcher only inherits the textview control and re-draws it to make the background light gray (I don't know what the color is) the elliptical background, the displayed text color is white. Textview has the Android: drawabletop; drawablebottom (not all of them are written here) attribute to display the application icon.

I don't need to talk much about it. Let's just look at the example. Let's just tap into the code and grow faster.

Step 1: Create an android project named applicationdemo. For example:

Step 2: Create a colors. xml file in the values directory and define the colors to use. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <color name = "white"> # ffffff </color> <br/> <color name = "black" >#000000 </color> <br/> <color name = "bubble_dark_background"> # b2191919 </color> <br/> </resources> <br/>

Step 3: Create a bubbletextview class that inherits textview. The Code is as follows:

Package COM. tutor. application; <br/> Import android. content. context; <br/> Import android. graphics. canvas; <br/> Import android. graphics. paint; <br/> Import android. graphics. rectf; <br/> Import android. text. layout; <br/> Import android. util. attributeset; <br/> Import android. widget. textview; <br/> public class bubbletextview extends textview {<br/> Private Static final int corner_radius = 8; <br/> Private Static final int padding_h = 5; <br/> Private Static final int padding_v = 1; <br/> private final rectf mrect = new rectf (); <br/> private paint mpaint; <br/> Public bubbletextview (context) {<br/> super (context); <br/> Init (); <br/>}< br/> Public bubbletextview (context, attributeset attrs) {<br/> super (context, attrs); <br/> Init (); <br/>}< br/> Public bubbletextview (context, attributeset attrs, int defstyle) {<br/> super (context, attrs, defstyle ); <br/> Init (); <br/>}< br/> private void Init () {<br/> setfocusable (true ); <br/> // we need extra padding below to prevent the bubble being cut. <br/> setpadding (padding_h, 0, padding_h, padding_v); <br/> mpaint = new paint (paint. anti_alias_flag); <br/> mpaint. setcolor (getcontext (). getresources () <br/>. getcolor (R. color. bubble_dark_background); <br/>}< br/> @ override <br/> protected void drawablestatechanged () {<br/> invalidate (); <br/> super. drawablestatechanged (); <br/>}< br/> @ override <br/> Public void draw (canvas) {<br/> final layout = getlayout (); <br/> final rectf rect = mrect; <br/> final int left = getcompoundpaddingleft (); <br/> final int Top = getextendedpaddingtop (); <br/> rect. set (left + layout. getlineleft (0)-padding_h, <br/> top + layout. getlinetop (0)-padding_v, <br/> math. min (left + layout. getlineright (0) + padding_h, <br/> getscrollx () + getright ()-getleft (), <br/> top + layout. getlinebottom (0) + padding_v); <br/> canvas. drawroundrect (rect, corner_radius, corner_radius, mpaint); <br/> super. draw (canvas); <br/>}< br/>

Step 4: Modify the main. xml layout file. The Code is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: Orientation = "vertical" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "fill_parent" <br/> <textview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: drawabletop = "@ drawable/icon" <br/> Android: TEXT = "applicationdemo" <br/> Android: textcolor = "@ color/Black" <br/> <COM. tutor. application. bubbletextview <br/> Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" <br/> Android: drawabletop = "@ drawable/icon" <br/> Android: textcolor = "@ color/White" <br/> Android: TEXT = "applicationdemo" <br/> </linearlayout> <br/>

Step 5: Modify the androidmanifest. xml file. Note that we have added a transparent style to the activity. launcher is actually a transparent activity.

The Code is as follows (8th lines of code ):

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. tutor. application "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". applicationdemo "<br/> Android: theme =" @ Android: style/theme. wallpaper. notitlebar "<br/> Android: Label =" @ string/app_name "> <br/> <intent-filter> <br/> <action Android: Name =" android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "7"/> <br/> </manifest>

Step 6: run the above project and check the effect as follows:

Change Android: drawableleft to Android: drawabletop. The effect is as follows:

OK ~ You're done. Stop and go to bed !!!

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.