使用Jetpack Compose實(shí)現(xiàn)翻轉(zhuǎn)卡片效果流程詳解
如何使用 Jetpack Compose 創(chuàng)建翻轉(zhuǎn)卡片效果

介紹
在電子商務(wù)和銀行應(yīng)用程序中輸入卡信息是很常見的情況。我認(rèn)為讓用戶更輕松地處理這種情況并創(chuàng)建更吸引眼球的 UI 將很有用。大多數(shù)應(yīng)用程序/網(wǎng)站都喜歡它。
執(zhí)行
在開發(fā)階段,您需要做的是打開一個 Android 項(xiàng)目并實(shí)施 Compose 庫。
如果我們繼續(xù)編碼,我們可以檢查以下 Compose 代碼。

您可以根據(jù)上面的設(shè)計(jì)在屏幕上創(chuàng)建您的卡片。
@Composable
fun AddCreditCard(backgroundColor: Color) {
var rotated by remember { mutableStateOf(false) }
val cardType =
when (result.value?.organization) {
"MasterCard" -> painterResource(R.drawable.mc)
"VISA" -> painterResource(R.drawable.visa)
else -> painterResource(R.drawable.ic_launcher_background)
}
val rotation by animateFloatAsState(
targetValue = if (rotated) 180f else 0f,
animationSpec = tween(500)
)
val animateFront by animateFloatAsState(
targetValue = if (!rotated) 1f else 0f,
animationSpec = tween(500)
)
val animateBack by animateFloatAsState(
targetValue = if (rotated) 1f else 0f,
animationSpec = tween(500)
)
Card(
modifier = Modifier
.height(220.dp)
.fillMaxWidth()
.padding(10.dp)
.graphicsLayer {
rotationY = rotation
cameraDistance = 8 * density
}
.clickable {
rotated = !rotated
},
shape = RoundedCornerShape(14.dp),
elevation = 4.dp,
backgroundColor = backgroundColor,
contentColor = Color.White
) {
if (!rotated) {
Column(
horizontalAlignment = Alignment.Start,
verticalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.padding(start = 8.dp, end = 8.dp, bottom = 8.dp),
) {
Row(horizontalArrangement = Arrangement.SpaceBetween) {
Icon(
painter = painterResource(R.drawable.ic_contactless),
contentDescription = "test",
modifier = Modifier
.width(50.dp)
.height(50.dp)
.padding(top = 6.dp, bottom = 6.dp, end = 20.dp)
.graphicsLayer {
alpha = animateFront
},
tint = Color.White
)
Spacer(modifier = Modifier.weight(1f))
Image(
painter = cardType,
contentDescription = "test",
modifier = Modifier
.width(50.dp)
.height(50.dp)
.graphicsLayer {
alpha = animateFront
}
)
}
result.value?.number?.let {
Text(
text = it,
modifier = Modifier
.padding(top = 16.dp)
.graphicsLayer {
alpha = animateFront
},
fontFamily = fontName,
fontWeight = FontWeight.Normal,
fontSize = 25.sp
)
}
Row(horizontalArrangement = Arrangement.SpaceBetween) {
Column(horizontalAlignment = Alignment.Start) {
Text(
text = "Card Holder",
color = Color.Gray,
fontSize = 9.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.graphicsLayer {
alpha = animateFront
}
)
Text(
text = "Mehmet Yozgatli",
color = Color.White,
fontSize = 15.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.graphicsLayer {
alpha = animateFront
}
)
}
Spacer(modifier = Modifier.weight(1f))
Column(horizontalAlignment = Alignment.Start) {
Text(
text = "VALID THRU",
color = Color.Gray,
fontSize = 9.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.graphicsLayer {
alpha = animateFront
}
)
result.value?.expire?.let {
Text(
text = it,
color = Color.White,
fontSize = 15.sp,
fontWeight = FontWeight.Bold,
modifier = Modifier
.graphicsLayer {
alpha = animateFront
}
)
}
}
}
}
} else {
Column(
modifier = Modifier.padding(top = 20.dp),
) {
Divider(
modifier = Modifier
.graphicsLayer {
alpha = animateBack
}, color = Color.Black, thickness = 50.dp
)
Text(
text = "123",
color = Color.Black,
modifier = Modifier
.padding(10.dp)
.background(Color.White)
.fillMaxWidth()
.graphicsLayer {
alpha = animateBack
rotationY = rotation
}
.padding(10.dp),
fontSize = 15.sp,
textAlign = TextAlign.End
)
Text(
text = "Developed by Mehmet Yozgatli",
color = Color.White,
modifier = Modifier
.fillMaxWidth()
.graphicsLayer {
alpha = animateBack
rotationY = rotation
}
.padding(5.dp),
fontFamily = fontName,
fontWeight = FontWeight.Thin,
fontSize = 10.sp,
textAlign = TextAlign.Center
)
}
}
}
}創(chuàng)建卡片后,將旋轉(zhuǎn)、animateFront 和 animateBack 值作為參數(shù)傳遞給組件時,就完成了動畫部分。
ML Kit銀行卡識別
通過使用華為機(jī)器學(xué)習(xí)服務(wù)的銀行卡識別服務(wù),您可以為用戶提供極大的便利。
您可以按照官方文檔中的實(shí)施步驟進(jìn)行操作。
https://developer.huawei.com/consumer/en/doc/development/hiai-Guides/dev-process-0000001050038076
輸出
卡片翻轉(zhuǎn)效果

使用機(jī)器學(xué)習(xí)套件獲取信息

結(jié)論
重要的是我們的應(yīng)用程序要易于使用并讓事情變得簡單。
到此這篇關(guān)于使用Jetpack Compose實(shí)現(xiàn)翻轉(zhuǎn)卡片效果流程詳解的文章就介紹到這了,更多相關(guān)Jetpack Compose翻轉(zhuǎn)卡片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Android中Activity的四大啟動模式實(shí)驗(yàn)簡述
本篇文章主要介紹了Android中Activity的四大啟動模式實(shí)驗(yàn)簡述,具有一定的參考價值,有興趣的可以了解一下。2016-12-12
android使用Socket通信實(shí)現(xiàn)多人聊天應(yīng)用
這篇文章主要為大家詳細(xì)介紹了android使用Socket通信實(shí)現(xiàn)多人聊天應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-03-03
Android筆記之:App應(yīng)用之啟動界面SplashActivity的使用
當(dāng)前比較成熟一點(diǎn)的應(yīng)用基本上都會在進(jìn)入應(yīng)用之顯示一個啟動界面.這個啟動界面或簡單,或復(fù)雜,或簡陋,或華麗,用意不同,風(fēng)格也不同2013-04-04
Android使用GridView實(shí)現(xiàn)日歷的方法
本篇文章主要介紹了Android使用GridView實(shí)現(xiàn)日歷的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
Android開發(fā)中Activity創(chuàng)建跳轉(zhuǎn)及傳值的方法
這篇文章主要介紹了Android開發(fā)中Activity創(chuàng)建跳轉(zhuǎn)及傳值的方法的相關(guān)資料,需要的朋友可以參考下2016-05-05

