Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将自动换行添加到画布/画图文本_Android_Image_Canvas_Text_Bitmap - Fatal编程技术网

Android 如何将自动换行添加到画布/画图文本

Android 如何将自动换行添加到画布/画图文本,android,image,canvas,text,bitmap,Android,Image,Canvas,Text,Bitmap,我是Android新手,我正在学习下面的应用程序 该应用程序是一个gridview应用程序,在一个数组中包含大量图片。 点击图片后,全屏打开。 在那里,你可以分享图片或者在上面写点什么,然后和文本一起分享 我现在的问题是我不明白断线是如何工作的 是的,我使用了谷歌和这个论坛,但我不知道这个功能是如何工作的 所以我的问题是 有人能帮我在我不知道的时候设置一个自动换行,比如说12个字符,或者在行满后设置自动换行 谢谢你们,伙计们 我的代码: private Bitmap Processing

我是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[]行=captionString.split(“\n”);
float y=rectText.height();
对于(int i=0;i
请问您为什么要发布这些代码?只有画布代码已经足够了。把注意力集中在这个问题上。因为我对安卓系统还不熟悉,而且我不理解断线这件事。也许其他刚接触安卓系统的人将来会需要它。。。你能帮我一下吗?你知道我问题的答案吗@greenappsEven如果您是新手,则没有理由发布不相关的代码。只在需要支持的地方发布代码。你强迫我们读300行代码,其中20行就足够了。为什么要占用我们这么多的时间?好吧,伙计,我要改变它,但你有答案给我吗?谢谢大家!@greenappsCanvas:drawText()不知道诸如“\n”或“\r\n”之类的换行符。它将在rect中的一行上打印整个字符串。如果你幸运的话,你可以在换行符中看到点或正方形。你到底看到了什么?所以,这取决于您首先将字符串拆分成行,然后在循环中,以递增的y位置打印行?你是对的,我不希望每行的文字都变大,因为20%的人很高兴它能工作,我没有看到我不需要它^^在你的帮助下我学到了很多谢谢你再次祝你愉快:)!!!有趣的是,你第一次发布了+300行代码,而现在——几天后——只有不到10行代码的解决方案。读这个:哈哈,是的,但只有在你的帮助下:)
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?
   }