Skip to content

Commit

Permalink
初步搭建动画模块
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujiang2 committed Mar 30, 2021
1 parent edcd0eb commit c09dc4a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
19 changes: 10 additions & 9 deletions banner/src/main/java/com/zj/banner/BannerPager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand All @@ -25,33 +25,33 @@ 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 <T : BaseBannerBean> BannerPager(
modifier: Modifier = Modifier,
items: List<T> = arrayListOf(),
repeat: Boolean = true,
config: BannerConfig = BannerConfig(),
indicator: Indicator = CircleIndicator(),
transformer: BasePageTransformer? = null,
onBannerClick: (T) -> Unit
) {
if (items.isEmpty()) {
throw NullPointerException("items is not null")
}

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,
Expand All @@ -75,7 +75,8 @@ fun <T : BaseBannerBean> 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()
Expand Down
8 changes: 8 additions & 0 deletions banner/src/main/java/com/zj/banner/ui/Pager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -107,6 +108,13 @@ class PagerState(

override fun toString(): String = "PagerState{minPage=$minPage, maxPage=$maxPage, " +
"currentPage=$currentPage, currentPageOffset=$currentPageOffset}"

/**
* 设置图片切换动画
*/
fun setTransformer(transformer: BasePageTransformer?) {

}
}

@Immutable
Expand Down
7 changes: 6 additions & 1 deletion banner/src/main/java/com/zj/banner/ui/config/BannerConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 高度
Expand All @@ -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
)

0 comments on commit c09dc4a

Please sign in to comment.