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;
}
}
Android中将布局文件转成bitmap的更多相关文章
- Android中将布局文件/View添加至窗口过程分析 ---- 从setContentView()谈起
本文主要内容是讲解一个视图View或者一个ViewGroup对象是如何添加至应用程序窗口中的.下文中提到的窗口可泛指我们能看到的界面,包括一个Activity呈现的界面(我们可以将之理解为应用程序窗口 ...
- 在JAVA中将class文件编译成jar文件包,运行提示没有主清单属性
在JAVA中将class文件编译成jar文件包,运行提示没有主清单属性 Maven 项目生成jar运行时提示“没有主清单属性” 新建了一个Maven的项目,mvn compile和mvn packag ...
- Android无布局文件下自定义通知栏notification的 icon
在开发项目一个与通知栏有关的功能时,由于自己的项目是基于插件形式的所以无法引入系统可用的布局文件,这样无法自定义布局,造成无法自定义通知栏的icon. 在网上也有一种不用布局文件更换icon的方法,但 ...
- Xamarin.Android之布局文件智能提示问题
一.前言 看到有人问关于xamarin.android的布局没智能提示问题(VS 2015),当然,写布局这东西没提示这是一件相对痛苦的事 ,所以这里就提供一个解决的方案! 二.解决方案 想要智能提示 ...
- Android -- Layout布局文件里的android:layout_height等属性为什么会不起作用?
有的时候,我们配置好的布局文件,在加载完成添加到我们的Activity中后发现,并没有安装我们设置的属性来布局,比如我们设置了android:layout_marginTop="100dip ...
- Android Layout布局文件里的android:layout_height等属性不起作用
有的时候,我们配置好的布局文件,在加载完成添加到我们的Activity中后发现,并没有安装我们设置的属性 来布局,比为我们设置了android:layout_marginTop="100di ...
- android studio布局文件/XML怎么代码补全
android studio中的布局文件代码补全方式是打第一个字母就提示了,而java代码有时候要按快捷键. 布局文件的话呢,要写在标签开始处才提示,在标签闭合处有时候不提示,有时候在内容里也会有不提 ...
- Android中布局文件中使用onClick属性
安卓开发中,布局文件中的控件有一个属性,是onClick,例如: <Button android:id="@+id/button1" ...
- android横屏布局文件设置
一.AndroidManifest.xml配置 1.在AndroidManifest.xml的activity(需要禁止转向的activity)配置中加入 android:screenOrient ...
随机推荐
- 图片缩放应用(nearest / bilinear / three-order interpolate)
typedef xPixel PIXELCOLORRGB; double Sinxx(double value){ if (value < 0) value = -value; if (valu ...
- instruments usage error specified target process is invalid
遇到这个问题的很多,但都没说具体的解决办法. 如果你的包名 路径之类的都正确,还是报这个错误的话,请重启手机.
- eclipse中的代码提示功能
Eclipse 的代码提示功能,具体配置 1. 打开Eclipse ,然后"window"→"Preferences" 2. 选择"java" ...
- EF MYSQL 不能选择实体框架版本
web.config文件里面加如下配置,然后编译 <provider invariantName="MySql.Data.MySqlClient" type="My ...
- 根据第三方库spire.pdf使用指定打印机打印pdf文件
private void button1_Click(object sender, EventArgs e) { PdfDocument doc = new PdfDocument(); string ...
- sublime插件@sublimelinter安装使用
sublimelinter插件是一款sublime编辑器的代码校验插件,支持多种语言,对于前端来说主要包含css和js校验. 要是用这款插件 1)安装node,然后在全局安装jshint(npm in ...
- 分享自制的13套 JQuery Mobile 界面主题(追加4套新款)
15套整合在一起的,其中2套官方+13套自制,款款精致,方便移动开发. 字体默认为微软雅黑. 适配于 JQuery Mobile 1.4.3 下载地址:http://files.cnblogs.com ...
- SQL SERVER 将表中字符串转换为数字的函数 (详询请加qq:2085920154)
在SQL SERVER 2005中,将表中字符串转换为数字的函数共2个:1. convert(int,字段名) 例如:select convert(int,'3')2. cast(字段名 as i ...
- mysql行列调换方法
行变列,列变行 财务样式模板: CREATE TABLE `grade` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR ...
- 解决JQuery.ajax.post乱码问题
问题:昨天在进行项目功能实现时,利用了$.ajax吧数据post给服务,接着保存到cookie中,而数据中是带有中文的 我的post代码: comCarId=encodeURIComponent(co ...