android - 如何向 Canvas/paintText 添加自动换行符

标签 android image canvas text bitmap

我是 Android 新手,正在学习我的以下应用。

该应用程序是一个 gridview 应用程序,在一个数组中有很多图片。 点击图片后全屏打开。 可以分享图片或在图片上写点东西,然后与文字一起分享。

我现在的问题是我不明白换行符是如何工作的 是的,我使用了谷歌和这个论坛,但我不知道该功能是如何工作的......

所以我的问题是 有人可以帮我设置一个自动换行符吗我不知道让我们说 12 个字符或行满后自动...

谢谢大佬们!

我的代码:

    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;
    }

如果您也需要,这里是我的布局文件:

    <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>

最佳答案

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?
   }

关于android - 如何向 Canvas/paintText 添加自动换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23322705/

相关文章:

Android 3D 游戏开发

javascript - 启用和禁用 ionic 中的输入字段

html - 根据图片大小调整div

java - 图像未从数据库中显示

javascript - 使用鼠标事件在 Canvas 上绘制一个圆圈

java - 如果服务器在写入 OutputStream 时关闭,SSLSocket 客户端不会抛出异常

android - java.lang.VerifyError : MappingJacksonHttpMessageConverter, 我需要什么 jackson 版本?

javascript - 我想使用 jquery mobile 显示 iphone 的 native 警告框

javascript - 旋转 Canvas 的一部分

javascript - Flash HTML5 Canvas fl_MouseOverHandler