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

小于屏幕高度的布局文件转成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的更多相关文章

  1. Android中将布局文件/View添加至窗口过程分析 ---- 从setContentView()谈起

    本文主要内容是讲解一个视图View或者一个ViewGroup对象是如何添加至应用程序窗口中的.下文中提到的窗口可泛指我们能看到的界面,包括一个Activity呈现的界面(我们可以将之理解为应用程序窗口 ...

  2. 在JAVA中将class文件编译成jar文件包,运行提示没有主清单属性

    在JAVA中将class文件编译成jar文件包,运行提示没有主清单属性 Maven 项目生成jar运行时提示“没有主清单属性” 新建了一个Maven的项目,mvn compile和mvn packag ...

  3. Android无布局文件下自定义通知栏notification的 icon

    在开发项目一个与通知栏有关的功能时,由于自己的项目是基于插件形式的所以无法引入系统可用的布局文件,这样无法自定义布局,造成无法自定义通知栏的icon. 在网上也有一种不用布局文件更换icon的方法,但 ...

  4. Xamarin.Android之布局文件智能提示问题

    一.前言 看到有人问关于xamarin.android的布局没智能提示问题(VS 2015),当然,写布局这东西没提示这是一件相对痛苦的事 ,所以这里就提供一个解决的方案! 二.解决方案 想要智能提示 ...

  5. Android -- Layout布局文件里的android:layout_height等属性为什么会不起作用?

    有的时候,我们配置好的布局文件,在加载完成添加到我们的Activity中后发现,并没有安装我们设置的属性来布局,比如我们设置了android:layout_marginTop="100dip ...

  6. Android Layout布局文件里的android:layout_height等属性不起作用

    有的时候,我们配置好的布局文件,在加载完成添加到我们的Activity中后发现,并没有安装我们设置的属性 来布局,比为我们设置了android:layout_marginTop="100di ...

  7. android studio布局文件/XML怎么代码补全

    android studio中的布局文件代码补全方式是打第一个字母就提示了,而java代码有时候要按快捷键. 布局文件的话呢,要写在标签开始处才提示,在标签闭合处有时候不提示,有时候在内容里也会有不提 ...

  8. Android中布局文件中使用onClick属性

    安卓开发中,布局文件中的控件有一个属性,是onClick,例如:           <Button             android:id="@+id/button1" ...

  9. android横屏布局文件设置

    一.AndroidManifest.xml配置 1.在AndroidManifest.xml的activity(需要禁止转向的activity)配置中加入   android:screenOrient ...

随机推荐

  1. (转载)持续集成(第二版)[来自:Martin Fowler]

    转载自:iTech的博客 持续集成(第二版) 作者:Martin Fowler 译者:雷镇 持续集成 是一种软件开发实践.在持续集成中,团队成员频繁集成他们的工作成果,一般每人每天至少集成一次,也可以 ...

  2. 【转】c3p0详细配置

      官方文档 : http://www.mchange.com/projects/c3p0/index.html <c3p0-config><default-config>&l ...

  3. 关于representation的理解

    目前见过的定义的比较确切的是Yoshua Bengio在ACL2010的一篇paper中关于word representation的定义: " A word Representation i ...

  4. 抽象工厂模式 shiyanlou

    二.什么是 抽象工厂模式 抽象工厂模式(Abstract Factory Pattern)是一种软件开发设计模式.抽象工厂模式提供了一种方式,可以将一组具有同一主题的单独的工厂封装起来.如果比较抽象工 ...

  5. WinForm 使用 HttpUtility

    在 Visual C# 中使用 HttpUtility 是无效的,即使添加了命名空间 System.Web,是因为需要在引用中添加 System.Web.dll. 可是没有 System.Web.dl ...

  6. Winform打砖块游戏制作step by step第6节---双缓冲应用

    一 引子 为了让更多的编程初学者,轻松愉快地掌握面向对象的思考方法,对象继承和多态的妙用,故推出此系列随笔,还望大家多多支持. 二 本节内容---双缓冲应用 1.  主界面截图如下: 2.  什么是双 ...

  7. 插入排序 - C语言

    插入排序的思想: 以现有的已排序元素为基础,下一个元素添加到正确的位置,则最终会完成排序. 第一个元素本身是已经排序好的.从第二个开始排. void insertSort(int arr[], int ...

  8. Shell文本处理 - 匹配与编辑

    正则表达式 符号 含义 . 匹配任意ASCII中任意单个字符,或是字母,或是数字 ^ 匹配行首 $ 匹配行尾 * 匹配任意字符或前一个的一次或多次重复 \ 转义,被转义的有$ . ‘ “ * [ ] ...

  9. java导入excel时遇到的版本问题

    java中读取excel文件时对不同的版本提供了不同的读取方法,这就要求我们在读取excel文件时获取excel文件的版本信息从而通过不同的版本去使用不同的读取方式, 在WorkbookFactory ...

  10. Git 教程

    Git 教程 新建 模板 小书匠 欢迎使用 小书匠(xiaoshujiang)编辑器,您可以通过设置里的修改模板来改变新建文章的内容. Git使用 Git - 关于版本控制 TortoiseGit日常 ...