Android Modular Programming-COOL Compact Multifunction button

Source: Internet
Author: User

Previous time customer needs, to add a few buttons, including clean-up cache, about, settings, positioning and so on. But the interface has been done, and then re-layout interface, very troublesome. So I did a little button. Share here for everyone to learn. Here are a few, animation effect without tools to save, is to expand in turn. At the end of the text with the source download link.



First on the custom layout file: Composerlayout.java

Package Com.example.button;import Android.annotation.suppresslint;import Android.content.context;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.util.attributeset;import Android.view.view;import Android.widget.imageview;import Android.widget.linearlayout;import Android.widget.RelativeLayout, @SuppressLint ("Viewconstructor") public class Composerlayout extends Relativelayout { public static byte Rightbottom = 1, Centerbottom = 2, Leftbottom = 3,leftcenter = 4, Lefttop = 5, Centertop = 6, Righttop = 7,rightcenter = 8;private Boolean hasinit = false; Private Boolean arebuttonsshowing = False;private Context mycontext;private ImageView Cross; Private relativelayout rlbutton;private animations Myani; Private linearlayout[] llayouts;  private int duretime = 300;public Composerlayout (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle); this.mycontext = context;} Public Composerlayout (context context, AttributeSet Attrs) {Super (context, attrs); this.mycontext = context;} Public Composerlayout (Context context) {super (context); this.mycontext = context;} /** * Initialize * * @param imgresid * sub-button picture drawable ID * @param showhidebuttonid * Main button drawable ID * @para M Crossid * Main button above cross tab drawable ID * @param pcode * location code, e.g. "upper right Corner" align_parent_bottom| Align_parent_right * @param radius * * @param durationmillis * animation time */public void Init (int[] Imgres Id, int showhidebuttonid, int crossid,byte pcode, int radius, final int durationmillis) {duretime = Durationmillis;int Ali  GN1 = Align2 = 14;if (Pcode = = Rightbottom) {//lower right corner align1 = Align_parent_right;align2 = Align_parent_bottom;} else if (Pcode = = Centerbottom) {//Lower align1 = Center_horizontal;align2 = Align_parent_bottom;} else if (Pcode = = Leftbottom) {//lower left corner align1 = Align_parent_left;align2 = Align_parent_bottom;} else if (Pcode = = Leftcent ER) {//center left align1 = ALIGN_PARENT_LEFT;ALIGN2 = center_vertical;} else if (Pcode = = Lefttop) {//upper left corner align1 = Align_parent_left;align2 = Align_parent_top;} else if (Pcode = centertop) {//Upper ALIGN1 = C Enter_horizontal;align2 = Align_parent_top;}  else if (Pcode = = righttop) {//upper-right corner align1 = Align_parent_right;align2 = Align_parent_top;} else if (Pcode = = Rightcenter) {//Right align1 = Align_parent_right;align2 = center_vertical;} Processing radius Relativelayout.layoutparams thislps = (layoutparams) this.getlayoutparams (); Bitmap Mbottom = Bitmapfactory.decoderesource (Mycontext.getresources (), imgresid[0]); if (Pcode = = Centerbottom | | pCode = = Centertop) {if (thislps.width! = -1&& Thislps.width! = -2&& Thislps.width < (radius + Mbottom.getwid Th () + radius * 0.1) * 2) {thislps.width = (int) (RADIUS * 1.1 + mbottom.getwidth ()) * 2);}} else {if (thislps.width! = -1&& Thislps.width! = -2&& Thislps.width < radius + mbottom.getwidth () + rad ius* 0.1) {thislps.width = (int) (RADIUS * 1.1 + mbottom.getwidth ());}} if (Pcode = = Leftcenter | | pcode = = RIghtcenter) {if (thislps.height! = -1&& Thislps.height! = -2&& Thislps.height < (radius + mBottom.getHe ight () + radius * 0.1) * 2) {thislps.width = (int) (RADIUS * 1.1 + mbottom.getheight ()) * 2);}}  else {if (thislps.height! = -1&& Thislps.height! = -2&& Thislps.height < radius + mbottom.getheight () + radius* 0.1) {thislps.height = (int) (RADIUS * 1.1 + mbottom.getheight ());}} This.setlayoutparams (THISLPS); Relativelayout RL1 = new Relativelayout (mycontext);//contains several sub-buttons Rlbutton = new Relativelayout (mycontext); Main-button llayouts = new linearlayout[imgresid.length];//N child button for (int i = 0; i < imgresid.length; i++) {ImageView img = n EW ImageView (mycontext);//Sub-button Img.setimageresource (Imgresid[i]); Linearlayout.layoutparams LLPs = new Linearlayout.layoutparams (LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); Img.setlayoutparams (LLPs); llayouts[i] = new LinearLayout (mycontext);// Sub-button Llayouts[i].setid (+ i);//set a random ID, convenient onclicK when recognized. Randomly set, if found with other controls, you can change it yourself. Llayouts[i].addview (IMG); Relativelayout.layoutparams rlps = new Relativelayout.layoutparams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); rlps.alignwithparent = True;rlps.addrule (align1, relativelayout.true); Rlps.addrule (ALIGN2, relativelayout.true); Llayouts[i].setlayoutparams (rlps); Llayouts[i].setvisibility ( view.invisible);//This cannot be Gonerl1.addview (Llayouts[i]);} Relativelayout.layoutparams rlps1 = new Relativelayout.layoutparams (RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); rlps1.alignwithparent = True;rlps1.addrule (align1, relativelayout.true); Rlps1.addrule (ALIGN2, relativelayout.true); Rl1.setlayoutparams (RLPS1); Relativelayout.layoutparams buttonlps = new Relativelayout.layoutparams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); buttonlps.alignwithparent = True;buttonlps.addrule (ALIGN1, Relativelayout.true); Buttonlps.addrule (Align2, Relativelayout.truE); Rlbutton.setlayoutparams (buttonlps); Rlbutton.setbackgroundresource (showhidebuttonid); cross = new ImageView ( Mycontext); Cross.setimageresource (CROSSID); Relativelayout.layoutparams crosslps = new Relativelayout.layoutparams (RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); crosslps.alignwithparent = True;crosslps.addrule (Center_in_parent, Relativelayout.true); Cross.setlayoutparams (crosslps); Rlbutton.addview (cross); Myani = new Animations (RL1, PCode, RADIUS), Rlbutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {if ( arebuttonsshowing) {myani.startanimationsout (duretime); Cross.startanimation (Animations.getrotateanimation (- 270,0, Duretime));} else {myani.startanimationsin (duretime); Cross.startanimation (Animations.getrotateanimation (0,-270, Duretime));} arebuttonsshowing =!arebuttonsshowing;}}); Cross.startanimation (animations.getrotateanimation (0), This.addview (RL1); This.addview (Rlbutton); Hasinit = true;} Public VoiD collapse () {myani.startanimationsout (duretime); Cross.startanimation (Animations.getrotateanimation (-270, 0, Duretime)); arebuttonsshowing = false;} public void expand () {Myani.startanimationsin (duretime); Cross.startanimation (animations.getrotateanimation (0, 270 , Duretime)); arebuttonsshowing = true;} public Boolean isinit () {return hasinit;} public Boolean isshow () {return arebuttonsshowing;} /** * Set each child button onclick event */public void Setbuttonsonclicklistener (final onclicklistener L) {if (llayouts! = null) {for (int i = 0; i < llayouts.length; i++) {if (llayouts[i]! = null) Llayouts[i].setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (final View view) {//Add other events here such as button enlargement or retract menu collapse (); L.onclick (view);}});}}}
Here you customize a baseactivity, the benefits of using baseactivity No, I said:

Package Com.example.button;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.menu;import Android.view.menuinflater;import Android.view.menuitem;public abstract class Baseactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setview (); Initview (); Setlistener (); InitData ();} @Overrideprotected void Onresume () {Super.onresume ();} @Overrideprotected void OnPause () {super.onpause ();} /** * Set the layout file */public abstract void Setview ()/** * Initialize the control in the layout file */public abstract void Initview ();/** * Set the control's listener */public a bstract void Setlistener ();p rivate void InitData () {String AppID = "Wx88818f8c48a95eb4";} @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {    Super.onactivityresult ( Requestcode, ResultCode, data);}}
Animation settings class, animations, here is an open source animation library Nineoldandroids.jar. Download the text in the original code has

