2013-03-21 100 views
1

我有Android应用程序中的文本图像代码,它正在成功地将文本转换为图像并保存到SD卡中的本地位置。我面临的问题是它不会将完整的文本转换为图像。这里是我提供给textview的文本“这是t ashdf asdhfj sdhkfh shd jshd hsdhfsdjkhfksdjfhsdlhfksldhfklh shdkfjhsdkj hfsdjk kdjhfsk djhfskldh shdjkfhk sjhdfkh”。以下是将其转换为图像enter image description hereAndroid 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(); 
    } 

回答

0

要完全将文本转换为你要的图像获取textview内部文本的宽度而不是textview本身。所以这里是你如何获得文本的宽度。

string texting="Some Text"; 
    Rect bounds = new Rect(); 
    Paint textPaint = tv.getPaint(); 
    textPaint.getTextBounds(texting,0,texting.length(),bounds); 
    int heights= bounds.height(); 
    int widths = bounds.width(); 

现在你可以使用上面的宽度和高度,以创建位图和完整文本(无论多久或小)将被转换为图像。

0

改变该行:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60); 

要这样:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 

要直接画你可以试试这个:

Paint paint = new Paint(); 
paint.setStyle(Paint.Style.FILL); 
paint.setAntiAlias(true); 
paint.setTextSize(30); 
canvas.drawText("Style.FILL", 75, 110, paint); 
+0

问题不显示。问题是图像的大小应覆盖整个文本并显示在保存的图像中。 testB = Bitmap.createBitmap(tv.getText()。length(),20,Bitmap.Config.ARGB_8888);这是我应该如何指定生成的图像大小与文本大小完全相同的主要问题。 – 2013-03-21 13:18:28

+0

您试图从视图中获取图像(本例中为TextView)。如果这个视图没有足够的空间来容纳它的所有内容,那么将要拍摄的图像将是有空间显示的内容。所以你必须放大你的TextView,所以它可以容纳所有的内容。 – 2013-03-21 13:22:14

+0

如果文本大于TextView,该怎么办?我必须将整个文本转换为图像。有没有一种方法可以将文本直接提供给画布 – 2013-03-21 13:24:40

0

我想问题在于你的宽度TextView。你的方法将你的视图的绘制区域转换为位图。如果你的文本的一部分被隐藏,那么确实不会绘制在绘图区域中。为什么你不从TextView获取文本,并将其写入到一个画布,您可以使用此方法:?

canvas.drawText(); 

示例代码:

Paint paint = new Paint(); 
paint.setColor(Color.RED); 
paint.setStyle(Paint.Style.FILL); 

paint.setAntiAlias(true); 

paint.setTextSize(20); 

canvas.drawText("Hello Android! Fill...", 50, 230, paint); 
+0

你可以建议我如何直接提供文字到画布,以便它可以将整个文字转换为图像? – 2013-03-21 13:22:45

+0

@JibranKhan请参阅我的编辑。 – hasanghaforian 2013-03-21 13:33:36

+0

感谢您的代码,请问您可以编辑并将我的代码与您的代码合并?我放弃了困惑,希望文本能够从用户输入中动态生成,并将其转换为精确宽度的图像。 – 2013-03-22 05:44:59