1.
2. Custom code:
<span style= "Font-family:comic Sans ms;font-size:14px;" >public class Imagetextview extends View implements Onclicklistener {private paint mimagepaint;private paint Mtextpain t;private int mtextsize=16;private Bitmap imagebitmap; Private context context; private int viewheight;private displaymetrics displaymetrics;private int imagetop;private int texttop; Public Imagetextview (Context context) {super (context); this.context = Context;init ();} Public Imagetextview (context context, AttributeSet Attrs) {Super (context, attrs); this.context = Context;init ();} Public Imagetextview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, defstyle); this.context = C Ontext;init ();} /** * Initialize data */private void init () {displaymetrics = Getdisplaymetrics (); mimagepaint = new Paint (); mtextpaint = new Paint () ; Mtextpaint.setcolor (Color.Black); mtextpaint.settextsize (mtextsize*displaymetrics.density);// Mtextpaint.setantialias (TRUE); Open torque tooth Imagebitmap = Bitmapfactory.decoderesource (getreSources (), r.drawable.home); Set the background color SetBackground (getresources (). getdrawable (R.drawable.item_background_selector));// Set Click event Setonclicklistener (this);} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC); viewheight = GetHeight (); imagetop = (Viewheight-imagebitmap.getheight ())/2;//The text is centered vertically in the vertical direction texttop = (int) ((Viewheight-getfontheight (Mtextpaint))/2-mtextpaint.getfontmetrics (). top);} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas); Canvas.drawbitmap (Imagebitmap, Imagetop, Mimagepaint); Canvas.drawtext ("Open", Imagebitmap.getwidth () +20, Texttop, mtextpaint);} /** * Get phone Resolution * @return */public Displaymetrics getdisplaymetrics () {displaymetrics dm = new Displaymetrics (); Get Displaymetrics object method One DM = Context.getapplicationcontext (). Getresources (). Getdisplaymetrics (); Get Displaymetrics Object Method two//((Activity) CX). Getwindowmanager (). Getdefaultdisplay (). GETmetrics (DM); return DM; }/** * Gets the height of the font * @param paint * @return */private int getfontheight (paint paint) {fontmetrics fm = paint.getfontmetrics (); return (int) Math.ceil (fm.descent-fm.ascent); }/** * Change Font Color */@Overridepublic boolean ontouchevent (Motionevent event) {switch (event.getaction ()) {case MotionEvent.ACTION_DOWN:mTextPaint.setColor (Color.Blue); Break;case MotionEvent.ACTION_UP:mTextPaint.setColor ( Color.Black); break;default:break;} Invalidate (); return super.ontouchevent (event);} @Overridepublic void OnClick (View v) {}}</span>
3, Text Center reference
http://blog.csdn.net/hursing/article/details/18703599
Custom Components---Picture and text for ImageButton effects