2

I want to convert the text in a string to image and display it in a ImageView and in addition i want to get the dimensions of the image created at run time for further use. Here is what i have done and used from search.

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

    tv.setLayoutParams(layoutParams);

    tv.setText("Some Text, can be long as much as required");
    tv.setTextColor(Color.BLACK);
    tv.setBackgroundColor(Color.WHITE);

    Bitmap testB;
    timer = new Timer();
    timer.schedule(new TickerTask(), 1000,25);
    testB = Bitmap.createBitmap(600, 20, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(testB);

    tv.layout(0, 0, 380, 100);
    tv.draw(c);

    iv = (ImageView) findViewById(R.id.menuIcon);
    iv.setLayoutParams(layoutParams);
    iv.setBackgroundColor(Color.GREEN);
    iv.setImageBitmap(testB);

Problems: The parameters are not working as set in the code. The complete text is not shown after the image is converted.

Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
  • Not really adviced.. How about using background image and keep changing its text.. SOmething like ImageButton..? – ngesh Mar 21 '13 at 10:48
  • Try this http://stackoverflow.com/questions/2801116/converting-a-view-to-bitmap-without-displaying-it-in-android – sinek Mar 21 '13 at 10:48
  • The task i have to acheive requires to convert the text to image. I am performing the animation on the text horizontally but not able to detect that when the text has completely passed the limit of the textview on left. Image width can help me apply restart on animation after completed – Jibran Khan Mar 21 '13 at 10:51

3 Answers3

1

I believe its taking up the whole screen because you don't have a container such as a Linear Layout which then contains an ImageView with layout constraints, so the ImageView expands to fill the available screen. Try this:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView tv = new TextView(this);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(80, 100);
    tv.setLayoutParams(layoutParams);
    tv.setText("testing 1 2 3");
    tv.setTextColor(Color.BLACK);
    tv.setBackgroundColor(Color.TRANSPARENT);

    Bitmap testB;

    testB = Bitmap.createBitmap(80, 100, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(testB);
    tv.layout(0, 0, 80, 100);
    tv.draw(c);

    ImageView iv = (ImageView)findViewById(R.id.menuIcon);
    iv.setLayoutParams(layoutParams);
    iv.setBackgroundColor(Color.GRAY);
    iv.setImageBitmap(testB);
    iv.setMaxHeight(80);
    iv.setMaxWidth(80);
}

And in your main.xml file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

  <ImageView android:id="@+id/menuIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />


</LinearLayout>
Furqi
  • 2,403
  • 1
  • 26
  • 32
0

This worked for me, for those who want the same thing to acheive

 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",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(),
                "Error during image saving", Toast.LENGTH_LONG).show();
    }
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
0

i have tried Text2Jpg which look awesome and convert any text to a jpg image with or without background custom image.

Use StaticLayout:

    TextPaint mTextPaint = new TextPaint();
    mTextPaint.setColor(bordercolor);
    mTextPaint.setTextSize(textSize);
    StaticLayout mTextLayout = new StaticLayout(text, mTextPaint, (int) (screen_width * .75f), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    // DynamicLayout mTextLayout = new DynamicLayout(text, mTextPaint,(int)(screen_width * .75f), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
    //mTextPaint.setStrokeWidth(screen_width * .70f);
    canvas.translate(screen_width * .12f, screen_height * .06f); //position the text
    mTextLayout.draw(canvas);
lvapp me
  • 1
  • 1