我有Android应用程序中的文本图像代码,它正在成功地将文本转换为图像并保存到SD卡中的本地位置。我面临的问题是它不会将完整的文本转换为图像。这里是我提供给textview的文本“这是t ashdf asdhfj sdhkfh shd jshd hsdhfsdjkhfksdjfhsdlhfksldhfklh shdkfjhsdkj hfsdjk kdjhfsk djhfskldh shdjkfhk sjhdfkh”。以下是将其转换为图像Android Text to Image issue
这里是我的代码
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60);
tv.setLayoutParams(layoutParams);
tv.setText("this is t ashdf asdhfj sdhkfh shd jshd hsdhfsdjkhfksdjfhsdlhfksldhfklh shdkfjhsdkj hfsdjk kdjhfsk djhfskldh shdjkfhk sjhdfkh ");
tv.setTextColor(Color.BLACK);
tv.setBackgroundColor(Color.WHITE);
Bitmap testB;
timer = new Timer();
timer.schedule(new TickerTask(), 1000,25);
testB = Bitmap.createBitmap(tv.getText().length(), 20, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(testB);
tv.layout(0, 0, 800, 30);
tv.draw(c);
iv = (ImageView) findViewById(R.id.menuIcon);
x=40;
iv.setPadding(x, 0, 0, 0);
iv.setLayoutParams(layoutParams);
iv.setBackgroundColor(Color.GREEN);
iv.setImageBitmap(testB);
iv.setDrawingCacheEnabled(true);
BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
Bitmap bitmap = drawable.getBitmap();
//Bitmap bitmap = testB;
File sdCardDirectory = Environment.getExternalStorageDirectory();
File image = new File(sdCardDirectory, "test.png");
boolean success = false;
// Encode the file as a PNG image.
FileOutputStream outStream;
try {
outStream = new FileOutputStream(image);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (success) {
Toast.makeText(getApplicationContext(), "Image saved with success"+tv.getText().length(),
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"Error during image saving", Toast.LENGTH_LONG).show();
}
问题不显示。问题是图像的大小应覆盖整个文本并显示在保存的图像中。 testB = Bitmap.createBitmap(tv.getText()。length(),20,Bitmap.Config.ARGB_8888);这是我应该如何指定生成的图像大小与文本大小完全相同的主要问题。 – 2013-03-21 13:18:28
您试图从视图中获取图像(本例中为TextView)。如果这个视图没有足够的空间来容纳它的所有内容,那么将要拍摄的图像将是有空间显示的内容。所以你必须放大你的TextView,所以它可以容纳所有的内容。 – 2013-03-21 13:22:14
如果文本大于TextView,该怎么办?我必须将整个文本转换为图像。有没有一种方法可以将文本直接提供给画布 – 2013-03-21 13:24:40