Android Custom Components-TextView with pictures

Source: Internet
Author: User

1. Defining attributes

<?XML version= "1.0" encoding= "Utf-8"?><Resources>        <declare-styleablename= "Icon_textview">        <attrname= "Iconsrc"format= "Reference"/>    </declare-styleable>    <attrname= "Customizestyle"format= "Reference" /></Resources>

2. Inheriting View:CustomTextView.java

ImportAndroid.content.Context;ImportAndroid.content.res.TypedArray;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Rect;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.widget.TextView;/*** Todo:document your custom TextView class.*/ Public classCustomtextviewextendsTextView {Private Static FinalString TAG = Customtextview.class. Getsimplename (); PrivateBitmap Bitmap;  PublicCustomtextview (Context context) {Super(context); }     PublicCustomtextview (Context context, AttributeSet attrs) { This(context, attrs, R.attr.customizestyle); }     PublicCustomtextview (context context, AttributeSet attrs,intDefstyle) {        Super(context, attrs, Defstyle); TypedArray a=context.obtainstyledattributes (Attrs, R.styleable.icon_textview); intRsid = A.getresourceid (r.styleable.icon_textview_iconsrc,0); if(rsid>0) {Bitmap=Bitmapfactory.decoderesource (Getresources (), rsid);    } a.recycle (); } @Overrideprotected voidOnDraw (canvas canvas) {rectbitmap (canvas); Super. OnDraw (canvas); }     Public voidrectbitmap (canvas canvas) {if(Bitmap! =NULL) {            //whether to crop the original pictureRect rect =NewRect (0, 0, Bitmap.getwidth (), Bitmap.getheight ()); //Where is it displayed?Rect Taget =NewRect (); //coordinates of the start XTaget.left=0; //the coordinates of the start Y: the height of the current view-the height the font actually occupies)/2 ensures that the picture is aligned with the textTaget.top= (int) (Getmeasuredheight ()-gettextsize ())/2+1;//System.out.println ("Getmeasuredheight:" +getmeasuredheight ());//System.out.println ("GetTextSize:" +gettextsize ()); //ensure picture equal to zoom: x coordinatesTaget.right = (int) (GetTextSize () * (Bitmap.getwidth ()/(float) (Bitmap.getheight ())); //coordinates of yTaget.bottom= (int) (taget.top+gettextsize ());            Canvas.drawbitmap (Bitmap, rect, Taget, Getpaint ()); Canvas.translate (Taget.right+2, 0.5f); }    }}

3: Layout file

<Relativelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:bingatt= "http://schemas.android.com/apk/res/package name"//Add package nameXmlns:tools= "Http://schemas.android.com/tools"Android:layout_height= "Match_parent"Android:layout_width= "Match_parent"Tools:context=". Mainactivity ">    <Demo.bing.customstyle.CustomTextViewAndroid:id= "@+id/icon36"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "My Emoji"android:textsize= "36SP"bingatt:iconsrc= "@drawable/icon"        />    <Demo.bing.customstyle.CustomTextViewAndroid:id= "@+id/icon24"Android:layout_below= "@id/icon36"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "My Emoji"android:textsize= "24SP"bingatt:iconsrc= "@drawable/icon"        />    <Demo.bing.customstyle.CustomTextViewAndroid:layout_below= "@id/icon24"Android:layout_width= "Fill_parent"Android:layout_height= "Wrap_content"Android:text= "my emoji."android:textsize= "12SP"bingatt:iconsrc= "@drawable/icon"        /></Relativelayout>

Android Custom Components-TextView with pictures

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.