Package Com.example.button;import Static Com.nineoldandroids.view.viewpropertyanimator.animate;import Java.util.arraylist;import Java.util.list;import Android.view.view;import Android.view.viewgroup;import Android.view.animation.animation;import Android.view.animation.rotateanimation;import Android.widget.linearlayout;import Com.nineoldandroids.animation.animator;import Com.nineoldandroids.animation.animator.animatorlistener;import Com.nineoldandroids.view.ViewPropertyAnimator; public class Animations {public final int R;//Radius public static byte Rightbottom = 1, Centerbottom = 2, Leftbottom = 3,lef Tcenter = 4, Lefttop = 5, Centertop = 6, righttop = 7,rightcenter = 8;private int pc; Location Code private ViewGroup clayout; Parent layoutprivate final int amount; There are a few buttons private double fullangle = 180.0;//in a few large angles, the private byte Xori = 1, yori = 1; X, y value direction, that is, the line up or down private Boolean isOpen = false;//Whether the record is open or closed private list<viewpropertyanimator> viewanimators = new Arraylist<viewpropertyaniMator> ();/** * Structure function * * @param comlayout * Package pop-up button layout * @param poscode * Position number, sub-rightbottom , Centerbottom, Leftbottom, Leftcenter, * lefttop, Centertop, Righttop, Rightcenter * @param radius * Half diameter *  /public animations (viewgroup comlayout, int poscode, int radius) {this.pc = Poscode;this.clayout = Comlayout;this.amount = Clayout.getchildcount (); R = radius;//initialization animation, each view corresponds to a animatorfor (int i = 0; i < amount; i++) {View Childat = Clayout.getchildat (i); Viewpropertyanimator anim = animate (Childat); Viewanimators.add (anim);}  if (Poscode = = Rightbottom) {//lower right corner Fullangle = 90;xori = -1;yori =-1;} else if (Poscode = = Centerbottom) {//Lower Fullangle = 180;xori = -1;yori =-1;} else if (Poscode = = Leftbottom) {//lower left corner fullangle = 90;xori = 1;yori =-1;} else if (Poscode = = Leftcenter) {//center left Fullang Le = 180;xori = 1;yori =-1;} else if (Poscode = = lefttop) {//upper-left corner fullangle = 90;xori = 1;yori = 1;} else if (Poscode = = centertop) {//Upper Fullangle = 180;xori = -1;yori = 1;} else if (Poscode = = righttop) {//upper-right corner Fullangle = 90;xori = -1;yori = 1;} else if (Poscode = = Rightcenter) {//Right middle Fullangl E = 180;xori = -1;yori =-1;}} Private class Animlistener implements Animatorlistener {private View target;public Animlistener (View _target) {target = _t Arget;} @Overridepublic void Onanimationstart (Animator animation) {} @Overridepublic void Onanimationend (Animator animation) { if (!isopen) {target.setvisibility (view.invisible);}} @Overridepublic void Onanimationcancel (Animator animation) {//TODO auto-generated method stub} @Overridepublic void  Onanimationrepeat (Animator animation) {//TODO auto-generated method stub}}<pre name= "code" class= "Java" >/** * Open * *
public void Startanimationsin (int durationmillis) {isOpen = true;for (int i = 0; i < Clayout.getchildcount (); i++) {fin Al LinearLayout Inoutimagebutton = (linearlayout) clayout.getchildat (i);d ouble offangle = fullangle/(amount-1); final d Ouble DeltaY, deltax;if (PC = = Leftcenter | | pc = = rightcenter) {deltax = Math.sin (Offangle * i * math.pi/180) * R;delta Y = Math.Cos (Offangle * i * math.pi/180) * R;} else {DeltaY = Math.sin (Offangle * i * math.pi/180) * R;deltax = Math.Cos (Offangle * i * math.pi/180) * R;} Viewpropertyanimator viewpropertyanimator = Viewanimators.get (i); Viewpropertyanimator.setlistener (null); Inoutimagebutton.setvisibility (view.visible); viewpropertyanimator.x ((float) (Inoutimagebutton.getleft () + XOri * deltax). Y ((float) (inoutimagebutton.gettop () + Yori * deltay));}} /** * */public void startanimationsout (int durationmillis) {isOpen = false;for (int i = 0; i < Clayout.getchildcount ( ); i++) {final LinearLayout Inoutimagebutton = (linearlayout) clayout. Getchildat (i); Viewpropertyanimator viewpropertyanimator = Viewanimators.get (i); Viewpropertyanimator.setlistener (null); Viewpropertyanimator.x ((float) inoutimagebutton.getleft ()). Y ((float) inoutimagebutton.gettop ()); Viewpropertyanimator.setlistener (New Animlistener (Inoutimagebutton));}} public int Getposcode () {return this.pc;} /** * Rotation function * * @param fromdegrees * from how many degrees * @param todegrees * to how many degrees * @param durationmillis * m */public static Animation G Etrotateanimation (float fromdegrees,float todegrees, int durationmillis) {rotateanimation rotate = new Rotateanimation ( Fromdegrees, Todegrees,animation.relative_to_self, 0.5f, animation.relative_to_self,0.5f); Rotate.setDuration ( Durationmillis); Rotate.setfillafter (true); return rotate;}}
Main activity:testactivity

Package Com.example.button;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.relativelayout;import Android.widget.toast;public class Testactivity extends BaseActivity {private Composerlayout clayout; @Overridepublic void Setview () {Setcontentview (r.layout.activity_main);} @Overridepublic void Initview () {//reference control Clayout = (composerlayout) Findviewbyid (r.id.test); Clayout.init (new int[] { R.drawable.composer_camera,r.drawable.composer_music, R.drawable.composer_place,r.drawable.composer_sleep, R.drawable.composer_thought,r.drawable.composer_with}, R.drawable.composer_button,r.drawable.composer_icn_plus, Composerlayout.rightcenter, 180,300);} @Overridepublic void Setlistener () {//Click event Listener, 100+0 corresponds to composer_camera,100+1 corresponding to Composer_music ... So you can just add a few buttons to your organic button. Onclicklistener Clickit = new Onclicklistener () {@Overridepublic void OnClick (View v) {if (V.getid () = = + 0) {toast.ma Ketext (Testactivity.this, "test a", 0). Show (); else if (v.getid () = = 1) {TOAST.MAketext (Testactivity.this, "Test b ...", 0). Show (); else if (v.getid () = = 2) {Toast.maketext (testactivity.this, "Test c ...", 0). Show ();} else if (v.getid () = = 3) {Toast.maketext (testactivity.this, "Test D ...", 0). Show ();} else if (v.getid () = = 4) {Toast.maketext (testactivity.this, "Test e ...", 0). Show ();} else if (v.getid () = = 5) {Toast.maketext (testactivity.this, "Test f:", 0). Show ();}}; Clayout.setbuttonsonclicklistener (Clickit); Relativelayout RL = (relativelayout) Findviewbyid (r.id.rlparent); Rl.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {//TODO auto-generated method Stubtoast.maketext (Testactivity.this, "The parent control can click on the OH!!!" , 0). Show (); System.out.println ("The parent control can be clicked. ");}});}}

Next is the layout file Activity_main:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/rlparent "    android:layout_width=" Fill_parent "    android: layout_height= "Fill_parent"    android:background= "#ABCDEF" >    <com.example.button.composerlayout        android:id= "@+id/test"        android:layout_width= "20DP"        android:layout_height= "Wrap_content        " Android:layout_alignparentright= "true"        android:layout_centervertical= "true" >    </ Com.example.button.composerlayout></relativelayout>


Download Nineoldandroids-2.4.0.jar

Download Demo



Android Modular Programming-COOL Compact Multifunction 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.