Skip to content

Commit

Permalink
去除依赖,使用原生实现
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujiang2 committed Jul 19, 2023
1 parent 9e686ee commit 7ac29de
Show file tree
Hide file tree
Showing 6 changed files with 426 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 1 addition & 5 deletions banner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ afterEvaluate {
// You can then customize attributes of the publication as shown below.
groupId = 'com.zj.banner'
artifactId = 'banner'
version = '2.5.0'
version = '2.5.2'
}
// // Creates a Maven publication called “debug”.
// debug(MavenPublication) {
Expand Down Expand Up @@ -71,8 +71,4 @@ dependencies {

implementation 'io.coil-kt:coil-compose:2.4.0'

def accompanist_version = "0.31.5-beta"
api "com.google.accompanist:accompanist-pager:$accompanist_version"
api "com.google.accompanist:accompanist-pager-indicators:$accompanist_version"

}
99 changes: 57 additions & 42 deletions banner/src/main/java/com/zj/banner/BannerPager.kt
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
@file:OptIn(ExperimentalFoundationApi::class)

package com.zj.banner

import android.util.Log
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.lerp
import com.google.accompanist.pager.*
import com.zj.banner.model.BaseBannerBean
import com.zj.banner.ui.BannerCard
import com.zj.banner.ui.config.BannerConfig
import com.zj.banner.utils.HorizontalPagerIndicator
import com.zj.banner.utils.VerticalPagerIndicator
import kotlinx.coroutines.launch
import java.util.*
import kotlin.math.absoluteValue
Expand All @@ -32,7 +39,6 @@ private const val TAG = "BannerPager"
* @param indicatorGravity Banner 指示器位置,直接使用 Alignment 即可进行设定
* @param onBannerClick Banner 点击事件的回调
*/
@OptIn(ExperimentalPagerApi::class)
@Composable
fun <T : BaseBannerBean> BannerPager(
modifier: Modifier = Modifier,
Expand All @@ -46,65 +52,76 @@ fun <T : BaseBannerBean> BannerPager(
throw NullPointerException("items is not null")
}

val pagerState = rememberPagerState()
val pagerState = rememberPagerState(
initialPage = 0,
initialPageOffsetFraction = 0f
) {
items.size
}

if (config.repeat) {
StartBanner(pagerState, config.intervalTime)
}

Box(modifier = modifier.height(config.bannerHeight)) {
HorizontalPager(
count = items.size,
modifier = Modifier,
state = pagerState,
itemSpacing = config.itemSpacing,
contentPadding = config.contentPadding
) { page ->
val item = items[page]
BannerCard(
bean = item,
modifier = Modifier
.graphicsLayer {
// Calculate the absolute offset for the current page from the
// scroll position. We use the absolute value which allows us to mirror
// any effects for both directions
val pageOffset = calculateCurrentOffsetForPage(page).absoluteValue
key = { items[it].data?:it },
pageContent = { page ->
val item = items[page]

// We animate the scaleX + scaleY, between 85% and 100%
lerp(
start = 0.85f,
stop = 1f,
fraction = 1f - pageOffset.coerceIn(0f, 1f)
).also { scale ->
scaleX = scale
scaleY = scale
}
BannerCard(
bean = item,
modifier = Modifier
.graphicsLayer {
// Calculate the absolute offset for the current page from the
// scroll position. We use the absolute value which allows us to mirror
// any effects for both directions
val offset =
(pagerState.currentPage - page) + pagerState.currentPageOffsetFraction
val pageOffset = offset.absoluteValue

// We animate the scaleX + scaleY, between 85% and 100%
lerp(
start = 0.85f,
stop = 1f,
fraction = 1f - pageOffset.coerceIn(0f, 1f)
).also { scale ->
scaleX = scale
scaleY = scale
}

// We animate the alpha, between 50% and 100%
alpha = lerp(
start = 0.5f,
stop = 1f,
fraction = 1f - pageOffset.coerceIn(0f, 1f)
)
}
.fillMaxSize()
.padding(config.bannerImagePadding),
shape = config.shape,
contentScale = config.contentScale
) {
Log.d(TAG, "item is :${item.javaClass}")
onBannerClick(item)
// We animate the alpha, between 50% and 100%
alpha = lerp(
start = 0.5f,
stop = 1f,
fraction = 1f - pageOffset.coerceIn(0f, 1f)
)
}
.fillMaxSize()
.padding(config.bannerImagePadding),
shape = config.shape,
contentScale = config.contentScale
) {
Log.d(TAG, "item is :${item.javaClass}")
onBannerClick(item)
}
}
}
)

if (indicatorIsVertical) {
VerticalPagerIndicator(
pagerState = pagerState,
pageCount = items.size,
modifier = Modifier
.align(indicatorGravity)
.padding(16.dp),
)
} else {
HorizontalPagerIndicator(
pagerState = pagerState,
pageCount = items.size,
modifier = Modifier
.align(indicatorGravity)
.padding(16.dp),
Expand All @@ -114,8 +131,6 @@ fun <T : BaseBannerBean> BannerPager(
}
}


@ExperimentalPagerApi
@Composable
fun StartBanner(pagerState: PagerState, intervalTime: Long) {
val coroutineScope = rememberCoroutineScope()
Expand Down
Loading

0 comments on commit 7ac29de

Please sign in to comment.