Android使用StaticLayout实现文本绘制自动换行

来源:互联网 发布:育知同创教育位置 编辑:程序博客网 时间:2024/06/02 23:47

使用的场景主要是绘制文本的时候指定绘制区域的宽度,文本需要根据宽度自动换行。



使用TextPaint和StaticLayout就可以实现这个功能,并可以获得绘制后的文本区域的高度:

 

Java代码  收藏代码
  1. package com.hu.text;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Canvas;  
  5. import android.graphics.Color;  
  6. import android.graphics.Paint;  
  7. import android.graphics.Paint.Style;  
  8. import android.text.Layout.Alignment;  
  9. import android.text.StaticLayout;  
  10. import android.text.TextPaint;  
  11. import android.view.MotionEvent;  
  12. import android.view.View;  
  13.   
  14. import com.example.texttest.R;  
  15.   
  16. public class MyView extends View {  
  17.       
  18.     TextPaint textPaint = null;  
  19.     StaticLayout staticLayout = null;  
  20.     Paint paint = null;  
  21.     int width = 50;  
  22.     int height = 0;  
  23.     String txt = null;  
  24.     boolean running = false;  
  25.   
  26.     public MyView(Context context) {  
  27.         super(context);  
  28.         textPaint = new TextPaint();  
  29.         textPaint.setAntiAlias(true);  
  30.         textPaint.setTextSize(12);  
  31.         txt = getResources().getString(R.string.my_text);  
  32.         staticLayout = new StaticLayout(txt, textPaint, width, Alignment.ALIGN_NORMAL, 10false);  
  33.         height = staticLayout.getHeight();  
  34.         paint = new Paint();  
  35.         paint.setStyle(Style.STROKE);  
  36.         paint.setColor(Color.RED);  
  37.     }  
  38.       
  39.     @Override  
  40.     public boolean onTouchEvent(MotionEvent event) {  
  41.         // TODO Auto-generated method stub  
  42.         switch (event.getAction()) {  
  43.         case MotionEvent.ACTION_DOWN:  
  44.             running = !running;  
  45.             if(running){  
  46.                 new Thread(){  
  47.                     public void run() {  
  48.                         while(running){  
  49.                             width ++;  
  50.                             staticLayout = new StaticLayout(txt, textPaint, width, Alignment.ALIGN_NORMAL, 10false);  
  51.                             height = staticLayout.getHeight();  
  52.                             postInvalidate();  
  53.                             try {  
  54.                                 Thread.sleep(50);  
  55.                             } catch (InterruptedException e) {  
  56.                                 // TODO Auto-generated catch block  
  57.                                 e.printStackTrace();  
  58.                             }  
  59.                             if(width >= 300){  
  60.                                 width = 50;  
  61.                             }  
  62.                         }  
  63.                     };  
  64.                 }.start();  
  65.             }  
  66.             break;  
  67.         default:  
  68.             break;  
  69.         }  
  70.         return super.onTouchEvent(event);  
  71.     }  
  72.   
  73.     @Override  
  74.     protected void onDraw(Canvas canvas) {  
  75.         // TODO Auto-generated method stub  
  76.         canvas.translate(2020);  
  77.         staticLayout.draw(canvas);  
  78.         canvas.drawRect(00, width, height, paint);  
  79.         super.onDraw(canvas);  
  80.     }  
  81.   
  82. }  

 

 完。

  • TextTest.zip (744.8 KB)
  • 下载次数: 3
原文地址:http://hzy3774.iteye.com/blog/2175198
0 0
原创粉丝点击