Android字符串进阶

来源:互联网 发布:虾囧笑话源码v4.0 编辑:程序博客网 时间:2024/06/08 11:20

一、特殊字符的输入

本文按照遇到问题的思路展开:

(一) 在Res下String.xml向字符串中插入“&”符号报错

如下图所示:

翻译:在对实体的引用中,实体名必须紧跟在“&”后。

查找知道:这设计到HTML语言的字符集知识:

在网页中除了可显示常见的ASCII字符和汉字外,HTML还有许多特殊字符,它们一起构成了HTML字符集。有2种情况需要使用特殊字符,一是网页中有其特殊意义的字符,二是键盘上没有的字符。 字符集HTML字符可以用一些代码来表示,代码可以有2种表示方式。即字符代码(命名实体)和数字代码(编号实体)。字符代码以&符开始,以分号结束,其间是字符名;数字代码也以&符开始,以分号结束,其间是#号加编号。示例见下图,完整的html字符集在文后Excel附件中,并非全部,仅常用。

(二) 这涉及到了Android对此有专门的处理

文档中有专门说明,如下图所示:

字符串可以包含风格标签(styling tag),需要注意的是:你必须转码(escaping)缩写号( apostrophe即’)和引用号(quotation mark 即”或’)。你可风格化(style)和格式化(format)字符串。

1, 对缩写号和引号的处理

文档示例如下:

正确的转码是:

A:用双引号将字符串全部圈住

B:使用转义符号“\”

错误做法:

A:不处理

错误如下图所示:

正确方法见上正确转码

B:使用html的字符代码代替缩写号

错误如下图所示:

对以上错误的修正:

Note:因为xml并不是完全实时编译,所以可以错误的xml语句并不当时报错,但当项目启动时,会报错。

2, 格式化字符串(format string)

即字符串中保留参数位作为模板,可以传入变量,转换。eg,SimpleDateFormat

模板为:yyyy-MM-dd,传入Date,得到1999-10-10

String.xml文件中代码如下:

  1. <string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string> 

Java中代码如下:

  1. Resources res = getResources(); 
  2. String text = String.format(res.getString(R.string.welcome_messages), username, mailCount); 

有必要说明的是:

%:产生字面值,貌似是这个意思。

1$:代表第一个参数

2$:代表第二个参数

s:参数类型是字符串

d:参数类型是数字

More:http://dlc.sun.com.edgesuite.net/jdk/jdk-api-localizations/jdk-api-zh-cn/builds/latest/html/zh_CN/api/java/util/Formatter.html#syntax

Java代码如下:

3, 在字符串中使用Html标记风格符号(Html makeup)

即Html的字符代码。一步一步,终于排除到问题的可能解答处。

1. Android支持的Html元素,如下图所示:

这三个字体标签,能够直接使用,称之为“styled text”。<b>内即为黑体字例如:

  1. <string name="welcome">Welcome to <b>Android</b>!</string> 

但是如果将以上的“<”分解,使用Html字符代码的话,用法将有所不同,这两个字符代码:

l “&lt;”对应的字符代码为:&lt;

l “>”对应的字符代码为:&gt;

如下:

  1. <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string> 

称之为“HTML-escaped”text,因为最终文本的显示要为styled text,所以需要将Html-styled text转为 styled text,调用fromHtml()方法。代码如下:

  1. Resources res = getResources(); 
  2. String text = String.format(res.getString(R.string.welcome_messages), username, mailCount); 
  3. CharSequence styledText = Html.fromHtml(text); 

因为fromHtml()方法转换的对象是html-styled,所以调用此方法之前,必须使用文本格式化(formated text)和TextUtil.htmlEncode()方法,确保所有的可能html字符已经被转码(escape)。如果代码中含有特殊字符“&”“&lt;”等,必须调用以上方法。代码如下:

  1. String escapedUsername = TextUtil.htmlEncode(username); 
  2.  
  3. Resources res = getResources(); 
  4. String text = String.format(res.getString(R.string.welcome_messages), escapedUsername, mailCount); 
  5. CharSequence styledText = Html.fromHtml(text); 
  6.  

 

