Created
June 12, 2023 17:04
-
-
Save lecuseasar/a5333e4822f412e1cb95a072bcf90d11 to your computer and use it in GitHub Desktop.
Android Jetpack Compose ImageCard Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.myfirstjetpackcomposeapplication | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.foundation.Image | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.layout.Box | |
import androidx.compose.foundation.layout.fillMaxSize | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.foundation.layout.padding | |
import androidx.compose.foundation.shape.RoundedCornerShape | |
import androidx.compose.material3.Card | |
import androidx.compose.material3.CardDefaults | |
import androidx.compose.material3.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Brush | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.painter.Painter | |
import androidx.compose.ui.layout.ContentScale | |
import androidx.compose.ui.res.painterResource | |
import androidx.compose.ui.text.TextStyle | |
import androidx.compose.ui.unit.dp | |
import androidx.compose.ui.unit.sp | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
val painter = painterResource(id = R.drawable.kermit) | |
val description = "Kermit hug a wine bottle" | |
val title = "Kermit is drinking a wine" | |
Box( | |
modifier = Modifier | |
.fillMaxWidth(.8f) | |
.padding(15.dp) | |
) { | |
ImageCard( | |
painter = painter, | |
contentDescription = description, | |
title = title | |
) | |
} | |
} | |
} | |
} | |
@Composable | |
fun ImageCard( | |
painter: Painter, | |
contentDescription: String, | |
title: String, | |
modifier: Modifier = Modifier, | |
) { | |
Card( | |
modifier = modifier.fillMaxWidth(), | |
shape = RoundedCornerShape(5.dp), | |
elevation = CardDefaults.cardElevation( | |
defaultElevation = 5.dp, | |
) | |
) { | |
Box(modifier = Modifier.height(200.dp)) {// Z-Stack | |
Image( | |
painter = painter, | |
contentDescription = contentDescription, | |
contentScale = ContentScale.Crop, | |
) | |
Box( | |
modifier = Modifier | |
.fillMaxSize() | |
.background( | |
brush = Brush.verticalGradient( | |
colors = listOf( | |
Color.Transparent, | |
Color.Black, | |
), | |
startY = 300f, // Parent'inin yuksekligini alip o sekilde bu degeri ata dynamic yap | |
) | |
) | |
) | |
Box( | |
modifier = Modifier | |
.fillMaxSize() | |
.padding(12.dp), | |
contentAlignment = Alignment.BottomStart, | |
) { | |
Text( | |
text = title, | |
style = TextStyle( | |
color = Color.White, | |
fontSize = 16.sp, | |
), | |
) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment