feat: 优化幻灯片播放体验
- 添加淡入淡出过渡动画,切换更平滑 - 添加缩放模式切换按钮(适应/填充屏幕) - 使用双 ImageView 实现无缝切换效果 - 优化控制面板按钮布局 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
32c3e76888
commit
4f7eb94047
2 files changed
+143
-29
No files matched your search
@@ -13,6 +13,7 @@ import android.widget.TextView;
|
|||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
import com.bumptech.glide.Glide;
|
import com.bumptech.glide.Glide;
|
||||||
|
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
|
||||||
import com.google.android.material.button.MaterialButton;
|
import com.google.android.material.button.MaterialButton;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -22,15 +23,17 @@ import cn.hugh.bz.gk2.utils.FileLogger;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 幻灯片播放器 Activity
|
* 幻灯片播放器 Activity
|
||||||
* 全屏播放图片,支持播放控制和间隔调整
|
* 全屏播放图片,支持播放控制、间隔调整和缩放模式切换
|
||||||
*/
|
*/
|
||||||
public class SlideshowPlayerActivity extends AppCompatActivity {
|
public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||||
private static final String TAG = "SlideshowPlayerActivity";
|
private static final String TAG = "SlideshowPlayerActivity";
|
||||||
private static final int MIN_INTERVAL = 3000; // 3秒
|
private static final int MIN_INTERVAL = 3000; // 3秒
|
||||||
private static final int MAX_INTERVAL = 15000; // 15秒
|
private static final int MAX_INTERVAL = 15000; // 15秒
|
||||||
private static final int CONTROL_HIDE_DELAY = 3000; // 3秒后隐藏控制面板
|
private static final int CONTROL_HIDE_DELAY = 3000; // 3秒后隐藏控制面板
|
||||||
|
private static final int CROSSFADE_DURATION = 500; // 淡入淡出时长
|
||||||
|
|
||||||
private ImageView ivSlideshow;
|
private ImageView ivSlideshow;
|
||||||
|
private ImageView ivSlideshowBack;
|
||||||
private View controlPanel;
|
private View controlPanel;
|
||||||
private TextView tvProgress;
|
private TextView tvProgress;
|
||||||
private SeekBar seekBarInterval;
|
private SeekBar seekBarInterval;
|
||||||
@@ -38,6 +41,7 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
private MaterialButton btnPrevious;
|
private MaterialButton btnPrevious;
|
||||||
private MaterialButton btnPlayPause;
|
private MaterialButton btnPlayPause;
|
||||||
private MaterialButton btnNext;
|
private MaterialButton btnNext;
|
||||||
|
private MaterialButton btnScaleMode;
|
||||||
private MaterialButton btnExit;
|
private MaterialButton btnExit;
|
||||||
private View endScreen;
|
private View endScreen;
|
||||||
private MaterialButton btnReplay;
|
private MaterialButton btnReplay;
|
||||||
@@ -47,6 +51,8 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
private int currentIndex = 0;
|
private int currentIndex = 0;
|
||||||
private boolean isPlaying = true;
|
private boolean isPlaying = true;
|
||||||
private int currentInterval = 5000; // 默认5秒
|
private int currentInterval = 5000; // 默认5秒
|
||||||
|
private boolean isFullScreen = false; // false=适应屏幕(fitCenter), true=填充屏幕(centerCrop)
|
||||||
|
private boolean useFrontImage = true; // 用于交替显示两个 ImageView
|
||||||
|
|
||||||
private Handler playHandler;
|
private Handler playHandler;
|
||||||
private Runnable playRunnable;
|
private Runnable playRunnable;
|
||||||
@@ -95,7 +101,7 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
setupControls();
|
setupControls();
|
||||||
logger.log(TAG, "onCreate: setupControls done");
|
logger.log(TAG, "onCreate: setupControls done");
|
||||||
|
|
||||||
loadImage(currentIndex);
|
loadImage(currentIndex, false);
|
||||||
logger.log(TAG, "onCreate: loadImage done");
|
logger.log(TAG, "onCreate: loadImage done");
|
||||||
|
|
||||||
startAutoPlay();
|
startAutoPlay();
|
||||||
@@ -109,6 +115,7 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
private void initViews() {
|
private void initViews() {
|
||||||
ivSlideshow = findViewById(R.id.ivSlideshow);
|
ivSlideshow = findViewById(R.id.ivSlideshow);
|
||||||
|
ivSlideshowBack = findViewById(R.id.ivSlideshowBack);
|
||||||
controlPanel = findViewById(R.id.controlPanel);
|
controlPanel = findViewById(R.id.controlPanel);
|
||||||
tvProgress = findViewById(R.id.tvProgress);
|
tvProgress = findViewById(R.id.tvProgress);
|
||||||
seekBarInterval = findViewById(R.id.seekBarInterval);
|
seekBarInterval = findViewById(R.id.seekBarInterval);
|
||||||
@@ -116,6 +123,7 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
btnPrevious = findViewById(R.id.btnPrevious);
|
btnPrevious = findViewById(R.id.btnPrevious);
|
||||||
btnPlayPause = findViewById(R.id.btnPlayPause);
|
btnPlayPause = findViewById(R.id.btnPlayPause);
|
||||||
btnNext = findViewById(R.id.btnNext);
|
btnNext = findViewById(R.id.btnNext);
|
||||||
|
btnScaleMode = findViewById(R.id.btnScaleMode);
|
||||||
btnExit = findViewById(R.id.btnExit);
|
btnExit = findViewById(R.id.btnExit);
|
||||||
endScreen = findViewById(R.id.endScreen);
|
endScreen = findViewById(R.id.endScreen);
|
||||||
btnReplay = findViewById(R.id.btnReplay);
|
btnReplay = findViewById(R.id.btnReplay);
|
||||||
@@ -123,6 +131,10 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
// 点击屏幕显示/隐藏控制面板
|
// 点击屏幕显示/隐藏控制面板
|
||||||
ivSlideshow.setOnClickListener(v -> toggleControlPanel());
|
ivSlideshow.setOnClickListener(v -> toggleControlPanel());
|
||||||
|
ivSlideshowBack.setOnClickListener(v -> toggleControlPanel());
|
||||||
|
|
||||||
|
// 更新缩放模式图标
|
||||||
|
updateScaleModeIcon();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupHandlers() {
|
private void setupHandlers() {
|
||||||
@@ -187,6 +199,11 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
resetHideTimer();
|
resetHideTimer();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
btnScaleMode.setOnClickListener(v -> {
|
||||||
|
toggleScaleMode();
|
||||||
|
resetHideTimer();
|
||||||
|
});
|
||||||
|
|
||||||
btnExit.setOnClickListener(v -> finish());
|
btnExit.setOnClickListener(v -> finish());
|
||||||
|
|
||||||
// 播放结束按钮
|
// 播放结束按钮
|
||||||
@@ -194,16 +211,61 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
btnExitEnd.setOnClickListener(v -> finish());
|
btnExitEnd.setOnClickListener(v -> finish());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void loadImage(int index) {
|
/**
|
||||||
|
* 加载图片,支持淡入淡出动画
|
||||||
|
* @param index 图片索引
|
||||||
|
* @param animate 是否使用动画
|
||||||
|
*/
|
||||||
|
private void loadImage(int index, boolean animate) {
|
||||||
if (index < 0 || index >= imageUris.size()) {
|
if (index < 0 || index >= imageUris.size()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String uri = imageUris.get(index);
|
String uri = imageUris.get(index);
|
||||||
Glide.with(this)
|
ImageView.ScaleType scaleType = isFullScreen ? ImageView.ScaleType.CENTER_CROP : ImageView.ScaleType.FIT_CENTER;
|
||||||
.load(uri)
|
|
||||||
.centerInside()
|
if (animate) {
|
||||||
.into(ivSlideshow);
|
// 使用双 ImageView 实现淡入淡出
|
||||||
|
ImageView frontView = useFrontImage ? ivSlideshow : ivSlideshowBack;
|
||||||
|
ImageView backView = useFrontImage ? ivSlideshowBack : ivSlideshow;
|
||||||
|
|
||||||
|
// 设置缩放模式
|
||||||
|
frontView.setScaleType(scaleType);
|
||||||
|
backView.setScaleType(scaleType);
|
||||||
|
|
||||||
|
// 将新图片加载到后面的 ImageView
|
||||||
|
Glide.with(this)
|
||||||
|
.load(uri)
|
||||||
|
.into(backView);
|
||||||
|
|
||||||
|
// 淡入淡出动画
|
||||||
|
backView.setAlpha(0f);
|
||||||
|
backView.animate()
|
||||||
|
.alpha(1f)
|
||||||
|
.setDuration(CROSSFADE_DURATION)
|
||||||
|
.start();
|
||||||
|
|
||||||
|
frontView.animate()
|
||||||
|
.alpha(0f)
|
||||||
|
.setDuration(CROSSFADE_DURATION)
|
||||||
|
.withEndAction(() -> {
|
||||||
|
// 动画结束后清除前面的图片
|
||||||
|
frontView.setImageDrawable(null);
|
||||||
|
frontView.setAlpha(1f);
|
||||||
|
})
|
||||||
|
.start();
|
||||||
|
|
||||||
|
// 切换前后
|
||||||
|
useFrontImage = !useFrontImage;
|
||||||
|
} else {
|
||||||
|
// 首次加载,不使用动画
|
||||||
|
ivSlideshow.setScaleType(scaleType);
|
||||||
|
ivSlideshowBack.setScaleType(scaleType);
|
||||||
|
|
||||||
|
Glide.with(this)
|
||||||
|
.load(uri)
|
||||||
|
.into(ivSlideshow);
|
||||||
|
}
|
||||||
|
|
||||||
updateProgress();
|
updateProgress();
|
||||||
}
|
}
|
||||||
@@ -225,13 +287,13 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
showEndScreen();
|
showEndScreen();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
loadImage(currentIndex);
|
loadImage(currentIndex, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void showPrevious() {
|
private void showPrevious() {
|
||||||
if (currentIndex > 0) {
|
if (currentIndex > 0) {
|
||||||
currentIndex--;
|
currentIndex--;
|
||||||
loadImage(currentIndex);
|
loadImage(currentIndex, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,6 +328,33 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
playHandler.removeCallbacks(playRunnable);
|
playHandler.removeCallbacks(playRunnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 切换缩放模式
|
||||||
|
*/
|
||||||
|
private void toggleScaleMode() {
|
||||||
|
isFullScreen = !isFullScreen;
|
||||||
|
updateScaleModeIcon();
|
||||||
|
|
||||||
|
// 更新当前图片的缩放模式
|
||||||
|
ImageView.ScaleType scaleType = isFullScreen ? ImageView.ScaleType.CENTER_CROP : ImageView.ScaleType.FIT_CENTER;
|
||||||
|
ivSlideshow.setScaleType(scaleType);
|
||||||
|
ivSlideshowBack.setScaleType(scaleType);
|
||||||
|
|
||||||
|
// 重新加载当前图片以应用新的缩放模式
|
||||||
|
loadImage(currentIndex, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateScaleModeIcon() {
|
||||||
|
// 使用不同图标表示当前模式
|
||||||
|
if (isFullScreen) {
|
||||||
|
// 当前是填充模式,点击切换到适应模式
|
||||||
|
btnScaleMode.setIcon(getDrawable(android.R.drawable.ic_menu_crop));
|
||||||
|
} else {
|
||||||
|
// 当前是适应模式,点击切换到填充模式
|
||||||
|
btnScaleMode.setIcon(getDrawable(android.R.drawable.ic_menu_view));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void toggleControlPanel() {
|
private void toggleControlPanel() {
|
||||||
if (controlPanel.getVisibility() == View.VISIBLE) {
|
if (controlPanel.getVisibility() == View.VISIBLE) {
|
||||||
hideControlPanel();
|
hideControlPanel();
|
||||||
@@ -316,7 +405,7 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
private void replay() {
|
private void replay() {
|
||||||
currentIndex = 0;
|
currentIndex = 0;
|
||||||
loadImage(currentIndex);
|
loadImage(currentIndex, false);
|
||||||
endScreen.setVisibility(View.GONE);
|
endScreen.setVisibility(View.GONE);
|
||||||
isPlaying = true;
|
isPlaying = true;
|
||||||
btnPlayPause.setIcon(getDrawable(android.R.drawable.ic_media_pause));
|
btnPlayPause.setIcon(getDrawable(android.R.drawable.ic_media_pause));
|
||||||
|
|||||||
@@ -6,12 +6,19 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:background="@android:color/black">
|
android:background="@android:color/black">
|
||||||
|
|
||||||
<!-- 全屏图片 -->
|
<!-- 双 ImageView 用于淡入淡出切换 -->
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/ivSlideshowBack"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:scaleType="fitCenter"
|
||||||
|
android:contentDescription="幻灯片图片背景" />
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/ivSlideshow"
|
android:id="@+id/ivSlideshow"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:scaleType="centerInside"
|
android:scaleType="fitCenter"
|
||||||
android:contentDescription="幻灯片图片" />
|
android:contentDescription="幻灯片图片" />
|
||||||
|
|
||||||
<!-- 控制面板 -->
|
<!-- 控制面板 -->
|
||||||
@@ -42,7 +49,7 @@
|
|||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
android:gravity="center_vertical"
|
android:gravity="center_vertical"
|
||||||
android:layout_marginBottom="16dp">
|
android:layout_marginBottom="12dp">
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
@@ -84,49 +91,67 @@
|
|||||||
<com.google.android.material.button.MaterialButton
|
<com.google.android.material.button.MaterialButton
|
||||||
android:id="@+id/btnPrevious"
|
android:id="@+id/btnPrevious"
|
||||||
style="@style/Widget.Material3.Button.IconButton"
|
style="@style/Widget.Material3.Button.IconButton"
|
||||||
android:layout_width="56dp"
|
android:layout_width="48dp"
|
||||||
android:layout_height="56dp"
|
android:layout_height="48dp"
|
||||||
app:icon="@android:drawable/ic_media_previous"
|
app:icon="@android:drawable/ic_media_previous"
|
||||||
app:iconSize="32dp"
|
app:iconSize="28dp"
|
||||||
app:iconTint="@android:color/white"
|
app:iconTint="@android:color/white"
|
||||||
app:backgroundTint="#40FFFFFF"
|
app:backgroundTint="#40FFFFFF"
|
||||||
android:layout_marginEnd="8dp" />
|
android:layout_marginEnd="4dp" />
|
||||||
|
|
||||||
<!-- 播放/暂停 -->
|
<!-- 播放/暂停 -->
|
||||||
<com.google.android.material.button.MaterialButton
|
<com.google.android.material.button.MaterialButton
|
||||||
android:id="@+id/btnPlayPause"
|
android:id="@+id/btnPlayPause"
|
||||||
style="@style/Widget.Material3.Button.IconButton"
|
style="@style/Widget.Material3.Button.IconButton"
|
||||||
android:layout_width="64dp"
|
android:layout_width="56dp"
|
||||||
android:layout_height="64dp"
|
android:layout_height="56dp"
|
||||||
app:icon="@android:drawable/ic_media_pause"
|
app:icon="@android:drawable/ic_media_pause"
|
||||||
app:iconSize="36dp"
|
app:iconSize="32dp"
|
||||||
app:iconTint="@android:color/white"
|
app:iconTint="@android:color/white"
|
||||||
app:backgroundTint="#60FFFFFF"
|
app:backgroundTint="#60FFFFFF"
|
||||||
android:layout_marginHorizontal="8dp" />
|
android:layout_marginHorizontal="4dp" />
|
||||||
|
|
||||||
<!-- 下一张 -->
|
<!-- 下一张 -->
|
||||||
<com.google.android.material.button.MaterialButton
|
<com.google.android.material.button.MaterialButton
|
||||||
android:id="@+id/btnNext"
|
android:id="@+id/btnNext"
|
||||||
style="@style/Widget.Material3.Button.IconButton"
|
style="@style/Widget.Material3.Button.IconButton"
|
||||||
android:layout_width="56dp"
|
android:layout_width="48dp"
|
||||||
android:layout_height="56dp"
|
android:layout_height="48dp"
|
||||||
app:icon="@android:drawable/ic_media_next"
|
app:icon="@android:drawable/ic_media_next"
|
||||||
app:iconSize="32dp"
|
app:iconSize="28dp"
|
||||||
app:iconTint="@android:color/white"
|
app:iconTint="@android:color/white"
|
||||||
app:backgroundTint="#40FFFFFF"
|
app:backgroundTint="#40FFFFFF"
|
||||||
android:layout_marginStart="8dp" />
|
android:layout_marginStart="4dp" />
|
||||||
|
|
||||||
|
<!-- 分隔 -->
|
||||||
|
<View
|
||||||
|
android:layout_width="1dp"
|
||||||
|
android:layout_height="32dp"
|
||||||
|
android:background="#40FFFFFF"
|
||||||
|
android:layout_marginHorizontal="12dp" />
|
||||||
|
|
||||||
|
<!-- 全屏/适应切换 -->
|
||||||
|
<com.google.android.material.button.MaterialButton
|
||||||
|
android:id="@+id/btnScaleMode"
|
||||||
|
style="@style/Widget.Material3.Button.IconButton"
|
||||||
|
android:layout_width="48dp"
|
||||||
|
android:layout_height="48dp"
|
||||||
|
app:icon="@android:drawable/ic_menu_crop"
|
||||||
|
app:iconSize="24dp"
|
||||||
|
app:iconTint="@android:color/white"
|
||||||
|
app:backgroundTint="#40FFFFFF" />
|
||||||
|
|
||||||
<!-- 退出 -->
|
<!-- 退出 -->
|
||||||
<com.google.android.material.button.MaterialButton
|
<com.google.android.material.button.MaterialButton
|
||||||
android:id="@+id/btnExit"
|
android:id="@+id/btnExit"
|
||||||
style="@style/Widget.Material3.Button.IconButton"
|
style="@style/Widget.Material3.Button.IconButton"
|
||||||
android:layout_width="56dp"
|
android:layout_width="48dp"
|
||||||
android:layout_height="56dp"
|
android:layout_height="48dp"
|
||||||
app:icon="@android:drawable/ic_menu_close_clear_cancel"
|
app:icon="@android:drawable/ic_menu_close_clear_cancel"
|
||||||
app:iconSize="28dp"
|
app:iconSize="24dp"
|
||||||
app:iconTint="@android:color/white"
|
app:iconTint="@android:color/white"
|
||||||
app:backgroundTint="#40FF0000"
|
app:backgroundTint="#40FF0000"
|
||||||
android:layout_marginStart="16dp" />
|
android:layout_marginStart="4dp" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user