在實踐中發現,有些需要打印的小票高度小於屏幕的高度,而有些小票內容過多高度高於屏幕高度。
小於屏幕高度的布局文件轉成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;
}
}