ImageView in Android using Jetpack Compose

25 Feb, 2021
Images, Graphics, and vectors attract so many users as they tell so much information in a very informative way. ImageView in Android is used to display different types of images from drawable or in the form of Bitmaps. In this article, we will take a look at the implementation of an ImageView in Android using Jetpack Compose. 
Attributes
Description
Step 1: Create a New Project
To create a new project in the Android Studio Canary Version please refer to How to Create a new Project in Android Studio Canary Version with Jetpack Compose.
Step 2: Add an Image to the drawable folder
After creating a new project we have to add an image inside our drawable folder for displaying that image inside our ImageView. Copy your image from your folder’s location and go inside our project. Inside our project Navigate to the app > res > drawable > Right-click on the drawable folder and paste your image there. 
Step 3: Working with the MainActivity.kt file
After adding this image navigates to the app > java > MainActivity.kt and add the below code to it. Comments are added inside the code for a detailed explanation. 
Kotlin
package com.example.edittext  import android.os.Bundleimport androidx.appcompat.app.AppCompatActivityimport androidx.compose.foundation.Imageimport androidx.compose.foundation.layout.*import androidx.compose.material.MaterialThemeimport androidx.compose.material.Surfaceimport androidx.compose.runtime.Composableimport androidx.compose.ui.Alignmentimport androidx.compose.ui.layout.ContentScaleimport androidx.compose.ui.platform.setContentimport androidx.compose.ui.res.imageResourceimport androidx.compose.ui.tooling.preview.Previewimport androidx.compose.ui.Modifierimport androidx.compose.ui.unit.Dp  import com.example.edittext.ui.EditTextTheme  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContent {            EditTextTheme {                // A surface container using the                // 'background' color from the theme                Surface(                        // to add background color for our screen.                        color = MaterialTheme.colors.background,                ) {                    // at below line we are calling                    // our function for image view.                    Img();                }            }        }    }}  @Composablefun Img() {      Column(            // we are using column to align our            // imageview to center of the screen.            modifier = Modifier.fillMaxWidth().fillMaxHeight(),              // below line is used for specifying            // vertical arrangement.            verticalArrangement = Arrangement.Center,              // below line is used for specifying            // horizontal arrangement.            horizontalAlignment = Alignment.CenterHorizontally,    ) {        // below line is used for creating a variable        // for our image resource file.        val img = imageResource(id = R.drawable.gfgimage)          // below line is used for creating a modifier for our image        // which includes image width and image height        val modifier = Modifier.preferredHeight(height = Dp(200F)).preferredWidth(width = Dp(200F))        // below is the widget for image.        Image(                // first parameter of our Image widget                // is our image path which we have created                // above and name it as img.                img,                  // below line is used to give                // alignment to our image view.                alignment = Alignment.Center,                  // below line is used to scale our image                // we are using center crop for it.                contentScale = ContentScale.Crop,                  // below line is used to add modifier                // to our image. we are adding modifier                // which we have created above and name                // it as modifier.                modifier = modifier        )      }}    // @Preview function is use to see preview// for our composable function in preview section.@Preview@Composablefun DefaultPreview() {    MaterialTheme {        // we are passing our composable        // function to display its preview.        Img()    }}
Android-Jetpack
Technical Scripter 2020
Android
Technical Scripter
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Android SDK and it's Components
How to Add Views Dynamically and Store Data in Arraylist in Android?
Flutter - Custom Bottom Navigation Bar
Retrofit with Kotlin Coroutine in Android
How to Post Data to API using Retrofit in Android?
Flutter - Stack Widget
How to Communicate Between Fragments in Android?
Activity Lifecycle in Android with Demo App
Introduction to Android Development
Animation in Android with Example