码迷,mamicode.com
首页 > 移动开发 > 详细

Android中将布局文件转成bitmap

时间:2016-12-15 11:13:54      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:android   linear   i++   process   ack   bundle   back   lin   super   

在实践中发现,有些需要打印的小票高度小于屏幕的高度,而有些小票内容过多高度高于屏幕高度。

小于屏幕高度的布局文件转成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;
}
}

Android中将布局文件转成bitmap

标签:android   linear   i++   process   ack   bundle   back   lin   super   

原文地址:http://www.cnblogs.com/ccdd1314/p/6182224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!