2013-12-13 29 views
0

我將通過繪圖創建一個jpeg。元素是文字和圖像。當涉及到實現時,只繪製圖像和背景顏色,但沒有文字。我沒有DIEA實際發生的事情連我都被稱爲canvas.drawTextAndroid將文本添加到畫布失敗

以下是我的代碼

String folderName = "droidCanvas"; 
       String textString ="Hello , I am user 12345. \n Below is my signature."; 
       String fileNameString = "test.jpg"; 
       File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + folderName); 
       if(!folder.exists()){ 
        folder.mkdir(); 
       } 

      File bmpFile = new File(folder.getAbsolutePath() + File.separator + fileNameString); 
      if(!bmpFile.exists()){ 
       try { 
        bmpFile.createNewFile(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 

      //Draw something 
      Paint paint = new Paint(); 
      paint.setColor(Color.RED); 

      Paint txtPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 

      txtPaint.setColor(Color.BLACK); 
      txtPaint.setTextSize(20); 
      bmpBase = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); 
      canvas = new Canvas(bmpBase); 
      canvas.drawColor(Color.WHITE); 
      canvas.drawCircle(w/2, h/2, 300, paint); 
      canvas.drawText(textString ,w/2, 0 , txtPaint); 

      //Export to jpeg 
      try 
      { 
       fos = new FileOutputStream(bmpFile); 
       bmpBase.compress(Bitmap.CompressFormat.JPEG, 100, fos); 

       fos.flush(); 
       fos.close(); 
       fos = null; 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      finally 
      { 
       if (fos != null) 
       { 
        try 
        { 
         fos.close(); 
         fos = null; 
        } 
        catch (IOException e) 
        { 
         e.printStackTrace(); 
        } 
       } 
      } 
+2

嘗試'canvas.drawText(textString,W/2,H/2,txtPaint);',你就可以至少看到文字。然後你可以稍後重新定位它。現在它正在被繪製,但你不能看到它,因爲它在極端的頂端中心和越界。 –

+0

我看到了...謝謝,放在圖片的左上角怎麼樣?在提供的框架外拍攝似乎很容易 –

+0

您想將文字置於左上角?對? –

回答

2

嘗試canvas.drawText(textString ,w/2, h/2 , txtPaint);,你將能至少看到的文字。然後你可以稍後重新定位它。現在它正在被繪製,但你不能看到它,因爲它在極端的頂端中心和越界。

如果你想將它放在左上角,然後做到這一點:

canvas.drawText(textString ,0 , txtPaint.getFontSpacing(), txtPaint);