测试:如果name中含有html character,不转为html-styled,会有什么情况发生。

xml中字符串format如下:

  1. <string name="welcome_messages">Hello, %1$s! You have &lt;b>%2$d new messages&lt;/b>.</string> 

 

测试代码如下:

  1. public void onCreate(Bundle savedInstanceState) { 
  2.         super.onCreate(savedInstanceState); 
  3.         setContentView(R.layout.main); 
  4.          
  5.         Resources rs = getResources(); 
  6.         mTextView1 = (TextView) findViewById(R.id.textView1); 
  7.         mTextvView2 = (TextView) findViewById(R.id.textView2); 
  8.          
  9.         String name = new  String("<Mike>"); 
  10.         int count = 12345
  11.  
  12.         //未转为html-styled 
  13.         format1 = String.format(rs.getString(R.string.welcome_messages), name,count); 
  14.         CharSequence styledText1 = Html.fromHtml(format1); 
  15.         mTextView1.setText(styledText1); 
  16.          
  17.         //转为html-styled 
  18.         format2 = String.format(rs.getString(R.string.welcome_messages), TextUtils.htmlEncode(name),count); 
  19.         CharSequence styledText2 = Html.fromHtml(format2); 
  20.         mTextvView2.setText(styledText2); 
  21.              
  22.     } 

模拟器显示如下:

2. 进入TextUtil类中,htmlEncode()方法做格式化字符代码的转换。且Android中,只接受以上五种特殊字符。代码如下:

  1. /** 
  2.      * Html-encode the string. 
  3.      * @param s the string to be encoded 
  4.      * @return the encoded string 
  5.      */ 
  6.     public static String htmlEncode(String s) { 
  7.         StringBuilder sb = new StringBuilder(); 
  8.         char c; 
  9.         for (int i = 0; i < s.length(); i++) { 
  10.             c = s.charAt(i); 
  11.             switch (c) { 
  12.             case '<'
  13.                 sb.append("&lt;"); //$NON-NLS-1$ 
  14.                 break
  15.             case '>'
  16.                 sb.append("&gt;"); //$NON-NLS-1$ 
  17.                 break
  18.             case '&'
  19.                 sb.append("&amp;"); //$NON-NLS-1$ 
  20.                 break
  21.             case '\''
  22.                 sb.append("&apos;"); //$NON-NLS-1$ 
  23.                 break
  24.             case '"'
  25.                 sb.append("&quot;"); //$NON-NLS-1$ 
  26.                 break
  27.             default
  28.                 sb.append(c); 
  29.             } 
  30.         } 
  31.         return sb.toString(); 
  32.     } 

(三) 源码中

有三处出现htmlEncode()方法。

如下图所示:

第一处:即上文提到的TextUtils类

第二处: TextUtils的测试类,暂时不知道有什么用处

第三处:XmlParser类

定位到代码,如下:

向上查看代码块描述:

显然和之上的分析吻合。这些方法是对xml的输出,输出需要标准化,即 被未来的styled text(或者是其他)准确转换convert。

(四) 流程图

 

 

 

 

(五) 单复数的处理(string format 引入的问题)

文档中说明如下:

测试各个关键字的效果如何。

Xml中定义plurals,如下:

  1. <plurals name="numberOfSongsAvailable"> 
  2.         <item quantity="zero">Zero song found.</item> 
  3.         <item quantity="one">One song found.</item> 
  4.         <item quantity="two">Two song found.</item> 
  5.         <item quantity="few">Few song found.</item> 
  6.         <item quantity="other">Other song found.</item> 
  7.         <item quantity="many">Many song found.</item> 
  8.     </plurals> 

