Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.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
Java Android-将布局另存为图像显示黑色图像_Java_Android_Android Bitmap - Fatal编程技术网

Java Android-将布局另存为图像显示黑色图像

Java Android-将布局另存为图像显示黑色图像,java,android,android-bitmap,Java,Android,Android Bitmap,我正在生成一个按钮点击图像。用于从中创建图像的布局当前在活动中不可见。矢量图像是布局的背景,但我看到的是黑色图像 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="370dp" android:layout_height="400dp" 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> 

有什么问题吗?

您需要先测量视图和布局:

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(视图、宽度、高度)
中传递测量视图的尺寸,以便用于
Bitmap.createBitmap()
,而不是使用硬编码值

检查这个为其他调整

顺便说一句,您不能按原样使用
370
,如果您确实需要使用硬编码的数字,请改用此数字:

float dp_370 = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 370, context.getResources().getDisplayMetrics());
要了解Android中密度无关像素的工作原理,请阅读:

编辑:

下面是一个工作代码示例,我正在使用它查看
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);

谢谢,但还是不行。我看不到可绘制的背景,也看不到文本。只是图像的颜色现在变了changed@Coder我用我用来查看生成的位图的工作示例更新了我的答案。问题可能出在您的
storeImage()
code上。
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);