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
+140
-26
No files matched your search
@@ -13,6 +13,7 @@ import android.widget.TextView;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
|
||||
import com.google.android.material.button.MaterialButton;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -22,15 +23,17 @@ import cn.hugh.bz.gk2.utils.FileLogger;
|
||||
|
||||
/**
|
||||
* 幻灯片播放器 Activity
|
||||
* 全屏播放图片,支持播放控制和间隔调整
|
||||
* 全屏播放图片,支持播放控制、间隔调整和缩放模式切换
|
||||
*/
|
||||
public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
private static final String TAG = "SlideshowPlayerActivity";
|
||||
private static final int MIN_INTERVAL = 3000; // 3秒
|
||||
private static final int MAX_INTERVAL = 15000; // 15秒
|
||||
private static final int CONTROL_HIDE_DELAY = 3000; // 3秒后隐藏控制面板
|
||||
private static final int CROSSFADE_DURATION = 500; // 淡入淡出时长
|
||||
|
||||
private ImageView ivSlideshow;
|
||||
private ImageView ivSlideshowBack;
|
||||
private View controlPanel;
|
||||
private TextView tvProgress;
|
||||
private SeekBar seekBarInterval;
|
||||
@@ -38,6 +41,7 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
private MaterialButton btnPrevious;
|
||||
private MaterialButton btnPlayPause;
|
||||
private MaterialButton btnNext;
|
||||
private MaterialButton btnScaleMode;
|
||||
private MaterialButton btnExit;
|
||||
private View endScreen;
|
||||
private MaterialButton btnReplay;
|
||||
@@ -47,6 +51,8 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
private int currentIndex = 0;
|
||||
private boolean isPlaying = true;
|
||||
private int currentInterval = 5000; // 默认5秒
|
||||
private boolean isFullScreen = false; // false=适应屏幕(fitCenter), true=填充屏幕(centerCrop)
|
||||
private boolean useFrontImage = true; // 用于交替显示两个 ImageView
|
||||
|
||||
private Handler playHandler;
|
||||
private Runnable playRunnable;
|
||||
@@ -95,7 +101,7 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
setupControls();
|
||||
logger.log(TAG, "onCreate: setupControls done");
|
||||
|
||||
loadImage(currentIndex);
|
||||
loadImage(currentIndex, false);
|
||||
logger.log(TAG, "onCreate: loadImage done");
|
||||
|
||||
startAutoPlay();
|
||||
@@ -109,6 +115,7 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
|
||||
private void initViews() {
|
||||
ivSlideshow = findViewById(R.id.ivSlideshow);
|
||||
ivSlideshowBack = findViewById(R.id.ivSlideshowBack);
|
||||
controlPanel = findViewById(R.id.controlPanel);
|
||||
tvProgress = findViewById(R.id.tvProgress);
|
||||
seekBarInterval = findViewById(R.id.seekBarInterval);
|
||||
@@ -116,6 +123,7 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
btnPrevious = findViewById(R.id.btnPrevious);
|
||||
btnPlayPause = findViewById(R.id.btnPlayPause);
|
||||
btnNext = findViewById(R.id.btnNext);
|
||||
btnScaleMode = findViewById(R.id.btnScaleMode);
|
||||
btnExit = findViewById(R.id.btnExit);
|
||||
endScreen = findViewById(R.id.endScreen);
|
||||
btnReplay = findViewById(R.id.btnReplay);
|
||||
@@ -123,6 +131,10 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
|
||||
// 点击屏幕显示/隐藏控制面板
|
||||
ivSlideshow.setOnClickListener(v -> toggleControlPanel());
|
||||
ivSlideshowBack.setOnClickListener(v -> toggleControlPanel());
|
||||
|
||||
// 更新缩放模式图标
|
||||
updateScaleModeIcon();
|
||||
}
|
||||
|
||||
private void setupHandlers() {
|
||||
@@ -187,6 +199,11 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
resetHideTimer();
|
||||
});
|
||||
|
||||
btnScaleMode.setOnClickListener(v -> {
|
||||
toggleScaleMode();
|
||||
resetHideTimer();
|
||||
});
|
||||
|
||||
btnExit.setOnClickListener(v -> finish());
|
||||
|
||||
// 播放结束按钮
|
||||
@@ -194,16 +211,61 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
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()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String uri = imageUris.get(index);
|
||||
ImageView.ScaleType scaleType = isFullScreen ? ImageView.ScaleType.CENTER_CROP : ImageView.ScaleType.FIT_CENTER;
|
||||
|
||||
if (animate) {
|
||||
// 使用双 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)
|
||||
.centerInside()
|
||||
.into(ivSlideshow);
|
||||
}
|
||||
|
||||
updateProgress();
|
||||
}
|
||||
@@ -225,13 +287,13 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
showEndScreen();
|
||||
return;
|
||||
}
|
||||
loadImage(currentIndex);
|
||||
loadImage(currentIndex, true);
|
||||
}
|
||||
|
||||
private void showPrevious() {
|
||||
if (currentIndex > 0) {
|
||||
currentIndex--;
|
||||
loadImage(currentIndex);
|
||||
loadImage(currentIndex, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,6 +328,33 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
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() {
|
||||
if (controlPanel.getVisibility() == View.VISIBLE) {
|
||||
hideControlPanel();
|
||||
@@ -316,7 +405,7 @@ public class SlideshowPlayerActivity extends AppCompatActivity {
|
||||
|
||||
private void replay() {
|
||||
currentIndex = 0;
|
||||
loadImage(currentIndex);
|
||||
loadImage(currentIndex, false);
|
||||
endScreen.setVisibility(View.GONE);
|
||||
isPlaying = true;
|
||||
btnPlayPause.setIcon(getDrawable(android.R.drawable.ic_media_pause));
|
||||
|
||||
@@ -6,12 +6,19 @@
|
||||
android:layout_height="match_parent"
|
||||
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
|
||||
android:id="@+id/ivSlideshow"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:scaleType="centerInside"
|
||||
android:scaleType="fitCenter"
|
||||
android:contentDescription="幻灯片图片" />
|
||||
|
||||
<!-- 控制面板 -->
|
||||
@@ -42,7 +49,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:layout_marginBottom="16dp">
|
||||
android:layout_marginBottom="12dp">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
@@ -84,49 +91,67 @@
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/btnPrevious"
|
||||
style="@style/Widget.Material3.Button.IconButton"
|
||||
android:layout_width="56dp"
|
||||
android:layout_height="56dp"
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="48dp"
|
||||
app:icon="@android:drawable/ic_media_previous"
|
||||
app:iconSize="32dp"
|
||||
app:iconSize="28dp"
|
||||
app:iconTint="@android:color/white"
|
||||
app:backgroundTint="#40FFFFFF"
|
||||
android:layout_marginEnd="8dp" />
|
||||
android:layout_marginEnd="4dp" />
|
||||
|
||||
<!-- 播放/暂停 -->
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/btnPlayPause"
|
||||
style="@style/Widget.Material3.Button.IconButton"
|
||||
android:layout_width="64dp"
|
||||
android:layout_height="64dp"
|
||||
android:layout_width="56dp"
|
||||
android:layout_height="56dp"
|
||||
app:icon="@android:drawable/ic_media_pause"
|
||||
app:iconSize="36dp"
|
||||
app:iconSize="32dp"
|
||||
app:iconTint="@android:color/white"
|
||||
app:backgroundTint="#60FFFFFF"
|
||||
android:layout_marginHorizontal="8dp" />
|
||||
android:layout_marginHorizontal="4dp" />
|
||||
|
||||
<!-- 下一张 -->
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/btnNext"
|
||||
style="@style/Widget.Material3.Button.IconButton"
|
||||
android:layout_width="56dp"
|
||||
android:layout_height="56dp"
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="48dp"
|
||||
app:icon="@android:drawable/ic_media_next"
|
||||
app:iconSize="32dp"
|
||||
app:iconSize="28dp"
|
||||
app:iconTint="@android:color/white"
|
||||
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
|
||||
android:id="@+id/btnExit"
|
||||
style="@style/Widget.Material3.Button.IconButton"
|
||||
android:layout_width="56dp"
|
||||
android:layout_height="56dp"
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="48dp"
|
||||
app:icon="@android:drawable/ic_menu_close_clear_cancel"
|
||||
app:iconSize="28dp"
|
||||
app:iconSize="24dp"
|
||||
app:iconTint="@android:color/white"
|
||||
app:backgroundTint="#40FF0000"
|
||||
android:layout_marginStart="16dp" />
|
||||
android:layout_marginStart="4dp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
Reference in new issue
Block a user