Monday, March 15, 2021

jetpack compose - Image gradient border

MainActivity.kt

package com.cfsuman.jetpackcompose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            MainContent()
        }
    }


    @Composable
    fun MainContent(){
        Column(
            modifier = Modifier
                .fillMaxSize()
                .background(Color(0xFFFFF0F5))
                .padding(16.dp),
            verticalArrangement = Arrangement.spacedBy(32.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            val sweepGradientBrush = Brush.sweepGradient(
                colors = listOf(Color.Red, Color.Green, Color.Yellow)
            )

            Image(
                painter = painterResource(id = R.drawable.flower5),
                contentDescription = "Localized description",
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .fillMaxWidth(0.9f)
                    .height(150.dp)
                    .clip(RoundedCornerShape(12.dp))
                    .border(
                        8.dp,
                        sweepGradientBrush,
                        RoundedCornerShape(12.dp)
                    )
            )

            val verticalGradientBrush = Brush.verticalGradient(
                colors = listOf(Color(0xFFFF5470), Color(0xFFEEDC82))
            )

            Image(
                painter = painterResource(id = R.drawable.flower5),
                contentDescription = "Localized description",
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .fillMaxWidth(0.9f)
                    .height(150.dp)
                    .clip(CutCornerShape(12.dp))
                    .border(
                        6.dp,
                        verticalGradientBrush,
                        CutCornerShape(12.dp)
                    )
            )

            val linearGradientBrush = Brush.linearGradient(
                colors = listOf(Color(0xFFED2939), Color(0xFFFFFF66))
            )

            Image(
                painter = painterResource(id = R.drawable.flower5),
                contentDescription = "Localized description",
                contentScale = ContentScale.Crop,
                modifier = Modifier
                    .size(200.dp)
                    .clip(CircleShape)
                    .border(10.dp,linearGradientBrush, CircleShape)
            )
        }
    }


    @Preview
    @Composable
    fun ComposablePreview(){
        //MainContent()
    }
}