Android custom Controls-TextView with images

Source: Internet
Author: User

Recently in the study of Android, while reading the code, see is Li Ning's Android development full handout, because it is on the book knocked,

So the code is basically consistent with the code of the original book.

See the fourth chapter, customize the control, feel the need to record something, all the code to write to the blog as their own later access to information.

First on

New Net.blogjava.mobile.widget/icontextview.java in SRC

The code is as follows:

 PackageNet.blogjava.mobile.widget;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Rect;ImportAndroid.util.AttributeSet;ImportAndroid.widget.TextView; Public classIcontextviewextendstextview{//the value of the namespace    Private FinalString namespace = "Http://net.blogjava.mobile"; //Image Resource ID    Private intResourceId = 0; PrivateBitmap Bitmap;  PublicIcontextview (Context context, AttributeSet attrs) {Super(context, attrs); ResourceId= Attrs.getattributeresourcevalue (namespace, "Iconsrc", 0); if(ResourceId > 0) Bitmap=Bitmapfactory.decoderesource (Getresources (), resourceId); } @Overrideprotected voidOnDraw (canvas canvas) {if(Bitmap! =NULL)        {                        //The area of the image captured from the original, in this case the entire imageRect src =NewRect (); //copy the captured image to the target area on the bitmap, in this case the same as the copy areaRect target =NewRect (); Src.left= 0; Src.top= 0; Src.right=bitmap.getwidth (); Src.bottom=bitmap.getheight (); intTextHeight = (int) gettextsize (); Target.left= 0; //calculates the ordinate of the image copied to the catalog area. Since the TextView Chinese text content is not drawn from the top, it is necessary to recalculate the ordinate of the drawing image.Target.top = (int) ((Getmeasuredheight ()-gettextsize ())/2) + 1; Target.bottom= Target.top +TextHeight; //To ensure that the image is not deformed, the width of the image needs to be recalculated according to the image heightTarget.right = (int) (TextHeight * (Bitmap.getwidth ()/(float) bitmap. GetHeight ()); //start drawing an imagecanvas.drawbitmap (bitmap, SRC, target, getpaint ()); //moves the text in TextView to the right by a certain distance (in this case, the position of the image width plus 2 pixel points)canvas.translate (Target.right+ 2, 0); }        Super. OnDraw (canvas); }}
Icontextview

Res/layout/main.xml

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:mobile= "Http://net.blogjava.mobile"android:orientation= "vertical"Android:layout_width= "Fill_parent"Android:layout_height= "Fill_parent">    <Net.blogjava.mobile.widget.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "First smiley face"mobile:iconsrc= "@drawable/small" />    <Net.blogjava.mobile.widget.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Second smiley face"android:textsize= "24DP"mobile:iconsrc= "@drawable/small" />    <Net.blogjava.mobile.widget.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Third smiley face"android:textsize= "36DP"mobile:iconsrc= "@drawable/small" />    <Net.blogjava.mobile.widget.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Fourth smiley face"android:textsize= "48DP"mobile:iconsrc= "@drawable/small" />    <Net.blogjava.mobile.widget.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Fifth smiley face"android:textsize= "36DP"mobile:iconsrc= "@drawable/small" />    <Net.blogjava.mobile.widget.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Sixth smiley face"android:textsize= "24DP"mobile:iconsrc= "@drawable/small" />    <Net.blogjava.mobile.widget.IconTextViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "Seventh smiley Face"mobile:iconsrc= "@drawable/small" />   </LinearLayout>                 
Main.xml

Here is a description of the xmlns:mobile= "Http://net.blogjava.mobile", which is a word that specifies the namespace

Android custom Controls-TextView with images

Related Article

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.