Android Demo---How to knock out rounded button+ with rounded head

Source: Internet
Author: User

Often play the app's small partners know that the app has a lot of buttons are rounded, round to feel full, full of tension, do not know the design of the corner button small partner is not and small series have the same idea ' (*∩_∩*), listen to small knitting company developed iOS small partner said, They directly have a rounded button, but for the development of Android's small partner is not the same, there is no direct fillet button for us to use, in the XML layout a button, not rounded, how to do nie, the method is always more difficult, Our opportunity to grow again, recently in the small series of projects, need to use the round corner of the button, but also need to use the rounded head, after half a day, finally is rounded, happy ing, such as the next, but the display of the user Center is a picture, not with ImageView, Describes how to knock out a rounded button, the small part of the next describes how to knock out the rounded head, as follows:

Today this blog, small made a little piece of how to make a round of the Android small demo, hoping to help the need for small partners, first, create a new Android project, under the drawable_xhdpi this file, create a new Android XML file, The name is fillet, as shown in:

Then the second step, we started to write the XML inside the fillet, the code looks like this:

<?xml version= "1.0" encoding= "UTF-8"?> <shape     xmlns:android= "http://schemas.android.com/apk/res/ Android "     android:shape=" Rectangle ">     <!--fill Color--     <solid android:color=" #4392F9 "/>     <!--set the four corners of the button arc--     <!--android:radius arc Radius--     <corners android:radius= "5dip"/>       <!--The text inside the Padding:button with the button boundary--<padding    android:left= "10DP"    android:top= "10DP"    android:right= "10DP"    

The third step is to write the file activity_main.xml inside the layout, as shown in the code below:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "     android:orientation=" vertical "     android:layout_width=" fill_parent "     android:layout_height=" Fill_parent "     >   <!--the layout of a story, which reads" (*∩_∩*) ′--> <textview        android:layout_width= "Fill_ Parent "       android:layout_height=" wrap_content "       android:text=" and a Story "/>   <!--   the layout of the Fillet button-- >  <button       android:id= "@+id/filletbutton"     android:layout_gravity= "center"    android: text= "Fillet button"     android:textcolor= "#FFFFFF"    android:layout_width= "wrap_content"       android:layout_ height= "Wrap_content"       

The fourth step is to write the contents of Styles.xml under the values file, as shown in the following code:

<resources> <!--Base application theme, DE Pendent on API level.    This theme are replaced by Appbasetheme from Res/values-vxx/styles.xml on newer devices. --<style name= "Appbasetheme" parent= "Android:Theme.Light" > <!--Theme customizations av Ailable in newer API levels can go in res/values-vxx/styles.xml, while customizations related to BAC        Kward-compatibility can go here. -</style> <!--application theme.  --<style name= "Apptheme" parent= "Appbasetheme" > <!--all customizations that is not specific to a Particular api-level can go here. -</style> <string name= "action_settings" >SETTINGS</STRING></RESOURCES>  

        Fifth step, write the contents of the Fillbuttonactivity.java class, as shown in the following code:    

Package Com.example.filletbutton;import Android.app.activity;import Android.os.bundle;import Android.view.View; Import Android.view.View.OnClickListener; Import Android.widget.button;import Android.widget.toast;public class Filletbuttonactivity extends Activity {  Button Filletbutton;      @Override public     void OnCreate (Bundle savedinstancestate) {          super.oncreate (savedinstancestate);          Setcontentview (r.layout.activity_main);                    filletbutton= (Button) Findviewbyid (R.id.filletbutton);        Register button event Filletbutton.setonclicklistener with anonymous class (          new Onclicklistener ()        {public                   void OnClick (View v)              {                  toast.maketext (filletbuttonactivity.this, "You clicked the Fillet button", Toast.length_long). Show ();}              }          );}  }

Finally, let's look at the effect of the run, as shown in:


After the introduction of how to knock out a rounded button, small series and then like the small friends to introduce, how to knock out a rounded head, first, first, we write the XML layout code, the specific code is as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "android:layout_width=" Match_parent "android:layout_height = "Match_parent" android:orientation= "vertical" tools:context= ".        Testactivity "> <framelayout android:layout_width=" match_parent "android:layout_height=" 135DP "            android:background= "@drawable/us" > <framelayout android:layout_width= "match_parent"                android:layout_height= "44DP" > <imagebutton android:id= "@+id/about_us_back" Android:layout_width= "14DP" android:layout_height= "25.5DP" android:layout_gravity= "center             _vertical "android:layout_marginleft=" 10DP "android:background=" @drawable/back "/> <!--about our layout--<textview Android:layout_widtH= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "center"         Android:text= "About Us" android:textcolor= "#fff" android:textsize= "19sp"/>              </FrameLayout> <imageview android:id= "@+id/imageview2" android:layout_width= "70DP" android:layout_height= "70DP" android:layout_margintop= "8DP" android:layout_gravity= "C Enter "android:layout_alignparenttop=" true "android:layout_centerhorizontal=" true "and roid:src= "@drawable/my"/> <!--user nickname, hassle-free layout-<textview android:layout_width= "Wra            P_content "android:layout_height=" wrap_content "android:layout_gravity=" Center_horizontal|bottom "    Android:layout_marginbottom= "5DP" android:textcolor= "#FFFFFF" android:text= "carefree Mood"/>    </FrameLayout></LinearLayout> 
Next, let's write the code inside the Java class, which looks like this:

Package Com.h8.imageroundcorner;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.paint;import Android.graphics.porterduffxfermode;import Android.graphics.rect;import Android.graphics.rectf;import Android.graphics.bitmap.config;import Android.graphics.porterduff.mode;import Android.os.Bundle;import Android.widget.imageview;public class Testactivity extends Activity {ImageView ImageView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.test); ImageView = (ImageView) Findviewbyid (R.ID.IMAGEVIEW2); Bitmap B = Bitmapfactory.decoderesource (Getresources (), r.drawable.my); Imageview.setimagebitmap (ToRoundCorner (b), 100));} public static Bitmap Toroundcorner (Bitmap Bitmap, int pixels) {Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bit Map.getheight (), config.argb_8888); Canvas canvas = new canvas (output); final int color = 0xff424242;final Paint paint = new paint (); final rect rect = new Rect (0, 0, bitmap.getwidth (), Bitmap.getheight ()); final Re CtF RECTF = new RECTF (rect), final float roundpx = Pixels;paint.setantialias (True); Canvas.drawargb (0, 0, 0, 0);p Aint.setco Lor (color); Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint);p Aint.setxfermode (New Porterduffxfermode (mode.src_ in)); Canvas.drawbitmap (bitmap, rect, rect, paint); return output;}}
Finally, let's take a look at:


Small Editor's note: this blog post, the small part of the main simple introduction, how to make a rounded button in the Android application. And how to make a rounded head, or that sentence for the small part, is not only a challenge is the opportunity, because the knowledge is the same, in addition, in the small part of the program life, leaving the most precious memories, although the later small not necessarily engaged in the industry of Android, code in the world, a lot of things, some sweet, some warm , some tactfully into a song, and some stretches endlessly, in these stories, our only common is that a certain year, a month, some placid days, once very love you! Love You-the days of this internship, Android brought to the small series of various surprises.     


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Demo---How to knock out rounded button+ with rounded head

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.