简体   繁体   English

Android Jetpack - 将图像设置为卡中的自定义样式

[英]Android Jetpack - Fit Image as Custom Style in Card

I have a screen that includes only a LazyRow horizontal list includes 2 Cards, so you can slide cards.我有一个仅包含 LazyRow 水平列表的屏幕,其中包含 2 张卡片,因此您可以滑动卡片。 You can see my code below:你可以在下面看到我的代码:


import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.Alignment
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.font.FontWeight
import androidx.compose.ui.unit.sp
import com.example.jetpackdeneme.R

@Composable
fun TestScreen1() {
    Box(modifier = Modifier.fillMaxSize()) {
        LazyColumn(
            modifier = Modifier.fillMaxSize(),
            contentPadding = PaddingValues(20.dp),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            item {
                LazyRow(
                    Modifier.height(160.dp),
                    contentPadding = PaddingValues(horizontal = 16.dp),
                    horizontalArrangement = Arrangement.Center,
                    verticalAlignment = Alignment.CenterVertically
                ) {
                    item {
                        PromotionItem1(
                            imagePainter = painterResource(id = R.drawable.statue_of_liberty),
                            title = "AMERICA",
                            header = "USA",
                            backgroundColor = Color.White
                        )
                    }
                    item {
                        PromotionItem1(
                            imagePainter = painterResource(id = R.drawable.statue_of_liberty),
                            title = "AMERICA",
                            header = "USA",
                            backgroundColor = Color.White
                        )
                    }
                }
            }
        }
    }
}

@Composable
fun PromotionItem1(
    title: String = "",
    header: String = "",
    backgroundColor: Color = Color.Transparent,
    imagePainter: Painter
) {
    Card(
        Modifier.width(300.dp),
        shape = RoundedCornerShape(8.dp),
        backgroundColor = backgroundColor,
        elevation = 0.dp
    ) {
        Row {
            Image(
                painter = imagePainter, contentDescription = "",
                modifier = Modifier
                    .fillMaxHeight()
                    .weight(1f),
                alignment = Alignment.CenterEnd,
                contentScale = ContentScale.Crop
            )
            Column(
                Modifier
                    .padding(horizontal = 16.dp)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center
            ) {
                Text(text = title, fontSize = 14.sp, color = Color.Black, fontWeight = FontWeight.Bold)
                Text(text = header, fontSize = 14.sp, color = Color.Black)
            }
        }
    }
}

The output is like this (it is not fitting, and I want a custom style): output是这样的(不合身,我要定制款式): 在此处输入图像描述

I wanted to set the image's bottom edge to bottom of card, and overflow the top a little bit like that output:我想将图像的底部边缘设置为卡的底部,并像 output 一样溢出顶部:

在此处输入图像描述

You can get the image here你可以在这里得到图像

How can I do that?我怎样才能做到这一点? Can you help me please?你能帮我吗?

First, replace Card with Box which doesn't clip layout that prevents overflow.首先,将 Card 替换为 Box ,它不会剪切防止溢出的布局。

Second, set a graphicsLayer to increase scale of Image but by default tranform origin is center since we want to push image upwards we need to set bottom left as transform origin which is TransformOrigin(0f,1f) .其次,设置 graphicsLayer 以增加 Image 的比例,但默认情况下变换原点是中心,因为我们想将图像向上推,我们需要将左下角设置为变换原点,即TransformOrigin(0f,1f)

Third, increase scale as you wish and have expected output.第三,随心所欲地扩大规模,并期望output。

@Composable
fun PromotionItem1(
    title: String = "",
    header: String = "",
    backgroundColor: Color = Color.Transparent,
    imagePainter: Painter
) {
    Box(
        Modifier
            .width(300.dp)
            .background(backgroundColor, RoundedCornerShape(8.dp)),

    ) {
        Row {

            Image(
                painter = imagePainter,
                contentDescription = "",
                modifier = Modifier
                    .graphicsLayer {
                        scaleY = 1.2f
                        scaleX = 1.2f
                        this.transformOrigin = TransformOrigin(0f, 1f)
                    }
                    .fillMaxHeight(),
                contentScale = ContentScale.Fit
            )

            Spacer(modifier = Modifier.weight(1f))
            Column(
                Modifier
                    .padding(horizontal = 16.dp)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center
            ) {
                Text(
                    text = title,
                    fontSize = 14.sp,
                    color = Color.Black,
                    fontWeight = FontWeight.Bold
                )
                Text(text = header, fontSize = 14.sp, color = Color.Black)
            }
        }
    }
}

Also i increased LazyRow height to Modifier.height(200.dp)我也将 LazyRow 高度增加到Modifier.height(200.dp)

Result结果

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM