文字转换成Drawable

来源:互联网 发布:拓扑结构容易网络风暴 编辑:程序博客网 时间:2024/04/29 17:08
https://github.com/devunwired/textdrawable


有的时候需要将文字转换成Drawable,显示于ImageView中。




源码:
Java代码 复制代码 收藏代码
  1. /** 
  2.  * Copyright (c) 2012 Wireless Designs, LLC 
  3.  * 
  4.  * Permission is hereby granted, free of charge, to any person obtaining 
  5.  * a copy of this software and associated documentation files (the 
  6.  * "Software"), to deal in the Software without restriction, including 
  7.  * without limitation the rights to use, copy, modify, merge, publish, 
  8.  * distribute, sublicense, and/or sell copies of the Software, and to 
  9.  * permit persons to whom the Software is furnished to do so, subject to 
  10.  * the following conditions: 
  11.  * 
  12.  * The above copyright notice and this permission notice shall be 
  13.  * included in all copies or substantial portions of the Software. 
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
  17.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  18.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
  19.  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
  20.  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
  21.  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
  22.  */  
  23.   
  24. package com.example.textdrawable.drawable;  
  25.   
  26. import android.content.Context;  
  27. import android.content.res.ColorStateList;  
  28. import android.content.res.Resources;  
  29. import android.content.res.TypedArray;  
  30. import android.graphics.Canvas;  
  31. import android.graphics.Color;  
  32. import android.graphics.ColorFilter;  
  33. import android.graphics.Paint;  
  34. import android.graphics.Path;  
  35. import android.graphics.Rect;  
  36. import android.graphics.Typeface;  
  37. import android.graphics.drawable.Drawable;  
  38. import android.text.Layout;  
  39. import android.text.StaticLayout;  
  40. import android.text.TextPaint;  
  41. import android.util.TypedValue;  
  42.   
  43. /** 
  44.  * A Drawable object that draws text. 
  45.  * A TextDrawable accepts most of the same parameters that can be applied to 
  46.  * {@link android.widget.TextView} for displaying and formatting text. 
  47.  * 
  48.  * Optionally, a {@link Path} may be supplied on which to draw the text. 
  49.  * 
  50.  * A TextDrawable has an intrinsic size equal to that required to draw all 
  51.  * the text it has been supplied, when possible.  In cases where a {@link Path} 
  52.  * has been supplied, the caller must explicitly call 
  53.  * {@link #setBounds(android.graphics.Rect) setBounds()} to provide the Drawable 
  54.  * size based on the Path constraints. 
  55.  */  
  56. public class TextDrawable extends Drawable {  
  57.   
  58.     /* Platform XML constants for typeface */  
  59.     private static final int SANS = 1;  
  60.     private static final int SERIF = 2;  
  61.     private static final int MONOSPACE = 3;  
  62.   
  63.     /* Resources for scaling values to the given device */  
  64.     private Resources mResources;  
  65.     /* Paint to hold most drawing primitives for the text */  
  66.     private TextPaint mTextPaint;  
  67.     /* Layout is used to measure and draw the text */  
  68.     private StaticLayout mTextLayout;  
  69.     /* Alignment of the text inside its bounds */  
  70.     private Layout.Alignment mTextAlignment = Layout.Alignment.ALIGN_NORMAL;  
  71.     /* Optional path on which to draw the text */  
  72.     private Path mTextPath;  
  73.     /* Stateful text color list */  
  74.     private ColorStateList mTextColors;  
  75.     /* Container for the bounds to be reported to widgets */  
  76.     private Rect mTextBounds;  
  77.     /* Text string to draw */  
  78.     private CharSequence mText = "";  
  79.   
  80.     /* Attribute lists to pull default values from the current theme */  
  81.     private static final int[] themeAttributes = {  
  82.             android.R.attr.textAppearance  
  83.     };  
  84.     private static final int[] appearanceAttributes = {  
  85.             android.R.attr.textSize,  
  86.             android.R.attr.typeface,  
  87.             android.R.attr.textStyle,  
  88.             android.R.attr.textColor  
  89.     };  
  90.   
  91.   
  92.     public TextDrawable(Context context) {  
  93.         super();  
  94.         //Used to load and scale resource items  
  95.         mResources = context.getResources();  
  96.         //Definition of this drawables size  
  97.         mTextBounds = new Rect();  
  98.         //Paint to use for the text  
  99.         mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);  
  100.         mTextPaint.density = mResources.getDisplayMetrics().density;  
  101.         mTextPaint.setDither(true);  
  102.   
  103.         int textSize = 15;  
  104.         ColorStateList textColor = null;  
  105.         int styleIndex = -1;  
  106.         int typefaceIndex = -1;  
  107.   
  108.         //Set default parameters from the current theme  
  109.         TypedArray a = context.getTheme().obtainStyledAttributes(themeAttributes);  
  110.         int appearanceId = a.getResourceId(0, -1);  
  111.         a.recycle();  
  112.   
  113.         TypedArray ap = null;  
  114.         if (appearanceId != -1) {  
  115.             ap = context.obtainStyledAttributes(appearanceId, appearanceAttributes);  
  116.         }  
  117.         if (ap != null) {  
  118.             for (int i=0; i < ap.getIndexCount(); i++) {  
  119.                 int attr = ap.getIndex(i);  
  120.                 switch (attr) {  
  121.                     case 0//Text Size  
  122.                         textSize = a.getDimensionPixelSize(attr, textSize);  
  123.                         break;  
  124.                     case 1//Typeface  
  125.                         typefaceIndex = a.getInt(attr, typefaceIndex);  
  126.                         break;  
  127.                     case 2//Text Style  
  128.                         styleIndex = a.getInt(attr, styleIndex);  
  129.                         break;  
  130.                     case 3//Text Color  
  131.                         textColor = a.getColorStateList(attr);  
  132.                         break;  
  133.                     default:  
  134.                         break;  
  135.                 }  
  136.             }  
  137.   
  138.             ap.recycle();  
  139.         }  
  140.   
  141.         setTextColor(textColor != null ? textColor : ColorStateList.valueOf(0xFF000000));  
  142.         setRawTextSize(textSize);  
  143.   
  144.         Typeface tf = null;  
  145.         switch (typefaceIndex) {  
  146.             case SANS:  
  147.                 tf = Typeface.SANS_SERIF;  
  148.                 break;  
  149.   
  150.             case SERIF:  
  151.                 tf = Typeface.SERIF;  
  152.                 break;  
  153.   
  154.             case MONOSPACE:  
  155.                 tf = Typeface.MONOSPACE;  
  156.                 break;  
  157.         }  
  158.   
  159.         setTypeface(tf, styleIndex);  
  160.     }  
  161.   
  162.   
  163.     /** 
  164.      * Set the text that will be displayed 
  165.      * @param text Text to display 
  166.      */  
  167.     public void setText(CharSequence text) {  
  168.         if (text == null) text = "";  
  169.   
  170.         mText = text;  
  171.   
  172.         measureContent();  
  173.     }  
  174.   
  175.     /** 
  176.      * Return the text currently being displayed 
  177.      */  
  178.     public CharSequence getText() {  
  179.         return mText;  
  180.     }  
  181.   
  182.     /** 
  183.      * Return the current text size, in pixels 
  184.      */  
  185.     public float getTextSize() {  
  186.         return mTextPaint.getTextSize();  
  187.     }  
  188.   
  189.     /** 
  190.      * Set the text size.  The value will be interpreted in "sp" units 
  191.      * @param size Text size value, in sp 
  192.      */  
  193.     public void setTextSize(float size) {  
  194.         setTextSize(TypedValue.COMPLEX_UNIT_SP, size);  
  195.     }  
  196.   
  197.     /** 
  198.      * Set the text size, using the supplied complex units 
  199.      * @param unit Units for the text size, such as dp or sp 
  200.      * @param size Text size value 
  201.      */  
  202.     public void setTextSize(int unit, float size) {  
  203.         float dimension = TypedValue.applyDimension(unit, size,  
  204.                 mResources.getDisplayMetrics());  
  205.         setRawTextSize(dimension);  
  206.     }  
  207.   
  208.     /* 
  209.      * Set the text size, in raw pixels 
  210.      */  
  211.     private void setRawTextSize(float size) {  
  212.         if (size != mTextPaint.getTextSize()) {  
  213.             mTextPaint.setTextSize(size);  
  214.   
  215.             measureContent();  
  216.         }  
  217.     }  
  218.   
  219.     /** 
  220.      * Return the horizontal stretch factor of the text 
  221.      */  
  222.     public float getTextScaleX() {  
  223.         return mTextPaint.getTextScaleX();  
  224.     }  
  225.   
  226.     /** 
  227.      * Set the horizontal stretch factor of the text 
  228.      * @param size Text scale factor 
  229.      */  
  230.     public void setTextScaleX(float size) {  
  231.         if (size != mTextPaint.getTextScaleX()) {  
  232.             mTextPaint.setTextScaleX(size);  
  233.             measureContent();  
  234.         }  
  235.     }  
  236.   
  237.     /** 
  238.      * Return the current text alignment setting 
  239.      */  
  240.     public Layout.Alignment getTextAlign() {  
  241.         return mTextAlignment;  
  242.     }  
  243.   
  244.     /** 
  245.      * Set the text alignment.  The alignment itself is based on the text layout direction. 
  246.      * For LTR text NORMAL is left aligned and OPPOSITE is right aligned. 
  247.      * For RTL text, those alignments are reversed. 
  248.      * @param align Text alignment value.  Should be set to one of: 
  249.      * 
  250.      *   {@link Layout.Alignment#ALIGN_NORMAL}, 
  251.      *   {@link Layout.Alignment#ALIGN_NORMAL}, 
  252.      *   {@link Layout.Alignment#ALIGN_OPPOSITE}. 
  253.      */  
  254.     public void setTextAlign(Layout.Alignment align) {  
  255.         if (mTextAlignment != align) {  
  256.             mTextAlignment = align;  
  257.             measureContent();  
  258.         }  
  259.     }  
  260.   
  261.     /** 
  262.      * Sets the typeface and style in which the text should be displayed. 
  263.      * Note that not all Typeface families actually have bold and italic 
  264.      * variants, so you may need to use 
  265.      * {@link #setTypeface(Typeface, int)} to get the appearance 
  266.      * that you actually want. 
  267.      */  
  268.     public void setTypeface(Typeface tf) {  
  269.         if (mTextPaint.getTypeface() != tf) {  
  270.             mTextPaint.setTypeface(tf);  
  271.   
  272.             measureContent();  
  273.         }  
  274.     }  
  275.   
  276.     /** 
  277.      * Sets the typeface and style in which the text should be displayed, 
  278.      * and turns on the fake bold and italic bits in the Paint if the 
  279.      * Typeface that you provided does not have all the bits in the 
  280.      * style that you specified. 
  281.      * 
  282.      */  
  283.     public void setTypeface(Typeface tf, int style) {  
  284.         if (style > 0) {  
  285.             if (tf == null) {  
  286.                 tf = Typeface.defaultFromStyle(style);  
  287.             } else {  
  288.                 tf = Typeface.create(tf, style);  
  289.             }  
  290.   
  291.             setTypeface(tf);  
  292.             // now compute what (if any) algorithmic styling is needed  
  293.             int typefaceStyle = tf != null ? tf.getStyle() : 0;  
  294.             int need = style & ~typefaceStyle;  
  295.             mTextPaint.setFakeBoldText((need & Typeface.BOLD) != 0);  
  296.             mTextPaint.setTextSkewX((need & Typeface.ITALIC) != 0 ? -0.25f : 0);  
  297.         } else {  
  298.             mTextPaint.setFakeBoldText(false);  
  299.             mTextPaint.setTextSkewX(0);  
  300.             setTypeface(tf);  
  301.         }  
  302.     }  
  303.   
  304.     /** 
  305.      * Return the current typeface and style that the Paint 
  306.      * using for display. 
  307.      */  
  308.     public Typeface getTypeface() {  
  309.         return mTextPaint.getTypeface();  
  310.     }  
  311.   
  312.     /** 
  313.      * Set a single text color for all states 
  314.      * @param color Color value such as {@link Color#WHITE} or {@link Color#argb(int, int, int, int)} 
  315.      */  
  316.     public void setTextColor(int color) {  
  317.         setTextColor(ColorStateList.valueOf(color));  
  318.     }  
  319.   
  320.     /** 
  321.      * Set the text color as a state list 
  322.      * @param colorStateList ColorStateList of text colors, such as inflated from an R.color resource 
  323.      */  
  324.     public void setTextColor(ColorStateList colorStateList) {  
  325.         mTextColors = colorStateList;  
  326.         updateTextColors(getState());  
  327.     }  
  328.   
  329.     /** 
  330.      * Optional Path object on which to draw the text.  If this is set, 
  331.      * TextDrawable cannot properly measure the bounds this drawable will need. 
  332.      * You must call {@link #setBounds(int, int, int, int) setBounds()} before 
  333.      * applying this TextDrawable to any View. 
  334.      * 
  335.      * Calling this method with <code>null</code> will remove any Path currently attached. 
  336.      */  
  337.     public void setTextPath(Path path) {  
  338.         if (mTextPath != path) {  
  339.             mTextPath = path;  
  340.             measureContent();  
  341.         }  
  342.     }  
  343.   
  344.     /** 
  345.      * Internal method to take measurements of the current contents and apply 
  346.      * the correct bounds when possible. 
  347.      */  
  348.     private void measureContent() {  
  349.         //If drawing to a path, we cannot measure intrinsic bounds  
  350.         //We must resly on setBounds being called externally  
  351.         if (mTextPath != null) {  
  352.             //Clear any previous measurement  
  353.             mTextLayout = null;  
  354.             mTextBounds.setEmpty();  
  355.         } else {  
  356.             //Measure text bounds  
  357.             float desired = Layout.getDesiredWidth(mText, mTextPaint);  
  358.             mTextLayout = new StaticLayout(mText, mTextPaint, (int)desired,  
  359.                     mTextAlignment, 1.0f, 0.0f, false);  
  360.             mTextBounds.set(00, mTextLayout.getWidth(), mTextLayout.getHeight());  
  361.         }  
  362.   
  363.         //We may need to be redrawn  
  364.         invalidateSelf();  
  365.     }  
  366.   
  367.     /** 
  368.      * Internal method to apply the correct text color based on the drawable's state 
  369.      */  
  370.     private boolean updateTextColors(int[] stateSet) {  
  371.         int newColor = mTextColors.getColorForState(stateSet, Color.WHITE);  
  372.         if (mTextPaint.getColor() != newColor) {  
  373.             mTextPaint.setColor(newColor);  
  374.             return  true;  
  375.         }  
  376.   
  377.         return false;  
  378.     }  
  379.   
  380.     @Override  
  381.     protected void onBoundsChange(Rect bounds) {  
  382.         //Update the internal bounds in response to any external requests  
  383.         mTextBounds.set(bounds);  
  384.     }  
  385.   
  386.     @Override  
  387.     public boolean isStateful() {  
  388.         /* 
  389.          * The drawable's ability to represent state is based on 
  390.          * the text color list set 
  391.          */  
  392.         return mTextColors.isStateful();  
  393.     }  
  394.   
  395.     @Override  
  396.     protected boolean onStateChange(int[] state) {  
  397.         //Upon state changes, grab the correct text color  
  398.         return updateTextColors(state);  
  399.     }  
  400.   
  401.     @Override  
  402.     public int getIntrinsicHeight() {  
  403.         //Return the vertical bounds measured, or -1 if none  
  404.         if (mTextBounds.isEmpty()) {  
  405.             return -1;  
  406.         } else {  
  407.             return (mTextBounds.bottom - mTextBounds.top);  
  408.         }  
  409.     }  
  410.   
  411.     @Override  
  412.     public int getIntrinsicWidth() {  
  413.         //Return the horizontal bounds measured, or -1 if none  
  414.         if (mTextBounds.isEmpty()) {  
  415.             return -1;  
  416.         } else {  
  417.             return (mTextBounds.right - mTextBounds.left);  
  418.         }  
  419.     }  
  420.   
  421.     @Override  
  422.     public void draw(Canvas canvas) {  
  423.         if (mTextPath == null) {  
  424.             //Allow the layout to draw the text  
  425.             mTextLayout.draw(canvas);  
  426.         } else {  
  427.             //Draw directly on the canvas using the supplied path  
  428.             canvas.drawTextOnPath(mText.toString(), mTextPath, 00, mTextPaint);  
  429.         }  
  430.     }  
  431.   
  432.     @Override  
  433.     public void setAlpha(int alpha) {  
  434.         if (mTextPaint.getAlpha() != alpha) {  
  435.             mTextPaint.setAlpha(alpha);  
  436.         }  
  437.     }  
  438.   
  439.     @Override  
  440.     public int getOpacity() {  
  441.         return mTextPaint.getAlpha();  
  442.     }  
  443.   
  444.     @Override  
  445.     public void setColorFilter(ColorFilter cf) {  
  446.         if (mTextPaint.getColorFilter() != cf) {  
  447.             mTextPaint.setColorFilter(cf);  
  448.         }  
  449.     }  
  450.   
  451. }  


用法:
Java代码 复制代码 收藏代码
  1. /** 
  2.  * Copyright (c) 2012 Wireless Designs, LLC 
  3.  * 
  4.  * Permission is hereby granted, free of charge, to any person obtaining 
  5.  * a copy of this software and associated documentation files (the 
  6.  * "Software"), to deal in the Software without restriction, including 
  7.  * without limitation the rights to use, copy, modify, merge, publish, 
  8.  * distribute, sublicense, and/or sell copies of the Software, and to 
  9.  * permit persons to whom the Software is furnished to do so, subject to 
  10.  * the following conditions: 
  11.  * 
  12.  * The above copyright notice and this permission notice shall be 
  13.  * included in all copies or substantial portions of the Software. 
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
  17.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  18.  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
  19.  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
  20.  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
  21.  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
  22.  */  
  23.   
  24. package com.example.textdrawable;  
  25.   
  26. import android.app.Activity;  
  27. import android.graphics.Color;  
  28. import android.graphics.Path;  
  29. import android.graphics.drawable.ClipDrawable;  
  30. import android.os.Bundle;  
  31. import android.os.Handler;  
  32. import android.text.Layout;  
  33. import android.util.TypedValue;  
  34. import android.view.Gravity;  
  35. import android.widget.Button;  
  36. import android.widget.EditText;  
  37. import android.widget.ImageView;  
  38. import com.example.textdrawable.drawable.TextDrawable;  
  39.   
  40. public class MyActivity extends Activity implements Runnable {  
  41.     //Row One  
  42.     ImageView mImageOne, mImageTwo;  
  43.     //Row Two  
  44.     ImageView mImageThree, mImageFour;  
  45.     //Row Three  
  46.     ImageView mImageFive;  
  47.     //Row Four  
  48.     EditText mEditText;  
  49.     //Row Five  
  50.     Button mButton;  
  51.   
  52.     @Override  
  53.     public void onCreate(Bundle savedInstanceState) {  
  54.         super.onCreate(savedInstanceState);  
  55.         setContentView(R.layout.main);  
  56.   
  57.         mImageOne = (ImageView) findViewById(R.id.image1);  
  58.         mImageTwo = (ImageView) findViewById(R.id.image2);  
  59.         mImageThree = (ImageView) findViewById(R.id.image3);  
  60.         mImageFour = (ImageView) findViewById(R.id.image4);  
  61.         mImageFive = (ImageView) findViewById(R.id.image5);  
  62.         mEditText = (EditText) findViewById(R.id.edittext1);  
  63.         mButton = (Button) findViewById(R.id.button1);  
  64.   
  65.         loadDrawables();  
  66.     }  
  67.   
  68.     @Override  
  69.     protected void onResume() {  
  70.         super.onResume();  
  71.         //Start the animation  
  72.         mAnimator.post(this);  
  73.     }  
  74.   
  75.     @Override  
  76.     protected void onPause() {  
  77.         super.onPause();  
  78.         //Stop the animation  
  79.         mAnimator.removeCallbacksAndMessages(null);  
  80.     }  
  81.   
  82.     private Handler mAnimator = new Handler();  
  83.     private int mLevel = 0;  
  84.     @Override  
  85.     public void run() {  
  86.         mLevel = (mLevel += 25) % 10000;  
  87.         mImageFive.setImageLevel(mLevel);  
  88.   
  89.         mAnimator.postDelayed(this10);  
  90.     }  
  91.   
  92.     private void loadDrawables() {  
  93.         /* 
  94.          * Create a simple TextDrawable with all default settings. 
  95.          * Text display settings will be pulled from the theme. 
  96.          */  
  97.         TextDrawable d = new TextDrawable(this);  
  98.         d.setText("SAMPLE TEXT\nLINE TWO");  
  99.         d.setTextAlign(Layout.Alignment.ALIGN_CENTER);  
  100.         //Apply to two ImageViews with different scale types  
  101.         mImageOne.setImageDrawable(d);  
  102.         mImageTwo.setImageDrawable(d);  
  103.   
  104.         /* 
  105.          * Create a second TextDrawable with a path applied. 
  106.          * Applying a path negates implicit bounds measurement, so you 
  107.          * must explicitly call setBounds() with a size that covers your 
  108.          * path constraint. 
  109.          */  
  110.         d = new TextDrawable(this);  
  111.         d.setText("TEXT DRAWN IN A CIRCLE");  
  112.         //Customize text size and color  
  113.         d.setTextColor(Color.BLUE);  
  114.         d.setTextSize(12);  
  115.   
  116.         /* 
  117.          * Create a circular path on which to draw the text.  It's best to 
  118.          * keep all path values positive, so the circle is centered such that 
  119.          * it's radius keeps the circle from going below (0, 0). 
  120.          * 
  121.          * We are using complex units so the values scale appropriately to different displays 
  122.          */  
  123.         Path p = new Path();  
  124.         int origin = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,  
  125.                 40, getResources().getDisplayMetrics());  
  126.         int radius = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,  
  127.                 30, getResources().getDisplayMetrics());  
  128.         int bound = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,  
  129.                 80, getResources().getDisplayMetrics());  
  130.         p.addCircle(origin, origin, radius, Path.Direction.CW);  
  131.         d.setTextPath(p);  
  132.         //Must call setBounds() since we are using a Path  
  133.         d.setBounds(00, bound, bound);  
  134.         //Apply to two ImageViews with different scale types  
  135.         mImageThree.setImageDrawable(d);  
  136.         mImageFour.setImageDrawable(d);  
  137.   
  138.         /* 
  139.          * Wrap a simple TextDrawable in a ClipDrawable to animate.  One convenient 
  140.          * advantage of ImageView is we can call setImageLevel() on the view itself to affect 
  141.          * the Drawable content. 
  142.          */  
  143.         d = new TextDrawable(this);  
  144.         d.setText("SHOW ME TEXT");  
  145.         ClipDrawable clip = new ClipDrawable(d, Gravity.LEFT, ClipDrawable.HORIZONTAL);  
  146.         mImageFive.setImageDrawable(clip);  
  147.   
  148.         /* 
  149.          * Construct a simple TextDrawable to act as a static prefix 
  150.          * inside of an EditText element 
  151.          */  
  152.         d = new TextDrawable(this);  
  153.         d.setText("SKU#");  
  154.         mEditText.setCompoundDrawablesWithIntrinsicBounds(d, nullnullnull);  
  155.   
  156.         /* 
  157.          * Create another simple TextDrawable that will be applied to a TextView 
  158.          * as the left and right drawable instance.  The instance is also clickable, 
  159.          * so the drawable text colors will change with state. 
  160.          */  
  161.         d = new TextDrawable(this);  
  162.         d.setText("TEXT\nDRAW");  
  163.         //Enlarge the text size  
  164.         d.setTextSize(20);  
  165.         //Set the text colors from a resource list  
  166.         d.setTextColor(getResources().getColorStateList(R.color.text_colors));  
  167.         //Center align the text  
  168.         d.setTextAlign(Layout.Alignment.ALIGN_CENTER);  
  169.         //Apply to a TextView, using the intrinsic bounds method  
  170.         // TextDrawable internally calculates the required size.  
  171.         mButton.setCompoundDrawablesWithIntrinsicBounds(d, null, d, null);  
  172.     }  
  173. }  


