2017-06-21 55 views
1

我有一個RelativeLayout,並想從中創建一個位圖併發送到藍牙打印機的字節數。 根據設備分辨率,當我的位圖生成時會更改textviews fontsize - 分辨率越高,字體大小越高。 無論設備分辨率如何,我只能生成一個尺寸?從Android中的RelativeLayout創建位圖

我的代碼:

layout.setDrawingCacheEnabled(true); 
layout.measure(View.MeasureSpec.makeMeasureSpec(580, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(layout.getMeasuredHeight(), View.MeasureSpec.UNSPECIFIED)); 
layout.layout(0, 0, layout.getMeasuredWidth(), layout.getMeasuredHeight()); 
layout.buildDrawingCache(false); 
Bitmap sImagem = Bitmap.createBitmap(layout.getDrawingCache(false)); 
layout.setDrawingCacheEnabled(false); 

580是我的紙張大小和打印機就行了!

回答

0

嘗試這個

 private void takeScreenShot(){ 
     int totalHeight = mScreenshotLayout.getHeight(); 
     int totalWidth = mScreenshotLayout.getWidth(); 

     Bitmap b = getBitmapFromView(mScreenshotLayout,totalHeight,totalWidth); 
     SendingMail(b); 

     } 

     public Bitmap getBitmapFromView(View view, int totalHeight, int totalWidth) { 
     Bitmap returnedBitmap = Bitmap.createBitmap(totalWidth,totalHeight ,Bitmap.Config.ARGB_8888); 
     Canvas canvas = new Canvas(returnedBitmap); 
     Drawable bgDrawable = view.getBackground(); 
     if (bgDrawable != null) 
     bgDrawable.draw(canvas); 
    else 
     canvas.drawColor(Color.WHITE); 
    view.draw(canvas); 
    return returnedBitmap; 
} 
+0

沒有工作... –