Change Transparency of an Image in Android Jetpack Compose
In Android, we use ImageView to display an image in the application. We can use stock images or even import them from the local devices in the application for displaying them. When we use Jetpack Compose, we use the Image function for displaying images. Image Transparency is the opacity of the image. Mathematically, if opacity is 1, then the displayed image is the same as the original. Opacity or the alpha value ranges between 0 and 1. In this article, we will show you how you could change the Transparency of an Image in Android using Jetpack Compose. Follow the below steps once the IDE is ready.
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: Add an Image in the app > res > drawable folder
Import an image of your choice in the drawable folder. In our application, we named it “sample_image”.
Step 3: 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.w3wiki.jcimagetransparency import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.Image import androidx.compose.foundation.layout.* import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.painterResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContent { // Calling the composable function // to display element and its contents MainContent() } } } // Creating a composable // function to display Top Bar @Composable fun MainContent(){ Scaffold( topBar = {TopAppBar( title = {Text( "GFG | Change Image Transparency" , color = Color.White)}, backgroundColor = Color( 0xff0f9d58 ) ) }, content = { MyContent()} ) } // Creating a composable function to // create two Images and a spacer between them // Calling this function as content in the above function @Composable fun MyContent(){ Column(Modifier.fillMaxSize(), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally) { // Displaying the original image Image( painter = painterResource(id = R.drawable.sample_image), contentDescription = null , ) // Adding a space of 100dp Spacer(modifier = Modifier.height( 100 .dp)) // Displaying the image with // less opacity (marked at 20%) Image( painter = painterResource(id = R.drawable.sample_image), contentDescription = null , alpha = 0 .2F ) } } // For displaying preview in // the Android Studio IDE emulator @Preview (showBackground = true ) @Composable fun DefaultPreview() { MainContent() } |
Output:
When you run the application you will see two images. The first one is the original image with 100% opacity and the second one with 20% opacity as shown below.
Contact Us