布局:
Xml代码 复制代码 收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:stretchColumns="*"  
  6.     android:padding="5dp">  
  7.   
  8.     <TableRow>  
  9.         <ImageView  
  10.             android:id="@+id/image1"  
  11.             android:layout_width="match_parent"  
  12.             android:layout_height="100dp"  
  13.             android:scaleType="center"/>  
  14.         <ImageView  
  15.             android:id="@+id/image2"  
  16.             android:layout_width="match_parent"  
  17.             android:layout_height="100dp"  
  18.             android:scaleType="centerCrop"/>  
  19.     </TableRow>  
  20.   
  21.     <TableRow>  
  22.         <ImageView  
  23.             android:id="@+id/image3"  
  24.             android:layout_width="match_parent"  
  25.             android:layout_height="100dp"  
  26.             android:scaleType="center" />  
  27.         <ImageView  
  28.             android:id="@+id/image4"  
  29.             android:layout_width="match_parent"  
  30.             android:layout_height="100dp"  
  31.             android:scaleType="fitCenter" />  
  32.     </TableRow>  
  33.   
  34.     <ImageView  
  35.         android:id="@+id/image5"  
  36.         android:layout_width="match_parent"  
  37.         android:layout_height="100dp"  
  38.         android:scaleType="fitCenter" />  
  39.   
  40.     <EditText  
  41.         android:id="@+id/edittext1"  
  42.         android:layout_width="match_parent"  
  43.         android:layout_height="wrap_content"  
  44.         android:inputType="phone"  
  45.         android:digits="0123456789"  
  46.         android:imeOptions="actionDone"/>  
  47.   
  48.     <Button  
  49.         android:id="@+id/button1"  
  50.         android:layout_width="match_parent"  
  51.         android:layout_height="wrap_content"  
  52.         android:text="Click Me" />  
  53.   
  54. </TableLayout>  
  • textdrawable-master.zip (27.9 KB)
  • 下载次数: 1
  • 大小: 59.9 KB
  • 查看图片附件

0 0