http://blog.csdn.net/gf771115/article/details/31354265

http://blog.csdn.net/gf771115/article/details/31354265http://blog.csdn.net/gf771115/article/details/31354265http://blog.csdn.net/gf771115/article/details/31354265http://blog.csdn.net/gf771115/article/details/31354265







Android 如何实现竖排文字显示?

分类:Android/OMS 78人阅读 评论(0) 收藏 举报

转自:http://blog.csdn.net/liaoyp_ios_android/article/details/6949768

  1. 在android.graphics.Canvas类中有个沿路径画字的方法
  2. voiddrawTextOnPath(Stringtext,Pathpath,floathOffset,floatvOffset,Paintpaint)
  3. Drawthetext,withoriginat(x,y),usingthespecifiedpaint,alongthespecifiedpath.
  4. voiddrawTextOnPath(char[]text,intindex,intcount,Pathpath,floathOffset,floatvOffset,Paintpaint)
  5. Drawthetext,withoriginat(x,y),usingthespecifiedpaint,alongthespecifiedpath.
  6. Test.java代码://需要在layout中定义Test,且设置背景,在java代码中设置testText
  7. publicclassTestextendsView{
  8. privatePaintpaint;
  9. privatePathpath;
  10. privateMatrixmatrix;
  11. privateintwidth=-1;
  12. privateintheight=-1;
  13. privatefloatleft=3;
  14. privatefloattop=18;
  15. privateStringtitle="";
  16. BitmapDrawabledrawable=(BitmapDrawable)getBackground();
  17. publicTest(Contextcontext,AttributeSetattrs){
  18. super(context,attrs);
  19. paint=newPaint();
  20. paint.setColor(Color.WHITE);//定义字体颜色
  21. paint.setTextSize(14);//定义字体大小
  22. path=newPath();
  23. path.lineTo(0,500);//定义字符路径
  24. matrix=newMatrix();
  25. Log.v("onMeasure","2");
  26. }
  27. @Override
  28. protectedvoidonDraw(Canvascanvas){
  29. //画背景
  30. Bitmapb=Bitmap.createBitmap(drawable.getBitmap(),0,0,width,height);
  31. canvas.drawBitmap(b,matrix,paint);
  32. //画字
  33. showText(canvas,title);
  34. }
  35. privatevoidshowText(Canvascanvas,Stringtext){
  36. floatw;
  37. finalintlen=text.length();
  38. floatpy=0+top;
  39. for(inti=0;i<len;i++){
  40. charc=text.charAt(i);
  41. w=paint.measureText(text,i,i+1);//获取字符宽度
  42. StringBufferb=newStringBuffer();
  43. b.append(c);
  44. if(py>81){//定义字的范围
  45. return;
  46. }
  47. if(isChinese(c)){
  48. py+=w;
  49. if(py>81){
  50. return;
  51. }
  52. canvas.drawText(b.toString(),left,py,paint);//中文处理方法
  53. }else{
  54. canvas.drawTextOnPath(b.toString(),path,py,-left-2,paint);//其他文字处理方法
  55. py+=w;
  56. }
  57. }
  58. }
  59. publicvoidsetText(Stringtitle){
  60. this.title=title;
  61. }
  62. publicStringgetText(){
  63. returntitle;
  64. }
  65. privatebooleanisChinese(charc){
  66. Character.UnicodeBlockub=Character.UnicodeBlock.of(c);
  67. if(ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
  68. ||ub==Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
  69. ||ub==Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
  70. ||ub==Character.UnicodeBlock.GENERAL_PUNCTUATION
  71. ||ub==Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
  72. ||ub==Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS){
  73. returntrue;
  74. }
  75. returnfalse;
  76. }
  77. //重写View大小方法,使view大小为背景图片大小
  78. @Override
  79. protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
  80. //super.onMeasure(widthMeasureSpec,heightMeasureSpec);
  81. if(null!=getBackground()){
  82. inth=drawable.getIntrinsicHeight();
  83. intw=drawable.getIntrinsicWidth();
  84. Log.v("onMeasure","null!=getBackground()h:"+h+"w:"+w);
  85. width=w;
  86. height=h;
  87. setMeasuredDimension(w,h);
  88. }else{
  89. width=widthMeasureSpec;
  90. height=heightMeasureSpec;
  91. super.measure(widthMeasureSpec,heightMeasureSpec);
  92. }
  93. }
  94. }
  95. 在Android中,若要通过程序改变屏幕显示的方向,必须要覆盖setRequestedOrientation()方法,而若要取得目前的屏幕方向,则需要访问getRequestedOrientation()方法。本范例为求简要示范更改做法,设计了一个按钮,当单击按钮的同时,判断当下的屏幕方向,例如竖排(PORTRAIT),则将其更改为横排(LANDSCAPE);若为横排(LANDSCAPE),则将其更改为竖排(PORTRAIT)

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. android上一些方法的区别和用法的注意事项
  5. android实现字体闪烁动画的方法
  6. Android中dispatchDraw分析
  7. Android四大基本组件介绍与生命周期
  8. Android(安卓)MediaPlayer 常用方法介绍
  9. 在Fragment中设置控件点击方法,执行失败。

随机推荐

  1. Android 用axis2 webService产生java.lan
  2. ADT在线安装
  3. android message机制
  4. [Android] SQLite的使用入门
  5. Android Review
  6. 如何POST一个JSON格式的数据给Restful服
  7. android:onTouch()和onTouchEvent()的区
  8. Android 复制文本
  9. Android(手机)连接电脑本地服务器(flask)
  10. Android 学习笔记8---SAX和DOM解析XML文