代码中如下:

  1. public void onCreate(Bundle savedInstanceState) { 
  2.        super.onCreate(savedInstanceState); 
  3.        setContentView(R.layout.main); 
  4.         
  5.        int count1 = 0
  6.        Resources res = getResources(); 
  7.        String songsFound1 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count1, count1); 
  8.        TextView textView1 = (TextView) findViewById(R.id.textView1); 
  9.        textView1.setText(songsFound1); 
  10.         
  11.        int count2 = 1
  12.        String songsFound2 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count2, count2); 
  13.        TextView textView2 = (TextView) findViewById(R.id.textView2); 
  14.        textView2.setText(songsFound2); 
  15.         
  16.        int count3 = 2
  17.        String songsFound3 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count3, count3); 
  18.        TextView textView3 = (TextView) findViewById(R.id.textView3); 
  19.        textView3.setText(songsFound3); 
  20.         
  21.        int count4 = 3
  22.        String songsFound4 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count4, count4); 
  23.        TextView textView4 = (TextView) findViewById(R.id.textView4); 
  24.        textView4.setText(songsFound4); 
  25.         
  26.        int count5 = 4
  27.        String songsFound5 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count5, count5); 
  28.        TextView textView5 = (TextView) findViewById(R.id.textView5); 
  29.        textView5.setText(songsFound5); 
  30.         
  31.        int count6 = 1000
  32.        String songsFound6 = res.getQuantityString(R.plurals.numberOfSongsAvailable, count6, count6); 
  33.        TextView textView6 = (TextView) findViewById(R.id.textView6); 
  34.        textView6.setText(songsFound6); 
  35.         
  36.         
  37.    } 
 

模拟器显示如下:

即在中文状态下,只支持oneother两个关键字。

 

二、字符及字符串的测量和处理

需求:实现长文字的滚动停留,所以需要将长字符串截取成指定TextView长度的字符串数组 ,然后使用ViewFilpper实现。

