1

我在 android 应用程序中有文本到图像的代码,它成功地将文本转换为图像并保存到 SD 卡中的本地位置。我面临的问题是它没有将完整的文本转换为图像。这是我提供给 textview 的文本“这是 t ashdf asdhfj sdhkfh shd jshd hsdhfsdjkhfksdjfhsdlhfksldhfklh shdkfjhsdkj hfsdjk kdjhfsk djhfskldh shdjkfhk sjhdfkh”。这是它转换为图像的内容在此处输入图像描述

这是我的代码

 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();
    }
4

3 回答 3

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);
于 2013-03-21T13:12:13.300 回答
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);
于 2013-03-21T13:15:49.107 回答
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();

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

于 2013-03-25T05:48:55.173 回答