2929import java .util .HashMap ;
3030import java .util .Locale ;
3131import java .util .Map ;
32+ import java .util .concurrent .CompletableFuture ;
33+ import java .util .concurrent .ExecutorService ;
34+ import java .util .concurrent .Executors ;
3235
3336
3437public class PrintTicketActivity extends PrinterBaseActivity <ActivityPrinterBaseBinding , PrintTicketViewModel > {
@@ -83,21 +86,21 @@ public int initVariableId() {
8386 }
8487
8588 private void setupViews () {
86- // 清理旧的位图资源
89+ // Clean up old bitmap resources
8790 cleanupBitmap ();
88-
89- // 构建参数映射
9091 Map <String , String > map = buildParameterMap ();
9192
92- // 生成收据位图并处理结果
93- viewModel .generateReceiptBitmap (map );
94- observeReceiptBitmap ();
93+ //Generate receipt bitmap and process the result
94+ // viewModel.generateReceiptBitmap(map);
95+
96+ generateReceiptInBackground (map );
9597
98+ // observeReceiptBitmap();
9699 initViewOnlick (map );
97100 }
98101
99102 /**
100- * 清理旧的位图资源
103+ *Clean up old bitmap resources
101104 */
102105 private void cleanupBitmap () {
103106 if (mBitmap != null && !mBitmap .isRecycled ()) {
@@ -107,7 +110,7 @@ private void cleanupBitmap() {
107110 }
108111
109112 /**
110- * 构建参数映射
113+ *Build parameter mapping
111114 */
112115 private Map <String , String > buildParameterMap () {
113116 Map <String , String > map = new HashMap <>();
@@ -119,25 +122,20 @@ private Map<String, String> buildParameterMap() {
119122 return map ;
120123 }
121124
122- /**
123- * 获取安全的非空字符串
124- */
125125 private String getSafeString (String value ) {
126126 return !TextUtils .isEmpty (value ) ? value : "" ;
127127 }
128128
129129 /**
130- * 构建终端时间字符串
130+ *Build terminal time string
131131 */
132132 private String buildTerminalTimeString () {
133133 if (TextUtils .isEmpty (terminalTime )) {
134134 return getSafeString (transactionTime );
135135 }
136136
137- // 格式化时间
138137 String formattedTime = formatDateTime (terminalTime );
139138
140- // 追加交易时间
141139 if (!TextUtils .isEmpty (transactionTime )) {
142140 return formattedTime + " " + transactionTime ;
143141 }
@@ -146,7 +144,7 @@ private String buildTerminalTimeString() {
146144 }
147145
148146 /**
149- * 格式化日期时间
147+ *Format date and time
150148 */
151149 private String formatDateTime (String dateTimeStr ) {
152150 if (TextUtils .isEmpty (dateTimeStr )) {
@@ -161,7 +159,7 @@ private String formatDateTime(String dateTimeStr) {
161159 return outputFormat .format (date );
162160 } catch (ParseException e ) {
163161 e .printStackTrace ();
164- return dateTimeStr ; // 解析失败时返回原字符串
162+ return dateTimeStr ;
165163 }
166164 }
167165
@@ -193,7 +191,7 @@ public void onClick(View view) {
193191 startPrintAnimation ();
194192 } else {
195193 Toast .makeText (PrintTicketActivity .this , "Receipt not ready" , Toast .LENGTH_SHORT ).show ();
196- // 重新生成bitmap
194+ //Regenerate bitmap
197195 viewModel .generateReceiptBitmap (map );
198196 }
199197 }
@@ -217,7 +215,7 @@ public void onClick(View view) {
217215 viewModel .printTicket (mBitmap );
218216 } else {
219217 Toast .makeText (PrintTicketActivity .this , "Receipt not ready" , Toast .LENGTH_SHORT ).show ();
220- // 重新生成bitmap
218+ //Regenerate bitmap
221219 viewModel .generateReceiptBitmap (map );
222220 }
223221 }
@@ -246,24 +244,23 @@ public void run() {
246244 imageHeight = screenHeight / 3 ;
247245 }
248246
249- // 计算需要移动的总距离(从当前位置移动到完全离开屏幕)
247+ // Calculate the total distance needed to move (from the current position to completely exit the screen)
250248 float currentY = contentBinding .receiptImage .getY ();
251249 float moveDistance = -(currentY + imageHeight );
252250
253- // 设置移动速度(像素/毫秒)
254- float moveSpeed = 0.2f ; // 0.8像素/毫秒
251+ //Set movement speed (pixels/milliseconds)
252+ float moveSpeed = 0.2f ;
255253
256- // 根据距离和速度计算持续时间
254+ //Calculate duration based on distance and speed
257255 long duration = (long ) (Math .abs (moveDistance ) / moveSpeed );
258256
259- // 限制动画时间范围
257+ //Limit animation time range
260258 duration = Math .max (1000 , Math .min (duration , 3000 ));
261259
262260 ObjectAnimator translateAnim = ObjectAnimator .ofFloat (contentBinding .receiptImage , "translationY" , 0f , moveDistance );
263261
264262 ObjectAnimator alphaAnim = ObjectAnimator .ofFloat (contentBinding .receiptImage , "alpha" , 1f , 0f );
265263
266- // 透明度动画应该比移动动画稍快
267264 alphaAnim .setDuration ((long ) (duration * 0.8 ));
268265 AnimatorSet animatorSet = new AnimatorSet ();
269266 animatorSet .playTogether (translateAnim , alphaAnim );
@@ -335,13 +332,9 @@ public void onDismiss() {
335332 }
336333 });
337334 }
338-
339-
340335 private void regenerateReceipt () {
341- // 确保在主线程执行
342336 runOnUiThread (() -> {
343- // 显示加载状态
344- // 延迟一下再重新生成,避免立即重试可能的问题
337+
345338 contentBinding .receiptImage .postDelayed (() -> {
346339 PrintTicketActivity .this .finish ();
347340 }, 100 );
@@ -351,10 +344,23 @@ private void regenerateReceipt() {
351344 @ Override
352345 protected void onDestroy () {
353346 super .onDestroy ();
354- // 清理bitmap资源
355347 if (mBitmap != null && !mBitmap .isRecycled ()) {
356348 mBitmap .recycle ();
357349 mBitmap = null ;
358350 }
359351 }
352+
353+ private static final ExecutorService BITMAP_EXECUTOR = Executors .newFixedThreadPool (2 );
354+
355+ private void generateReceiptInBackground (Map <String , String > map ) {
356+ contentBinding .receiptImage .setVisibility (View .INVISIBLE );
357+ viewModel .setShowLoading (true );
358+ CompletableFuture
359+ .runAsync (() -> viewModel .generateReceiptBitmap (map ), BITMAP_EXECUTOR )
360+ .thenRunAsync (() -> {
361+ viewModel .setShowLoading (false );
362+ contentBinding .receiptImage .setVisibility (View .VISIBLE );
363+ observeReceiptBitmap ();
364+ }, this ::runOnUiThread );
365+ }
360366}
0 commit comments