首页 技术 正文
技术 2022年11月18日
0 收藏 893 点赞 2,961 浏览 4356 个字

1、需求

在项目开发过程中,打印小票前需要添加打印预览功能,交易数据在打印前转成bitmap然后直接打印,为了显示这个bitmap需要将其传给显示activity。

2、解决方法

把bitmap存储为byte数组,然后再通过Intent传递。

3、XML布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <include layout="@layout/header_layout" /> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="455dp"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="25dp"
android:layout_marginLeft="18dp"
android:layout_marginRight="17.5dp"
android:src="@drawable/print_preview"/> <ImageView
android:id="@+id/printPreview"
android:layout_width="match_parent"
android:layout_height="385dp"
android:layout_marginTop="60dp"
android:layout_marginLeft="61.5dp"
android:layout_marginRight="61dp"/>
</RelativeLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal">
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selection_button_gray_left"
android:text="Cancel"
android:textColor="@color/cancel_button_text"/>
<Button
android:id="@+id/print_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/selection_button_blue_right"
android:text="Print"
android:textColor="@color/key_normal_color"/>
</LinearLayout>
</LinearLayout></LinearLayout>

如何在两个activity之间传递bitmap


4、生成bitmap

public class ReceiptPreviewTrans {    public Bitmap preview(TransData transData){
ReceiptGeneratorTrans receiptGeneratorTrans = new ReceiptGeneratorTrans(transData, 1, 1, false);
return receiptGeneratorTrans.generate();
}
}

5、ActionPrintPreview实现

public class ActionPrintPreview extends AAction {    private Context context;
private Handler handler;
private TransData transData; public ActionPrintPreview(ActionStartListener listener) {
super(listener);
} public void setParam(Context context, Handler handler, TransData transData) {
this.context = context;
this.handler = handler;
this.transData = transData;
} @Override
protected void process() {
ReceiptPreviewTrans receiptPreviewTrans = new ReceiptPreviewTrans();
Bitmap bitmap = receiptPreviewTrans.preview(transData);
Intent intent = new Intent(context, PrintPreviewActivity.class);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte [] bitmapByte =baos.toByteArray();
intent.putExtra("bitmap", bitmapByte);
context.startActivity(intent);
}
}

6、PrintPreviewActivity实现

public class PrintPreviewActivity extends BaseActivityWithTickForAction implements View.OnClickListener{    private TextView headerText;
private ImageView backBtn; private Bitmap bitmap;
private byte [] bis;
private ImageView imageView;
private Button btnCancel;
private Button btnPrint; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Device.enableStatusBar(false);
} @Override
protected void onResume() {
super.onResume();
} @Override
protected void loadParam() {
Intent intent=getIntent();
if(intent !=null)
{
bis=intent.getByteArrayExtra("bitmap");
bitmap= BitmapFactory.decodeByteArray(bis, 0, bis.length);
}
} @Override
protected int getLayoutId() {
return R.layout.activity_print_preview_layout;
} @Override
protected void initViews() {
headerText = (TextView) findViewById(R.id.header_title);
headerText.setText(R.string.preview);
backBtn = (ImageView) findViewById(R.id.header_back);
backBtn.setVisibility(View.GONE); imageView = (ImageView)findViewById(R.id.printPreview);
imageView.setImageBitmap(bitmap);
btnCancel = (Button)findViewById(R.id.cancel_button);
btnPrint = (Button)findViewById(R.id.print_button);
} @Override
protected void setListeners(){
btnCancel.setOnClickListener(this);
btnPrint.setOnClickListener(this);
} @Override
public void onClick(View v) { switch (v.getId()) {
case R.id.cancel_button:
break;
case R.id.print_button:
finish(new ActionResult(TransResult.SUCC, null));
break;
default:
break;
} } @Override
protected void handleMsg(Message msg) { } public void finish(ActionResult result) {
tickTimerStop();
AAction action = TransContext.getInstance().getCurrentAction();
if (action != null) {
action.setResult(new ActionResult(TransResult.SUCC, result.getData()));
} else {
finish();
}
}
}

7、效果图

如何在两个activity之间传递bitmap

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903