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