在android中,如何在不重塑的情况下将线性布局转换为位图?
我正在为pax打印机开发一个应用程序,所以我需要将我的xml布局(线性、相对…)转换为位图,我的问题是,当我将任何布局转换为图像时,创建的位图宽度大小将被重新塑造。为什么会发生这种情况?我怎样才能解决这个问题? 这是我的转换代码:在android中,如何在不重塑的情况下将线性布局转换为位图?,android,android-layout,android-bitmap,layout-inflater,Android,Android Layout,Android Bitmap,Layout Inflater,我正在为pax打印机开发一个应用程序,所以我需要将我的xml布局(线性、相对…)转换为位图,我的问题是,当我将任何布局转换为图像时,创建的位图宽度大小将被重新塑造。为什么会发生这种情况?我怎样才能解决这个问题? 这是我的转换代码: LayoutInflater lInflater = (LayoutInflater)getApplicationContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE); View view = lIn
LayoutInflater lInflater = (LayoutInflater)getApplicationContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = lInflater.inflate(R.layout.testlayout, null);
view.setDrawingCacheEnabled(true);
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false); // clear drawing cache
imgview.setImageBitmap(b);
storeImage(b);
我尝试了许多其他的转换方法,但我得到了同样的结果。
这是我的布局之一:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/printLL"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#fff">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/printing">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="2sp"
android:gravity="center"
android:text="Header"
android:textSize="13sp" />
<TextView
android:id="@+id/counter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="2sp"
android:gravity="center"
android:text="Title" />
<TableLayout
android:id="@+id/record_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:background="#ffffff"
android:gravity="center"
android:shrinkColumns="*"
android:stretchColumns="*"
>
<TableRow android:layout_gravity="center_horizontal">
<TextView
android:id="@+id/CustomerName"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="Username" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="@+id/customer_id"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
<TextView
android:id="@+id/postalcode"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="@+id/Address"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="@+id/ID"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
<TextView
android:id="@+id/customer_number"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="@+id/Serial"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
<TextView
android:id="@+id/Digit"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="@+id/PrevCode"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
<TextView
android:id="@+id/PrevDebt"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:id="@+id/PrevDate"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextView" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/Usage_type"
android:layout_weight="1"
android:gravity="center"
android:padding="3sp"
android:text="TextViwe" />
</TableRow>
</TableLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
请注意,无论布局、文本视图、图像视图,。。。或者我用的是哪种布局类型。都是同样的问题
感谢您的帮助尝试一下:
public static Bitmap view2Bitmap(final FrameLayout view) {
if (view == null) return null;
boolean drawingCacheEnabled = view.isDrawingCacheEnabled();
boolean willNotCacheDrawing = view.willNotCacheDrawing();
view.setDrawingCacheEnabled(true);
view.setWillNotCacheDrawing(false);
Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap;
if (null == drawingCache) {
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
drawingCache = view.getDrawingCache();
if (drawingCache != null) {
bitmap = Bitmap.createBitmap(drawingCache);
} else {
bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
}
} else {
bitmap = Bitmap.createBitmap(drawingCache);
}
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCacheDrawing);
view.setDrawingCacheEnabled(drawingCacheEnabled);
return bitmap;
}
您好,我尝试了您的方法,但不起作用。结果相同。奇怪的是,我复制了您的代码,一切正常,我可以在
ImageView
中看到bitmap
显示,您的意思是创建的位图可以吗?奇怪。我很困惑。这个代码有什么问题!是的,我试过使用FrameLayout
引用的printing
id