From c09dc4a0453817ce637c917d26fce0674dfead75 Mon Sep 17 00:00:00 2001 From: zhujiang2 Date: Tue, 30 Mar 2021 21:38:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5=E6=90=AD=E5=BB=BA=E5=8A=A8?= =?UTF-8?q?=E7=94=BB=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/zj/banner/BannerPager.kt | 19 ++++++++++--------- .../src/main/java/com/zj/banner/ui/Pager.kt | 8 ++++++++ .../com/zj/banner/ui/config/BannerConfig.kt | 7 ++++++- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/banner/src/main/java/com/zj/banner/BannerPager.kt b/banner/src/main/java/com/zj/banner/BannerPager.kt index 5b52030..6216a42 100644 --- a/banner/src/main/java/com/zj/banner/BannerPager.kt +++ b/banner/src/main/java/com/zj/banner/BannerPager.kt @@ -14,7 +14,7 @@ import com.zj.banner.ui.config.BannerConfig import com.zj.banner.ui.indicator.CircleIndicator import com.zj.banner.ui.indicator.Indicator import com.zj.banner.ui.indicator.NumberIndicator -import kotlinx.coroutines.CoroutineScope +import com.zj.banner.ui.pagetransformer.BasePageTransformer import kotlinx.coroutines.launch import java.util.* @@ -25,19 +25,19 @@ private const val TAG = "BannerPager" * 这里需要注意的是,数据 Model 必须要继承自 [BaseBannerBean],因为在 [BannerCard] 中需要使用到其中的一些参数 * * @param items 数据 - * @param repeat 是否循环播放 * @param config Banner 的一些配置参数 [BannerConfig] * @param indicator Banner 指示器,你可以使用默认的 [CircleIndicator] 或 [NumberIndicator],也可以自定义,仅需要继承 * [Indicator] 即可。 + * @param transformer 图片切换动画,可以使用默认的,也可以通过继承 [BasePageTransformer] 进行自定义 * @param onBannerClick Banner 点击事件的回调 */ @Composable fun BannerPager( modifier: Modifier = Modifier, items: List = arrayListOf(), - repeat: Boolean = true, config: BannerConfig = BannerConfig(), indicator: Indicator = CircleIndicator(), + transformer: BasePageTransformer? = null, onBannerClick: (T) -> Unit ) { if (items.isEmpty()) { @@ -45,13 +45,13 @@ fun BannerPager( } val pagerState: PagerState = remember { PagerState() } - val coroutineScope = rememberCoroutineScope() - if (repeat) { - startBanner(pagerState, config.intervalTime, coroutineScope) - } - + pagerState.setTransformer(transformer) pagerState.maxPage = (items.size - 1).coerceAtLeast(0) + if (config.repeat) { + startBanner(pagerState, config.intervalTime) + } + Box(modifier = modifier.height(config.bannerHeight)) { Pager( state = pagerState, @@ -75,7 +75,8 @@ fun BannerPager( var mTimer: Timer? = null var mTimerTask:TimerTask? = null -fun startBanner(pagerState: PagerState, intervalTime: Long, coroutineScope: CoroutineScope) { +fun startBanner(pagerState: PagerState, intervalTime: Long) { + val coroutineScope = rememberCoroutineScope() mTimer?.cancel() mTimerTask?.cancel() mTimer = Timer() diff --git a/banner/src/main/java/com/zj/banner/ui/Pager.kt b/banner/src/main/java/com/zj/banner/ui/Pager.kt index 8fee408..e7f5071 100644 --- a/banner/src/main/java/com/zj/banner/ui/Pager.kt +++ b/banner/src/main/java/com/zj/banner/ui/Pager.kt @@ -21,6 +21,7 @@ import androidx.compose.ui.layout.Layout import androidx.compose.ui.layout.Measurable import androidx.compose.ui.layout.ParentDataModifier import androidx.compose.ui.unit.Density +import com.zj.banner.ui.pagetransformer.BasePageTransformer import kotlinx.coroutines.launch import kotlin.math.roundToInt @@ -107,6 +108,13 @@ class PagerState( override fun toString(): String = "PagerState{minPage=$minPage, maxPage=$maxPage, " + "currentPage=$currentPage, currentPageOffset=$currentPageOffset}" + + /** + * 设置图片切换动画 + */ + fun setTransformer(transformer: BasePageTransformer?) { + + } } @Immutable diff --git a/banner/src/main/java/com/zj/banner/ui/config/BannerConfig.kt b/banner/src/main/java/com/zj/banner/ui/config/BannerConfig.kt index 906ec68..790ca25 100644 --- a/banner/src/main/java/com/zj/banner/ui/config/BannerConfig.kt +++ b/banner/src/main/java/com/zj/banner/ui/config/BannerConfig.kt @@ -4,6 +4,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.ui.graphics.Shape import androidx.compose.ui.unit.Dp import androidx.compose.ui.unit.dp +import com.zj.banner.ui.pagetransformer.BasePageTransformer data class BannerConfig( // banner 高度 @@ -13,5 +14,9 @@ data class BannerConfig( // banner 图片的 shape var shape: Shape = RoundedCornerShape(10.dp), // banner 切换间隔时间 - var intervalTime: Long = 3000 + var intervalTime: Long = 3000, + // 是否循环播放 + var repeat: Boolean = true, + // banner 图片切换动画 + var transformer: BasePageTransformer? = null ) \ No newline at end of file