2017-08-01 5 views
0

J'ai donc la mise en page suivante appelée demo_text.xmlConversion mise en page XML externe à l'aide de la mise en page Bitmap gonfleur

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/screen"> 

<TextView 
    android:id="@+id/textToDisplay" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:text="Hello World" 
</FrameLayout> 

Je suis en train de convertir cette demo_text à un Bitmap. Ceci est mon onCreate:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    View inflatedFrame = getLayoutInflater().inflate(R.layout.demo_text, null); 
    FrameLayout frameLayout = (FrameLayout) inflatedFrame.findViewById(R.id.screen) ; 
    frameLayout.setDrawingCacheEnabled(true); 
    frameLayout.buildDrawingCache(); 
    Bitmap bm = frameLayout.getDrawingCache(); 

} 

Je veux que le bitmap soit la totalité de la mise en page mais le Bitmap retourne toujours null.

+0

Pourquoi utilisez-vous en tant que root 'FrameLayout' de votre point de vue dans votre activité? Vous pouvez utiliser 'FrameLayout' dans un' Fragment'. Quoi qu'il en soit, vous pouvez essayer de passer la racine 'ViewGroup' au second argument de la méthode' inflate' au lieu de 'null'. (Vous ne savez pas si cela fonctionne) – lalosoft

+0

Dupliquer https://stackoverflow.com/a/12406426/2700586 – Mani

Répondre

0

Vous pouvez essayer avec le code ci-dessous

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
FrameLayout frameLayout = (FrameLayout) inflatedFrame.findViewById(R.id.screen); 
Bitmap b = loadBitmapFromView(frameLayout); 
} 

méthode utilisation de belolw pour obtenir bitmap.

public Bitmap loadBitmapFromView(View v) { 
    Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, 
    v.getLayoutParams().height, Bitmap.Config.ARGB_8888);     
    Canvas c = new Canvas(b); 
    v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom()); 
    v.draw(c); 
    return b; 
} 
3

Le code suivant fonctionne:

View inflatedFrame = getLayoutInflater().inflate(R.layout.demo_text, null); 
    FrameLayout frameLayout = (FrameLayout) inflatedFrame.findViewById(R.id.screen) ; 
    frameLayout.setDrawingCacheEnabled(true); 
    frameLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), 
      View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
    frameLayout.layout(0, 0, frameLayout.getMeasuredWidth(), frameLayout.getMeasuredHeight()); 
    frameLayout.buildDrawingCache(true); 
    final Bitmap bm = frameLayout.getDrawingCache();