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>
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();
}
}
}