Skip to content

Instantly share code, notes, and snippets.

@truongngoclinh
Last active July 17, 2019 10:27
Show Gist options
  • Save truongngoclinh/51b3a2ffcbd09030be3b5b74fcfc6148 to your computer and use it in GitHub Desktop.
Save truongngoclinh/51b3a2ffcbd09030be3b5b74fcfc6148 to your computer and use it in GitHub Desktop.
private Bitmap generateReceiptBitmap(ArrayList<PrinterDatum> data) {
ReceiptViewAdapter adapter = new ReceiptViewAdapter();
RecyclerView rv = new RecyclerView(mActivity);
rv.setLayoutManager(new LinearLayoutManager(mActivity, LinearLayoutManager.VERTICAL, false));
rv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
rv.setAdapter(adapter);
adapter.setData(data);
return loadBitmapFromView(rv);
}
public Bitmap loadBitmapFromView(View v) {
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
v.measure(spec, spec);
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredWidth(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
c.translate((-v.getScrollX()), (-v.getScrollY()));
v.draw(c);
return b;
}
public Bitmap loadBitMapFromRecyclerView(RecyclerView view) {
ReceiptViewAdapter adapter = (ReceiptViewAdapter) view.getAdapter();
Bitmap result = null;
if (adapter != null) {
int size = adapter.getItemCount();
int height = 0;
int width = 0;
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
LruCache<String, Bitmap> bmCache = new LruCache<>(cacheSize);
for (int i = 0; i < size; i++) {
BaseRecyclerAdapter.ViewHolder holder = adapter.createHolder(view, adapter.getItemViewType(i));
adapter.onBindViewHolder(holder, i);
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
holder.itemView.setDrawingCacheEnabled(true);
holder.itemView.measure(spec, spec);
holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight());
holder.itemView.buildDrawingCache();
Bitmap bmDrawingCache = Bitmap.createBitmap(holder.itemView.getDrawingCache());
if (bmDrawingCache != null) {
bmCache.put(String.valueOf(i), bmDrawingCache);
}
holder.itemView.setDrawingCacheEnabled(false);
holder.itemView.destroyDrawingCache();
height += holder.itemView.getMeasuredHeight();
width = (width == 0 || width < holder.itemView.getMeasuredWidth())
? holder.itemView.getMeasuredWidth()
: width;
}
result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas bigCanvas = new Canvas(result);
bigCanvas.drawColor(Color.WHITE);
Paint p = new Paint();
int h = 0;
for (int i = 0; i < size; i++) {
Bitmap bm = bmCache.get(String.valueOf(i));
bigCanvas.drawBitmap(bm, 0f, h, p);
h += bm.getHeight();
bm.recycle();
}
bmCache.evictAll();
}
return result;
}
private static class ReceiptViewAdapter extends BaseRecyclerAdapter<PrinterDatum, BaseRecyclerAdapter.ViewHolder<PrinterDatum>> {
private final static int TYPE_TEXT = 1;
private final static int QRCODE = 2;
private final static int LINEFEED = 3;
private final static int LOGO = 4;
@Override
public int getItemViewType(int position) {
PrinterDatum item = mData.get(position);
if (item.printerDataType == PrinterDatum.PrinterDataType.TEXT) {
return TYPE_TEXT;
}
if (item.printerDataType == PrinterDatum.PrinterDataType.LOGO) {
return LOGO;
}
return 1;
}
@Override
public BaseRecyclerAdapter.ViewHolder createHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case TYPE_TEXT:
return TextViewHolder.create(parent.getContext());
case LOGO:
return LogoViewHolder.create(parent.getContext());
default:
return null;
}
}
static class TextViewHolder extends BaseRecyclerAdapter.ViewHolder<PrinterDatum> {
private RobotoTextView mTextView;
public static TextViewHolder create(Context context) {
View v = LayoutInflater.from(context).inflate(R.layout.com_garena_cyberpay_app_activity_wifi_printer_item_text, null);
return new TextViewHolder(v);
}
private TextViewHolder(View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.cp_row_text);
}
@Override
public void bindData(PrinterDatum data, int position) {
mTextView.setText(data.data);
}
}
static class LogoViewHolder extends BaseRecyclerAdapter.ViewHolder<PrinterDatum> {
private final Context mContext;
private ImageView mImageView;
public static LogoViewHolder create(Context context) {
View v = LayoutInflater.from(context).inflate(R.layout.com_garena_cyberpay_app_activity_wifi_printer_item_logo, null);
return new LogoViewHolder(v, context);
}
private LogoViewHolder(View itemView, Context context) {
super(itemView);
mContext = context;
mImageView = itemView.findViewById(R.id.cp_row_image);
}
@Override
public void bindData(PrinterDatum data, int position) {
try {
InputStream is = mContext.getAssets().open(data.data + ".png");
Drawable d = Drawable.createFromStream(is, null);
mImageView.setImageDrawable(d);
} catch (IOException e) {
Sentry.e(e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment