Android Custom Controls Series tutorial----the real fillet button.

Source: Internet
Author: User

Frontier:now online casually enter a round corner button will appear a lot of blog and article tips to do such an effect, but that is mostly XML files to do, there is a big drawback, because every time you need to rewrite the XML file (even the simple change of the color of the button needs to be modified). ~ ~ for what? Because do not modify the concubine can't do!!! Today we'll take you on a real round button, so let's look at the effect. text dry start:obviously our button background is we want to achieve the fillet part, then we can't help thinking of setbackground this method, look inside the parameters, need is a drawable, and our button also need to click on the unavailable fillet effect, So we naturally think of statelistdrawable this class, but also if the fillet effect, then also need to roundrectshape this class and shapedrawable this class, if these classes are unfamiliar, then I will briefly introduce the role of these classes. statelistdrawableused primarily to add and manage backgrounds each state should use that drawable class, which needs to be aware of the management of each state within it. In fact, it can be understood that a hashmap corresponds to a value for each state. RoundrectshapeRemember when we needed a rounded rectangle? For this class is our main rectangle that draws rounded corners. but pay attention to its construction method, then the code inside again said. shapedrawablestate management and shape all have, then our pig feet will debut, right is shapedrawble it is our setbackground inside need drawble AH. Well, the role of each class is introduced, so let's go directly to the code.
Import Android.content.context;import Android.content.res.colorstatelist;import Android.graphics.color;import Android.graphics.drawable.shapedrawable;import Android.graphics.drawable.statelistdrawable;import Android.graphics.drawable.shapes.roundrectshape;import android.view.gravity;import android.widget.Button;/* * @ FileName:StyleButton.java * @Version: V1.0 * @Date: 2014-5-7 Create * @author: Edsheng */public class Stylebutton extends Button{public static int[]mnormalstate= new int[] {};p ublic static int[]mpressstate= new int[] {Android. R.attr.state_pressed, Android. r.attr.state_enabled};p ublic static int[]mdisablestate= new int[] {-android. r.attr.state_enabled};p ublic static int[]mselectedstate= new int[] {Android. R.attr.state_selected, Android. r.attr.state_enabled};p rivate intmradius= 0;//Default fillet radius//default text and background color private intmbgnormalcolor= color.red;private intmbgpressedcolor= color.green;private intmtextnormalcolor= color.white;private intmTextPressedColor= Color.GRAY; Public StylebUtton (Context context) {super (context); Initui ();} private void Initui () {setgravity (gravity.center); Builddraweablestate (); Buildcolordrawablestate ();} /** * Build picture drawble */private void Buildcolordrawablestate () {colorstatelist colorstatelist = new Colorstatelist (New int[][ ] {mpressstate, mnormalstate},new int[] {mtextpressedcolor, mtextnormalcolor}); SetTextColor (colorStateList);} /** * Build background drawble */private void Builddraweablestate () {float outrectr[] = new float[] {Mradius, Mradius, Mradius, Mradius , Mradius, Mradius, Mradius, Mradius};//Create a state manager statelistdrawable drawable = new statelistdrawable ();/** * Note Statelistdra The construction method of the wable we use here is the first parameter it is a float array that holds the radius of the fillet, it is a eight value that is saved clockwise by Top-left *///Create an arc shape roundrectshape rectshape = new Roundrectshape (outrectr, NULL, NULL);//create drawableshapedrawable presseddrawable = new shapedrawable (rectshape);// Sets the color of our button background presseddrawable.getpaint (). SetColor (Mbgpressedcolor);//Added to the state management Drawable.addstate (Mpressstate, presseddrawable);//shapedrawable disabledrawable = New Shapedrawable (Rectshape);//disabledrawable.getpaint (). SetColor (Prssedclor);//disabledrawable.getpaint (). Setalpha (//drawable.addstate); (Mdisablestate, disabledrawable); Shapedrawable normaldrawable = new shapedrawable (rectshape); Normaldrawable.getpaint (). SetColor (MBgNormalColor); Drawable.addstate (Mnormalstate, normaldrawable);//Set our background is the selectorsetbackgrounddrawable (drawable) inside the XML;} /** * Set Rounded Rectangle * * @param radius */public void Setradius (int radius) {This.mradius = Radius;builddraweablestate ();} /** * Set Button background color * * @param normalcolor * @param prssedclor */public void setbgnormalpressedcolor (int normalcolor, int PRSs Edclor) {Mbgnormalcolor = Normalcolor;mbgpressedcolor = Prssedclor;builddraweablestate ();} /** * Set Button text color * * @param normalcolor * @param pressedcolor */public void settextnormalpressedcolor (int normalcolor, int Pressedcolor) {Mtextpressedcolor = Pressedcolor;mtextnormalcolor = Normalcolor;buildcolordrawablestate ();}}

This way our rounded buttons are created.Let's see how we can use it. Directly post the Oncrate function.
Requestwindowfeature (Window.feature_no_title); LinearLayout linearlayout = new LinearLayout (this); Linearlayout.setorientation (linearlayout.vertical); Linearlayout.layoutparams layoutparams = new Layoutparams, layoutparams.bottommargin = 20; Layoutparams commomlayoutparams = new Layoutparams (layoutparams.match_parent, layoutparams.wrap_content); Commomlayoutparams.bottommargin = 20; Stylebutton button = new Stylebutton (this), Button.settext ("button"), Linearlayout.addview (button, layoutparams); Stylebutton button2 = new Stylebutton (this); Button2.settext ("button"); Button2.setradius (+); Linearlayout.addview ( Button2, Commomlayoutparams); Stylebutton Button3 = new Stylebutton (this); Button3.settext ("button"); Button3.setradius (32); Button3.settextnormalpressedcolor (Color.cyan, Color.White); Button3.setbgnormalpressedcolor (Color.BLUE, Color.cyan); Linearlayout.addview (Button3, commomlayoutparams); Stylebutton button4 = new Stylebutton (this); Button4.settext ("button"); Button4.setradius; Button4.setbgnormalpresseDcolor (Color.gray, Color.cyan); Button4.settextnormalpressedcolor (color.red, Color.White); LinearLayout.addView ( Button4, Commomlayoutparams); Stylebutton button5 = new Stylebutton (this), button5 = new Stylebutton (this); Button5.settext ("button"); Button5.setradius ( Button5.settextnormalpressedcolor (Color.Black, Color.Blue); Button5.setbgnormalpressedcolor (Color.WHITE, Color.cyan); Linearlayout.addview (Button5, commomlayoutparams); Stylebutton button6 = new Stylebutton (this); Button6.settext ("button"); Button6.setradius (50); Button6.settextnormalpressedcolor (Color.Black, Color.cyan); Button6.setbgnormalpressedcolor (Color.CYAN, Color.Blue); Layoutparams params = new Layoutparams (layoutparams.match_parent, layoutparams.wrap_content);p arams.leftmargin = 80; Params.rightmargin = 80;linearlayout.addview (button6, params); Stylebutton button7 = new Stylebutton (this); Button7.settext ("button"); Button7.setradius (n); Linearlayout.addview ( Button7, Layoutparams); Setcontentview (LinearLayout);
Is it convenient and quick to use? Whether it's a color change or a fillet radius so we've customized a rounded button of our own. All right. Take it for a pleasant use.

Android Custom Controls Series tutorial----the real fillet button.

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.