我的 View 中有 imageView 和 textView 在 LinearLayout 中,我想从这两个控件中获取一个位图。我尝试这样做:
public static 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;
}
但是我的应用程序崩溃了,我看到了这个:
Caused by: java.lang.IllegalArgumentException: width and height must be > 0
最佳答案
if (v.getMeasuredHeight() <= 0) {
v.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.draw(c);
return b;
}
关于Java android 从 imageView 和 TextView 获取一张位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55901370/