Custom Components---Picture and text for ImageButton effects

Source: Internet
Author: User

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

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.