On an article, learned the use of andengine in the Buttonsprite, said to achieve the buttonsprite with text, directly paste code:
public class Textbuttonsprite extends Rectangle {private final static String TAG = "Textbuttonsprite";
Display text private final text mText;
Buttonsprite Mbuttonsprite; Public Textbuttonsprite (float PX, float PY, tiledtextureregion ptiledtextureregion, Font Pfont, String ptext, Vertex
Bufferobjectmanager Pvertexbufferobjectmanager) {super (PX, PY, Ptiledtextureregion.getwidth (), ptiledtextureregion
. GetHeight (), Pvertexbufferobjectmanager);
Mbuttonsprite = new Buttonsprite (0, 0, ptiledtextureregion, Pvertexbufferobjectmanager);
MText = new Text (0, 0, Pfont, Ptext, Pvertexbufferobjectmanager);
float buttontextureregionwidth = Ptiledtextureregion.getwidth ();
float buttontextureregionheight = Ptiledtextureregion.getheight ();
float textWidth = Mtext.getwidth ();
float textHeight = Mtext.getheight (); When the length of the text is greater than the length of the button, the button is scaled to fit the text if (TextWidth > Buttontextureregionwidth | | textHeight > BUTTONTEXTUREREGIONH Eight) {Float Widthscale = TextwidTh/buttontextureregionwidth;
float Heightscale = textheight/buttontextureregionheight;
if (Widthscale > Heightscale) {setwidth (buttontextureregionwidth * widthscale);
SetHeight (Buttontextureregionheight * widthscale);
Add an offset, of course, you can not add Mbuttonsprite.setscale ((Widthscale + 0.1f));
} else {setwidth (buttontextureregionwidth * heightscale);
SetHeight (Buttontextureregionheight * heightscale);
Add an offset, of course, you can not add Mbuttonsprite.setscale ((Heightscale + 0.1f));
}} if (Mchildrenignoreupdate) {}//Calculate center position Float TEXTX = (getwidth ()-mtext.getwidth ())/2;
float texty = (getheight ()-mtext.getheight ())/2;
Mbuttonsprite.setposition ((GetWidth ()-buttontextureregionwidth)/2, (GetHeight ()-buttontextureregionwidth)/2);
Mtext.setposition (TEXTX, texty);
Attachchild (Mbuttonsprite);
Attachchild (MText); }//===========================================================//Constants//===========================================================//===========================================================//Fields//====== =====================================================
// ======================================================== = = =//constructors//===========================================================//=============================== ============================//Getter & Setter//===========================================================// ===========================================================//Methods for/from superclass/interfaces//=========== ================================================ @Override Public boolean onareatouched (TouchEvent pscenetouchevent
, float ptoucharealocalx, float ptoucharealocaly) {log.e (TAG, "[onareatouched]");
Return mbuttonsprite.onareatouched (Pscenetouchevent, Ptoucharealocalx, ptoucharealocaly); }//===========================================================//Methods//===========================================================//===========================================================//Inner
and Anonymous Classes//===========================================================}