2017-05-25 126 views
0

我正在按鈕單擊生成圖像。用於創建圖像的佈局在活動內部當前不可見。矢量圖像是佈局的背景,但我看到的只是黑色圖像。Android - 保存佈局作爲圖像顯示黑色圖像

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="370dp" 
    android:layout_height="400dp" 
    android:background="@drawable/ic_background"> 

    <TextView 
     android:id="@+id/address" 
     android:layout_width="match_parent" 
     android:textColor="@color/white" 
     android:textSize="40sp" 
     android:textStyle="bold" 
     android:gravity="center" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

生成圖像

private void generateAndShareImage(){ 
    final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    final View view = inflater.inflate(R.layout.image_address, null); 
    final TextView addressView = (TextView) view.findViewById(R.id.address); 
    addressView.setText("Test Address"); 

    final Bitmap cardImage = getBitmapFromView(view); 
    //store image in external storage 
    String savedImagePath = storeImage(cardImage); 
    if(savedFilePath != null){ 
     scanGallery(savedImagePath); 
    } 
} 

private void scanGallery(final String path) { 
    try { 
     MediaScannerConnection.scanFile(getBaseContext(), new String[] { path },null, new MediaScannerConnection.OnScanCompletedListener() { 
      public void onScanCompleted(String path, Uri uri) { 
      } 
     }); 
    } catch (Exception e) { 
     Log.e("Error scanning gallery",e.getMessage()); 
    } 
} 

private Bitmap getBitmapFromView(@NonNull final View view){ 
    final Bitmap bitmap = Bitmap.createBitmap(370, 400, Bitmap.Config.ARGB_8888); 
    final Canvas canvas = new Canvas(bitmap); 
    view.draw(canvas); 
    return bitmap; 
} 

有什麼不對?

回答

0

您需要measurelayout你的意見第一:

int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 
view.measure(spec, spec); 
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 
final Bitmap cardImage = getBitmapFromView(view); 

上面的代碼應該工作。

它更好地傳遞getBitmapFromView(view, width, height)中的測量視圖的尺寸,用於Bitmap.createBitmap()而不是使用硬編碼值。

檢查了這一點其他的調整Converting a view to Bitmap without displaying it in Android?

BTW,你不能使用370作爲它的是,如果你真的需要使用硬編碼的數字,然後用這個來代替:

float dp_370 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 370, context.getResources().getDisplayMetrics()); 

要了解密度獨立的像素在Android中是如何工作的,請閱讀: What is the difference between "px", "dp", "dip" and "sp" on Android?

https://material.io/guidelines/layout/units-measurements.html#units-measurements-scaleable-pixels-sp

編輯:

下面是我使用的是看在ImageView生成的位圖工作的代碼示例:

final LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
final View test = inflater.inflate(R.layout.test, null); 
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); 
test.measure(spec, spec); 
int measuredWidth = test.getMeasuredWidth(); 
int measuredHeight = test.getMeasuredHeight(); 
test.layout(0, 0, measuredWidth, measuredHeight); 
final Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888); 
final Canvas canvas = new Canvas(bitmap); 
test.draw(canvas); 

//now show this inside an ImageView 
ImageView image = (ImageView) findViewById(R.id.image); 
image.setImageBitmap(bitmap); 
+0

謝謝,但它仍然無法正常工作。我沒有在背景中看到drawable,也沒有看到文字。只是圖像的顏色現在變了 – Coder

+0

@Coder我更新了我正在使用的工作示例的答案,以查看生成的位圖。可能是你的'storeImage()'代碼的問題。 –