1

Androidアプリケーションでテキストから画像へのコードがあり、テキストを画像に正常に変換し、SDカードのローカルの場所に保存しています。私が直面している問題は、テキスト全体を画像に変換していないことです。これは、私がテキストビューに提供しているテキストです。これが画像に変換されたものですここに画像の説明を入力

これが私のコードです

 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

問題はあなたの幅についてだと思います。あなたの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

この行を変更します:

 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

To convert the complete the text to an image you have to get the width of the text inside the textview not the textview itself. So here is how you can get the width of text.

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

Now you can use the above width and height to create the bitmap and the complete text (no matter how long or small) will be converted to image.

于 2013-03-25T05:48:55.173 に答える