【Android开发学习41】Android 将文字转为为图片drawText

项目介绍:

将android 项目assets中的1.txt文档中的文字转换成一张图片

第一步:创建工具类,用于读取文档,存入一个String数组, 代码如下:

package test.com;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class TextProperty {
		private int heigt;		//读入文本的行数
		private String []context = new String[1024];	//存储读入的文本  
		/*
		 *@parameter wordNum  设置每行显示的字数
		 * 构造函数将文本读入,将每行字符串切割成小于等于35个字符的字符串  存入字符数组
		 * 
		 */
		public TextProperty(int wordNum ,InputStreamReader in) throws Exception {
		int i=0;
		BufferedReader br = new BufferedReader(in);
		String s;
		while((s=br.readLine())!=null){	
			//读入时去掉  空白的行  
//			if(s.trim().equals("")){
//				continue;
//			}
			 if(s.length()>wordNum){
			   int k=0;
			   while(k+wordNum<=s.length()){
				 context[i++] = s.substring(k, k+wordNum);
				   k=k+wordNum;
			   }
			   context[i++] = s.substring(k,s.length());
			}
			else{
				context[i++]=s;
			}
		}
		this.heigt = i;
		in.close();
		br.close();
		}


		public int getHeigt() {
			return heigt;
		}

		public String[] getContext() {
			
			return context;
		}
		

		
}


 

第二步:读入具体文档,将之转换成图片:

package test.com;



import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class TestActivity extends Activity {
    private Button btn;
    private final int WORDNUM = 35;  //转化成图片时  每行显示的字数
    private final int WIDTH = 450;   //设置图片的宽度
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.ok);
        btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				int x=5,y=10;
				try {
				TextProperty tp = new TextProperty(WORDNUM, new InputStreamReader(getResources().getAssets().open("1.txt")));
				Bitmap bitmap = Bitmap.createBitmap(WIDTH, 20*tp.getHeigt(), Config.ARGB_8888);
				Canvas canvas = new Canvas(bitmap);
				Paint paint = new Paint();
				String [] ss = tp.getContext();
					for(int i=0;i<tp.getHeigt();i++){
					canvas.drawText(ss[i], x, y, paint);
					y=y+20;
					}
				
				canvas.save(Canvas.ALL_SAVE_FLAG);
				canvas.restore();
				String path = Environment.getExternalStorageDirectory() + "/image.png";
			    System.out.println(path);
					FileOutputStream os = new FileOutputStream(new File(path));
					bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
					os.flush();
					os.close();
				}
				   catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
		});
    }
}

 

 

转化显示效果:

 


项目下载:点击这里

转载自: http://blog.csdn.net/msl0903/article/details/7872392
 
 

本文博客源地址:http://blog.csdn.net/ypist

 

 

 

你可能感兴趣的:(android,canvas,bitmap,drawText)