2013-07-02 30 views
0

我正在繪製一些文本行。幾行後,它離開屏幕。我想要做的是在位圖中捕捉所有行(也是畫布外的行)。如何繪製外部畫布並製作其位圖?

I have the code below which only works for within the canvas(screen). 

    private class DeciderView extends View { 

     private Paint paint; 
     String text = ""; 
     String[] options = { "een", "twee", "drie", "vier", "vijf", "zes", "zeven", "acht", 
       "negen", "tien", "elf", "twaalf", "dertien", "vertien", "vijftien" }; 

     public DeciderView(Context context) { 
      super(context); 
      // Keep screen on 
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
      // Remove title bar 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      // Remove notification bar 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
        WindowManager.LayoutParams.FLAG_FULLSCREEN); 
      this.setBackgroundColor(Color.WHITE); 
      paint = new Paint(); 
      paint.setTextSize(75); 
      paint.setColor(Color.BLACK); 
      paint.setStrokeWidth(10); 
      paint.setAntiAlias(true); 
      setDrawingCacheEnabled(true); 

      setDrawingCacheQuality(DRAWING_CACHE_QUALITY_HIGH); 

     } 

     private void drawInput(Canvas canvas) { 
      Paint p = new Paint(); 
      p.setTextAlign(Align.CENTER); 
      p.setTextSize(canvas.getWidth()/10f); 
      p.setColor(Color.BLACK); 

      float xText = canvas.getWidth()/2f; 

      float yText = (canvas.getHeight()/4f); 
      for (int i = 0; i < options.length; i++) { 
       text += options[i] + "\n"; 
      } 
      for (String line : text.split("\n")) { 
       canvas.drawText(line, xText, yText, p); 
       yText -= (p.ascent() + p.descent()) * 2.5f; 

      } 
     } 


     @Override 
     public void onDraw(Canvas canvas) { 

      drawInput(canvas); 

      this.setDrawingCacheEnabled(true); 
      Bitmap b = Bitmap.createBitmap(this.getDrawingCache()); 
      b = Bitmap.createScaledBitmap(b, canvas.getWidth(), canvas.getHeight(), false); 

      try { 
       b.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(
         new File(Environment.getExternalStorageDirectory().getPath() 
           + "/Pictures/aaaaa.jpg"))); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } 

      if (b != null) { 
       b.recycle(); 
       b = null; 
      } 
     } 

    } 

因此,基本上我想從所有行中創建一個位圖,即使這些行是在畫布外繪製的。

回答

1

爲什麼不直接繪製到位圖?您可以創建一個具有支持它的位圖的新Canvas。您使用普通畫布繪製命令,輸出將寫入位圖而不是屏幕。如果您還想將其繪製到屏幕上,則只需將新位圖繪製到屏幕上即可。要做到這一點,所有你需要的是

Canvas myCanvas = new Canvas(myBitmap); 
+0

謝謝,這個工程很好。我遇到的唯一問題是,在將其保存到sdcard後,位圖的質量較低。 – MeesterPatat