Android中将布局文件转成bitmap

少年辛苦终身事,莫向光阴惰寸功。这篇文章主要讲述Android中将布局文件转成bitmap相关的知识,希望能为你提供帮助。
在实践中发现,有些需要打印的小票高度小于屏幕的高度,而有些小票内容过多高度高于屏幕高度。
【Android中将布局文件转成bitmap】小于屏幕高度的布局文件转成bitmap较为容易,高于屏幕高度的布局文件转成长图bitmap较为复杂。
一.小于屏幕高度的布局文件转成bitmap 1.需求
在交易过程中常常需要打印小票,利用布局文件组织小票格式,并将其转成bitmap之后打印出来较为方便。
2.布局文件转bitmap

public class ReceiptViewActivity extends Activity{
private View view;
private boolean isEnd = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.activity_receiptview, null);
setContentView(view);

new Thread(new Runnable() {
@Override
public void run() {
while (!isEnd){
try {
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
Pic pic = new Pic(bitmap);
Printer printer = new Printer(SaleActivity.dal);
int printstatus = printer.printbitmap(pic);
if (printstatus == 0){
isEnd = true;
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}).start();
}
}

二.高于屏幕高度的布局文件转成bitmap 1.需求
有时小票内容过多,屏幕显示不下,需要滚动显示并打印完整小票。
2.利用ScrlooView实现滚动显示
< ?xml version="1.0" encoding="utf-8"?>
< ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="@color/white"
android:orientation="vertical"
android:scrollbars="none"
android:id="@+id/scrollview">
< LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
< TextView
android:id="@+id/counter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/greater_magrin_space"
android:gravity="center"
android:text="10s"
android:textColor="@color/black"
android:textSize="@dimen/content_text_size_standard"/>
< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/greater_magrin_space"
android:gravity="center"
android:text="@string/title_sale"
android:textColor="@color/black"
android:textSize="@dimen/content_text_size_standard"/>
< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/content_header_space"
android:gravity="center"
android:text="@string/processing"
android:textColor="@color/black"
android:textSize="@dimen/content_text_size_standard"/>
< TextView
android:layout_width="match_parent"
android:layout_height="@dimen/greater_magrin_space" />
< TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/greater_magrin_space"
android:gravity="center"
android:text="10s"
android:textColor="@color/black"
android:textSize="@dimen/content_text_size_standard"/>

< /LinearLayout>

< /ScrollView>

3.布局文件转bitmap
public class ReceiptViewActivity extends Activity{
private ScrollView scrollView;
private boolean isEnd = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiptview);

scrollView = (ScrollView)findViewById(R.id.scrollview);

new Thread(new Runnable() {
@Override
public void run() {
while (!isEnd){
try {
Bitmap bitmap = getBitmapByView(scrollView);
Pic pic = new Pic(bitmap);
Printer printer = new Printer(SaleActivity.dal);
int printstatus = printer.printbitmap(pic);
if (printstatus == 0){
isEnd = true;
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}).start();
}
//ScrollView 转成bitmap长图
public static Bitmap getBitmapByView(ScrollView scrollView) {
int h = 0;
Bitmap bitmap = null;

for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
scrollView.getChildAt(i).setBackgroundColor(
Color.parseColor("#ffffff"));
}

bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.RGB_565);
final Canvas canvas = new Canvas(bitmap);
scrollView.draw(canvas);
return bitmap;
}
}


































































































































    推荐阅读