Gemini优化图片滚动缓存问题「待验证」见ImageListScreen.kt中的ImageListScreen视图
This commit is contained in:
67
app/src/main/java/com/aiosman/riderpro/ImageListScreen.kt
Normal file
67
app/src/main/java/com/aiosman/riderpro/ImageListScreen.kt
Normal file
@@ -0,0 +1,67 @@
|
||||
import android.content.Context
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.unit.dp
|
||||
import coil.compose.rememberAsyncImagePainter
|
||||
import coil.request.ImageRequest
|
||||
import coil.ImageLoader
|
||||
import coil.disk.DiskCache
|
||||
import coil.memory.MemoryCache
|
||||
|
||||
data class ImageItem(val url: String)
|
||||
|
||||
@Composable
|
||||
fun ImageListScreen(context: Context, imageList: List<ImageItem>) {
|
||||
val imageLoader = getImageLoader(context)
|
||||
|
||||
LazyColumn(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentPadding = PaddingValues(16.dp)
|
||||
) {
|
||||
items(imageList) { item ->
|
||||
ImageItem(item, imageLoader, context) // 传递 context 参数
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun ImageItem(item: ImageItem, imageLoader: ImageLoader, context: Context) { // 接收 context 参数
|
||||
val painter = rememberAsyncImagePainter(
|
||||
model = ImageRequest.Builder(context) // 使用 context 参数
|
||||
.data(item.url)
|
||||
.crossfade(true)
|
||||
.build(),
|
||||
imageLoader = imageLoader
|
||||
)
|
||||
|
||||
Image(
|
||||
painter = painter,
|
||||
contentDescription = null,
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.height(200.dp),
|
||||
contentScale = ContentScale.Crop
|
||||
)
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
}
|
||||
|
||||
fun getImageLoader(context: Context): ImageLoader {
|
||||
return ImageLoader.Builder(context)
|
||||
.memoryCache {
|
||||
MemoryCache.Builder(context)
|
||||
.maxSizePercent(0.25) // 设置内存缓存大小为可用内存的 25%
|
||||
.build()
|
||||
}
|
||||
.diskCache {
|
||||
DiskCache.Builder()
|
||||
.directory(context.cacheDir.resolve("image_cache"))
|
||||
.maxSizePercent(0.02) // 设置磁盘缓存大小为可用存储空间的 2%
|
||||
.build()
|
||||
}
|
||||
.build()
|
||||
}
|
||||
@@ -24,6 +24,8 @@ import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.compose.ui.unit.sp
|
||||
import androidx.navigation.NavOptions
|
||||
import androidx.navigation.navOptions
|
||||
import com.aiosman.riderpro.LocalNavController
|
||||
import com.aiosman.riderpro.R
|
||||
import com.aiosman.riderpro.ui.NavigationRoute
|
||||
@@ -83,7 +85,12 @@ fun LoginPage() {
|
||||
text = "Login in".uppercase(),
|
||||
backgroundImage = R.mipmap.rider_pro_grey_bg_big
|
||||
) {
|
||||
navController.navigate(NavigationRoute.UserAuth.route)
|
||||
navController.navigate(
|
||||
NavigationRoute.UserAuth.route,
|
||||
navOptions {
|
||||
popUpTo(navController.currentDestination?.id ?: 0) { inclusive = true }
|
||||
}
|
||||
)
|
||||
}
|
||||
ActionButton(
|
||||
modifier = Modifier
|
||||
@@ -92,7 +99,12 @@ fun LoginPage() {
|
||||
text = "Sign In".uppercase(),
|
||||
backgroundImage = R.mipmap.rider_pro_red_bg_big
|
||||
){
|
||||
navController.navigate(NavigationRoute.SignUp.route)
|
||||
navController.navigate(
|
||||
NavigationRoute.SignUp.route,
|
||||
navOptions {
|
||||
popUpTo(navController.currentDestination?.id ?: 0) { inclusive = true }
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user