Skip to content

Commit 985ab61

Browse files
committed
fix: optimize printer SDK and update printer sdk version v1.8.1
1 parent f976a4c commit 985ab61

7 files changed

Lines changed: 44 additions & 37 deletions

File tree

pos_android_studio_demo/pos_android_app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ android {
3232
minSdk 24
3333
targetSdk 34
3434
versionCode 119
35-
versionName "8.2.0-2512231430"
35+
versionName "8.2.0-2512261610"
3636
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3737
ndk {
3838
// Set up supported SO library architecture
Binary file not shown.
Binary file not shown.

pos_android_studio_demo/pos_android_app/src/main/java/com/dspread/pos/printerAPI/PrinterHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public Bitmap getTicketBitmap(Context context, Map<String, String> map) throws R
237237
mPrinter.addTexts(new String[]{"AMOUNT:", ""}, new int[]{5, 5}, new int[]{PrintStyle.Alignment.NORMAL, PrintStyle.Alignment.CENTER}, new int[]{PrintStyle.FontStyle.NORMAL, PrintStyle.FontStyle.NORMAL}, 10);
238238
mPrinter.addPrintLintStyle(new PrintLineStyle(PrintStyle.FontStyle.BOLD, PrintLine.CENTER, 12));
239239
mPrinter.addText("$: " + map.get("terAmount"));
240-
mPrinter.feedLines(110);
240+
mPrinter.feedLines(120);
241241
Bitmap receiptBitmap = mPrinter.getReceiptBitmap();
242242
return receiptBitmap;
243243
}

pos_android_studio_demo/pos_android_app/src/main/java/com/dspread/pos/ui/printer/activities/PrintTicketActivity.java

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
import java.util.HashMap;
3030
import java.util.Locale;
3131
import java.util.Map;
32+
import java.util.concurrent.CompletableFuture;
33+
import java.util.concurrent.ExecutorService;
34+
import java.util.concurrent.Executors;
3235

3336

3437
public 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
}

pos_android_studio_demo/pos_android_app/src/main/java/com/dspread/pos/ui/printer/activities/base/BasePrinterViewModel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ public void onPrintComplete(boolean success, String message) {
5353
isLoading.set(false);
5454
resultText.set(message);
5555
}
56+
57+
public void setShowLoading(boolean isShow) {
58+
isLoading.set(isShow);
59+
}
5660
}

pos_android_studio_demo/pos_android_app/src/main/res/layout/activity_print_ticket.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
xmlns:tools="http://schemas.android.com/tools">
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android">
53

64
<data>
75

@@ -16,6 +14,5 @@
1614
android:layout_height="wrap_content"
1715
android:adjustViewBounds="true"
1816
android:background="@mipmap/ic_print_preview"
19-
android:scaleType="centerInside"
20-
android:src="@drawable/ic_printer" />
17+
android:scaleType="centerInside"/>
2118
</layout>

0 commit comments

Comments
 (0)