Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用布局充气器将外部XML布局转换为位图_Android_Layout_Bitmap_Views - Fatal编程技术网

Android 使用布局充气器将外部XML布局转换为位图

Android 使用布局充气器将外部XML布局转换为位图,android,layout,bitmap,views,Android,Layout,Bitmap,Views,因此,我有一个名为demo_text.xml的布局 <?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

因此,我有一个名为demo_text.xml的布局

<?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>

我希望位图是整个布局,但位图总是返回null

您可以尝试使用以下代码

@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);
}
使用bellow方法获取位图

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;
}

你可以试试下面的代码

@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);
}
使用bellow方法获取位图

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;
}

以下代码起作用:

    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();

以下代码起作用:

    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();

为什么在活动中使用
FrameLayout
作为视图的根目录?您可以在
片段中使用
FrameLayout
。无论如何,您可以尝试在
inflate
方法的第二个参数处传递根
ViewGroup
,而不是
null
。(不确定是否有效)复制为什么在活动中使用
FrameLayout
作为视图的根目录?您可以在
片段中使用
FrameLayout
。无论如何,您可以尝试在
inflate
方法的第二个参数处传递根
ViewGroup
,而不是
null
。(不确定是否有效)重复