Question

I´m new to Android and i´m learning on my following app.

The app is a gridview app with a lot of pictures in an array. After clicking on a picture fullscreen is opened. There it´s possible to share the picture or write something on it and share it with the text.

My problem now is that i don´t understand how line break is working And yes i used google and this forum but i don´t know how that function is working...

So my question is Could somebody help me to set an automatic line break after i don´t know let´s say 12 characters or automatic after the line is full...

Thank you dudes!

My Code:

    private Bitmap ProcessingBitmap(){
        Bitmap bm1 = null;
        Bitmap newBitmap = null;

        try {
            bm1 = BitmapFactory.decodeStream(
                    getContentResolver().openInputStream(source1));

            Config config = bm1.getConfig();
            if(config == null){
                config = Bitmap.Config.ARGB_8888;
            }

            newBitmap = Bitmap.createBitmap(bm1.getWidth(), bm1.getHeight(), config);
            Canvas newCanvas = new Canvas(newBitmap);

            newCanvas.drawBitmap(bm1, 0, 0, null);

            String captionString = editTextCaption.getText().toString();
            if(captionString != null){

                Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
                paintText.setColor(Color.WHITE);
                paintText.setTextSize(25);
                paintText.setStyle(Style.FILL);

                Rect rectText = new Rect();
                paintText.getTextBounds(captionString, 0, captionString.length(), rectText);

                newCanvas.drawText(captionString, 
                        0, rectText.height(), paintText);

                Toast.makeText(getApplicationContext(), 
                        "drawText: " + captionString, 
                        Toast.LENGTH_LONG).show();


            }else{
                Toast.makeText(getApplicationContext(), 
                        "caption empty!", 
                        Toast.LENGTH_LONG).show();

            }

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    + File.separator + "tmp1.jpeg");
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //write the bytes in file
            FileOutputStream fo = new FileOutputStream(f);
            try {
                fo.write(bytes.toByteArray());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // remember close de FileOutput
            try {
                fo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.d("done","done");
            MediaScannerConnection.scanFile(this,
                      new String[] { f.toString() }, null,
                      new MediaScannerConnection.OnScanCompletedListener() {
                  public void onScanCompleted(String path, Uri uri) {
                      Log.i("ExternalStorage", "Scanned " + path + ":");
                      Log.i("ExternalStorage", "-> uri=" + uri);
                  }
              });

            } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }

        return newBitmap;
    }

And here my layout file if you need it too:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:background="@color/black"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/loadimage1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/green"
        android:text="Load Image" />

    <TextView
        android:id="@+id/sourceuri1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/caption"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:imeOptions="actionNext"
        android:background="@color/white" />

    <Button
        android:id="@+id/processing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/green"
        android:text="Show text on Image" />

    <ImageView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Was it helpful?

Solution

String [] lines=captionString.split("\n"); 

float y = rectText.height(); 

for (int i = 0; i < lines.length; i++) 
   {
   paintText.getTextBounds(lines[i], 0, lines[i].length(), rectText);

   newCanvas.drawText(lines[i], 0, y, paintText); 

   y += rectText.height() * 1.2;  // why 1.2?
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top