Display images and text on RadioButton

Source: Internet
Author: User

 

It is easy to use XML files, but sometimes it must be implemented dynamically using java code. This is complicated. This requires inheriting RadioButton and overwriting the onDraw method.

 

The image in the Code is a Bitmap object.

 

@ Override

Protected void onDraw (Canvas canvas ){

Super. onDraw (canvas );

 

If (image! = Null ){

Paint pt = new Paint ();

Pt. setARGB );

// Eliminate the Sawtooth

Pt. setAntiAlias (true );

// Center the display image

Int imageX = (int) (this. getWidth ()-image. getWidth ()/2;

 

Canvas. drawBitmap (image, imageX, 5, pt );

Pt. setARGB (255,255,255,255 );

// Center the display string

Int strX = (int) (this. getWidth ()-name. getBytes (). length * 5.5)/2;

Canvas. drawText (name, strX, (image. getHeight () + 15), pt );

}

 

}

 

From the wxg630815 Column

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.