分割代码如下:

  1. /* 
  2.  * 根据要求分割字符串 
  3.  */ 
  4. public static String[] getLineStrs(String content, Paint paint, float width, float textSize) { 
  5.     paint.setTextSize(textSize);//Note1:测量的工具首先需要定义单位 
  6.  
  7.     int index = 0
  8.     int start = 0
  9.     int end = 0
  10.      
  11.     float textLength = paint.measureText(content); 
  12.  
  13.     int lineNum = (int) Math.ceil(1.5*textLength / width) ;//Note2:计算行数因为判断的不准确,所以增加余量1.5倍,最后处理 
  14.     String[] mSplitTextParts = new String[lineNum]; 
  15.      
  16.     for (int i = 0; i <= content.length(); i++) { 
  17.         end = i; 
  18.  
  19.         float measureLength = paint.measureText(content, start, end);//Note3:[start,end)范围的字符串 
  20.  
  21.         if (measureLength >= width) { 
  22.                 mSplitTextParts[index] = content.substring(start, end);//Note4:[start,end)范围的字符串 
  23.                 start = end; 
  24.                 index++;     
  25.         } 
  26.  
  27.         if (end == content.length()) { 
  28.             mSplitTextParts[index] = content.substring(start, end); 
  29.             return Arrays.copyOf(mSplitTextParts, index);//Note5:因为行数判断的不准确,所以需要清除掉未赋值的null值字符串 
  30.         } 
  31.     return null
  32.   

猜测分割不准的原因是:中英文混杂。可能是半角全角的问题。

测试后发现:

1, 全英文状态下,半角全角计算的标准行数(未经过1.5倍的余量修正)都ok。

2, 全中文状态下,半角全角计算的标准行数(未经过1.5倍的余量修正)都ok。

3, 在中英文混杂状态下,计算的标准行数比实际截取的行数要大。在这里存疑。有知道的朋友,请留言。


三、字体属性及测量(FontMetrics)

最近的一个模块正好用到字体的相关内容,整理出来。

(一) 字体的几个参数 ,以Android API文档定义为准,见下图

要点如下:

1. 基准点是baseline

2. Ascent是baseline之上至字符最高处的距离

3. Descent是baseline之下至字符最低处的距离

4. Leading文档说的很含糊,其实是上一行字符的descent到下一行的ascent之间的距离

5. Top指的是指的是最高字符到baseline的值,即ascent的最大值

6. 同上,bottom指的是最下字符到baseline的值,即descent的最大值

 

Note:网上有很多错误的图,如果有疑问,就参看文档,区分对错。

 

为了帮助理解,我特此搜索了不同的示意图。对照示意图,会很容易理解FontMetrics的参数。

pic-1

 

pic-2

 

pic-3

 

pic-4

 

pic-5

pic-6

(二) 测试

 1,测试的代码直接使用网上的代码,因为重复着众多,无所给出原始出处,故不注出。

我增加了Bitmap作为输出显示,完整代码如下:

  1. public class FontMetricsDemoActivity extends Activity { 
  2.     private Canvas canvas; 
  3.  
  4.     /** Called when the activity is first created. */ 
  5.     @Override 
  6.     public void onCreate(Bundle savedInstanceState) { 
  7.         super.onCreate(savedInstanceState); 
  8.         setContentView(R.layout.main); 
  9.          
  10.         Paint textPaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  11.         textPaint.setTextSize( 55); 
  12.         textPaint.setColor( Color.WHITE); 
  13.  
  14.         // FontMetrics对象 
  15.         FontMetrics fontMetrics = textPaint.getFontMetrics(); 
  16.         String text = "abcdefghijklmnopqrstu"
  17.          
  18.          
  19.  
  20.         // 计算每一个坐标 
  21.         float baseX = 0
  22.         float baseY = 100
  23.         float topY = baseY + fontMetrics.top; 
  24.         float ascentY = baseY + fontMetrics.ascent; 
  25.         float descentY = baseY + fontMetrics.descent; 
  26.         float bottomY = baseY + fontMetrics.bottom; 
  27.         float leading = baseY + fontMetrics.leading; 
  28.          
  29.          
  30.         Log.d("fontMetrics""baseX    is:" + 0); 
  31.         Log.d("fontMetrics""baseY    is:" + 100); 
  32.         Log.d("fontMetrics""topY     is:" + topY); 
  33.         Log.d("fontMetrics""ascentY  is:" + ascentY); 
  34.         Log.d("fontMetrics""descentY is:" + descentY); 
  35.         Log.d("fontMetrics""bottomY  is:" + bottomY); 
  36.         Log.d("fontMetrics""leading  is:" + leading); 
  37.  
  38.          
  39.          
  40.         Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.fontmetrics); 
  41.         Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 
  42.          
  43.         canvas  = new Canvas(mutableBitmap); 
  44.          
  45.          
  46.  
  47.         // 绘制文本 
  48.         canvas.drawText(text, baseX, baseY, textPaint); 
  49.  
  50.         // BaseLine描画 
  51.         Paint baseLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  52.          
  53.         baseLinePaint.setColor( Color.RED); 
  54.         canvas.drawLine(0, baseY, canvas.getWidth(), baseY, baseLinePaint); 
  55.  
  56.         // Base描画 
  57.         canvas.drawCircle( baseX, baseY, 5, baseLinePaint); 
  58.  
  59.         // TopLine描画 
  60.         Paint topLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  61.         topLinePaint.setColor( Color.LTGRAY); 
  62.         canvas.drawLine(0, topY, canvas.getWidth(), topY, topLinePaint); 
  63.  
  64.         // AscentLine描画 
  65.         Paint ascentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  66.         ascentLinePaint.setColor( Color.GREEN); 
  67.         canvas.drawLine(0, ascentY, canvas.getWidth(), ascentY, ascentLinePaint); 
  68.  
  69.         // DescentLine描画 
  70.         Paint descentLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  71.         descentLinePaint.setColor( Color.YELLOW); 
  72.         canvas.drawLine(0, descentY, canvas.getWidth(), descentY, descentLinePaint); 
  73.  
  74.         // ButtomLine描画 
  75.         Paint bottomLinePaint = new Paint( Paint.ANTI_ALIAS_FLAG); 
  76.         bottomLinePaint.setColor( Color.MAGENTA); 
  77.         canvas.drawLine(0, bottomY, canvas.getWidth(), bottomY, bottomLinePaint);  
  78.          
  79.         ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
  80.         imageView.setImageBitmap(mutableBitmap); 
  81.          
  82.     } 

 log显示如下:

Note1:注意到各个数值都是整数,这是建立在baseY=100的情况下,去掉baseY,重新运行代码,log如下:

Note2: 参照线为baseline,即baseline=0的情况下,其他各线的数值。leading = 0,即行间距=0

2,以上是根据paint设置,获取相关的FontMetrics属性,并且只绘制了一行字符串,我们猜想,如果是多行,是否可以获得行间距leanding,代码如下:

  1. //test_multiply_lines 
  2.         TextView textView = (TextView) findViewById(R.id.textView1); 
  3.         String textMultiLines = "abcdefghijklmnopqrstuabcdefghijklmnopqrstuabcdefghijklmnopqrstuabcdefghijklmnopqrstuabcdefghijklmnopqrstu"
  4.         textView.setTextSize(55); 
  5.         textView.setText(textMultiLines); 
  6.          
  7.         FontMetrics fontMetrics2 = textView.getPaint().getFontMetrics(); 
  8.          
  9.          
  10.  
  11.         // 计算每一个坐标 
  12.  
  13.         float topY = fontMetrics2.top; 
  14.         float ascentY = fontMetrics2.ascent; 
  15.         float descentY =  fontMetrics2.descent; 
  16.         float bottomY = fontMetrics2.bottom; 
  17.         float leading =  fontMetrics2.leading; 
  18.          
  19.          
  20.  
  21.         Log.d("fontMetrics""topY     is:" + topY); 
  22.         Log.d("fontMetrics""ascentY  is:" + ascentY); 
  23.         Log.d("fontMetrics""descentY is:" + descentY); 
  24.         Log.d("fontMetrics""bottomY  is:" + bottomY); 
  25.         Log.d("fontMetrics""leading  is:" + leading); 

log如下:

Note:显然,即使是多行的情况下,仍不能获得leading。

3,如果text是单行,获得各个属性将会怎样,代码如下:

  1. String text = "abcdefghijklmnopqrstu"
  2.         TextView textView = (TextView) findViewById(R.id.textView1); 
  3.         textView.setTextSize(55); 
  4.         textView.setText(text); 
  5.          
  6.         FontMetrics fontMetrics = textView.getPaint().getFontMetrics(); 
  7.  
  8.         // 计算每一个坐标 
  9.         float baseX = 0
  10.         float baseY = 100
  11.         float topY = baseY + fontMetrics.top; 
  12.         float ascentY = baseY + fontMetrics.ascent; 
  13.         float descentY = baseY + fontMetrics.descent; 
  14.         float bottomY = baseY + fontMetrics.bottom; 
  15.         float leading =  fontMetrics.leading; 
  16.          
  17.          
  18.  
  19.         Log.d("fontMetrics""topY     is:" + fontMetrics.top); 
  20.         Log.d("fontMetrics""ascentY  is:" + fontMetrics.ascent); 
  21.         Log.d("fontMetrics""descentY is:" + fontMetrics.descent); 
  22.         Log.d("fontMetrics""bottomY  is:" + fontMetrics.bottom); 
  23.         Log.d("fontMetrics""leading  is:" + fontMetrics.leading); 

log如下图所示:

Note:与多行获得的属性都相同。

 

结论:

A:虽然paint和textView所设置的textSize均为55,且为相同的字符串,但是两个获得的FontMetrics属性值并不相同。但是,我们发现,做除法之后,均为1.5倍关系。做出猜测,即Paint下,为mdpi对应的size,而TextView的size已经关联到了显示屏幕本身的320dip。所以获得属性值均为整1.5倍数

B:各种情况下,均未获得leading值。


本文出自 “小新专栏” 博客,请务必保留此出处http://mikewang.blog.51cto.com/3826268/864801

0 0
原创粉丝点击