Open In App

Android Jetpack Compose – Display Mirror of an ImageView

Last Updated : 20 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Many times in the android application we have to display a mirror image of an image view within our android application to show its mirror image. In this article, we will take a look at How to add a mirror image effect to our image view in android using Jetpack Compose.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.example.newcanaryproject
  
import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.webkit.WebSettings.TextSize
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.BlurredEdgeTreatment
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.*
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.LayoutDirection
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import com.example.newcanaryproject.ui.theme.NewCanaryProjectTheme
import com.example.newcanaryproject.ui.theme.greenColor
import com.github.fernandodev.easyratingdialog.library.EasyRatingDialog
  
class MainActivity : ComponentActivity() {
  
    override fun onCreate(savedInstanceState: Bundle?) {
  
        super.onCreate(savedInstanceState)
        setContent {
            NewCanaryProjectTheme {
                // on below line we are specifying 
                // background color for our application
                Surface(
                    // on below line we are specifying
                    // modifier and color for our app
                    modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
                ) {
  
                    // on the below line we are specifying
                    // the theme as the scaffold.
                    Scaffold(
  
                        // in scaffold we are 
                        // specifying the top bar.
                        topBar = {
  
                            // inside top bar we are specifying 
                            // background color.
                            TopAppBar(backgroundColor = greenColor,
  
                                // along with that we are specifying
                                // title for our top bar.
                                title = {
  
                                    // in the top bar we are 
                                    // specifying tile as a text
                                    Text(
                                        // on below line we are specifying
                                        // text to display in top app bar.
                                        text = "GFG",
  
                                        // on below line we are specifying
                                        // modifier to fill max width.
                                        modifier = Modifier.fillMaxWidth(),
  
                                        // on below line we are specifying
                                        // text alignment.
                                        textAlign = TextAlign.Center,
  
                                        // on below line we are specifying
                                        // color for our text.
                                        color = Color.White
                                    )
                                })
                        }) {
                        // on below line we are calling
                        // method to display UI
                        mirrorImage(LocalContext.current)
                    }
                }
            }
        }
    }
}
  
@Composable
fun mirrorImage(context: Context) {
  
    Column(
        modifier = Modifier
            .fillMaxHeight()
            .fillMaxWidth()
            .background(Color.White), horizontalAlignment = Alignment.CenterHorizontally
    ) {
        // on below line we are adding a spacer.
        Spacer(modifier = Modifier.height(100.dp))
        // on below line we are adding a text
        Text(
            // on below line specifying 
            // text for heading.
            text = "Mirror in Android",
            // adding text alignment,
            textAlign = TextAlign.Center,
            // on below line adding text color.
            color = greenColor,
            // on below line adding font weight.
            fontWeight = FontWeight.Bold,
            // on below line adding
            // padding from all sides.
            modifier = Modifier
                .padding(10.dp)
                .fillMaxWidth()
        )
        // on below line adding a spacer.
        Spacer(modifier = Modifier.height(40.dp))
  
        // on below line we are adding mirror
        // image for our image view.
        MirrorImage {
            // on below line we are 
            // creating a simple image.
            Image(
                // on below line specifying image id
                painterResource(id = R.drawable.android),
                // on below line specifying image description.
                contentDescription = "Android",
                // on below line specifying content scale.
                contentScale = ContentScale.Crop,
                // on below line adding modifier for image.
                modifier = Modifier
                    .width(200.dp)
                    .height(200.dp)
                    // on below line adding 
                    // rounded radius for image.
                    .clip(
                        RoundedCornerShape(24.dp)
                    )
            )
        }
  
    }
}
  
// on below line creating a 
// widget to mirror our image.
@Composable
fun MirrorImage(image: @Composable () -> Unit) {
  
    // on below line creating a column.
    Column {
        // on below line displaying
        // original image.
        image()
        // on below line creating  a box.
        Box(modifier = Modifier
            // on below line adding a 
            // graphic layer to it
            // to display mirror image.
            .graphicsLayer {
                // on below line adding 
                  // alpha and rotation for image.
                alpha = 0.70f
                rotationZ = 180f
            }
            // on below line drawing content
            .drawWithContent {
                // on below line adding colors for image.
                val colors = listOf(Color.White, Color.Transparent)
                // on below line drawing content.
                drawContent()
                // on below line drawing rectangle.
                drawRect(
                    // on below line specifying
                    // brush and blend mode.
                    brush = Brush.verticalGradient(colors),
                    blendMode = BlendMode.DstIn
                )
            }
            // on below line adding blur effect for image.
            .blur(radiusX = 1.dp, radiusY = 3.dp, BlurredEdgeTreatment.Unbounded)
  
        ) {
            // on below line displaying
            // our mirror image effect. 
            image()
        }
    }
}


Now run your application to see the output of it. 

Output:

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads