feat: 在 WallpaperEngine 中实现双击检测功能
- 添加双击检测相关字段(lastTapTime 和 DOUBLE_TAP_TIMEOUT) - 重写 onTouchEvent 方法处理触摸事件 - 双击间隔设置为 300 毫秒 - 检测到双击时触发强制更换壁纸 - 支持通过配置开关启用/禁用双击功能 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
a3599b056f
commit
520435831f
1 file changed
+34
@@ -125,6 +125,10 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
private boolean isCharging = true; // Default to charging to avoid pausing initially
|
||||
private boolean changeOnReturn = false; // 标志:是否因返回桌面而需要更换壁纸
|
||||
|
||||
// 双击检测相关
|
||||
private long lastTapTime = 0;
|
||||
private static final long DOUBLE_TAP_TIMEOUT = 300; // 双击间隔时间(毫秒)
|
||||
|
||||
public void handleBatteryChanged(boolean isCharging) {
|
||||
this.isCharging = isCharging;
|
||||
if (currentMedia != null && currentMedia.isVideo() && isPlayingVideo && visible) {
|
||||
@@ -897,5 +901,35 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
canvas.drawText("No media selected", centerX, centerY, paint);
|
||||
canvas.drawText("or album disabled", centerX, centerY + 60, paint);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTouchEvent(android.view.MotionEvent event) {
|
||||
super.onTouchEvent(event);
|
||||
|
||||
// 只处理 ACTION_DOWN 事件
|
||||
if (event.getAction() != android.view.MotionEvent.ACTION_DOWN) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否启用了双击功能
|
||||
AppConfig config = prefs.loadConfig();
|
||||
if (!config.isDoubleTapEnabled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long timeDiff = currentTime - lastTapTime;
|
||||
|
||||
if (timeDiff < DOUBLE_TAP_TIMEOUT) {
|
||||
// 检测到双击
|
||||
fileLogger.log(TAG, "Double tap detected, changing wallpaper");
|
||||
forceChangeNext = true;
|
||||
handler.post(this::drawWallpaper);
|
||||
lastTapTime = 0; // 重置,避免三击被识别为两次双击
|
||||
} else {
|
||||
// 记录单击时间
|
||||
lastTapTime = currentTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user