Android ImageFilterView Using Kotlin With Example

In this article, we will learn about android ImageFilterView using Kotlin. We will go through various example that demonstrates how to use different attributes of ImageFilterView. For example,

In this article, we will get answer to questions like –

  • What is ImageFilterView?
  • Why should we consider ImageFilterView while designing ui for any app?
  • What are possibilities using ImageFilterView while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin Android ImageFilterView Using Kotlin With Example

Getting Started

We can define android ImageFilterView widget as below –

ImageFilterView is a subclass of ImageView that can display combine and filter images using various filtering options.

Now, how do we use ImageFilterView in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as ImageFilterView. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android ImageFilterView Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using ImageFilterView in Kotlin

Follow steps below to use ImageFilterView in newly created project –

  • We need drawable resources for our application. You can either download any image from other sources or add one using our tutorial on how to add vector image in android studio.
  • Open res/values/strings.xml file. Then, add below code into it.
    <resources>
        <string name="app_name">ImageFilterView</string>
        <string name="saturation">Saturation:</string>
        <string name="warmth">Warmth:</string>
        <string name="contrast">Contrast:</string>
    </resources>
    
  • Open res/layout/activity_main.xml file. Then, add below code in it –
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        tools:context=".MainActivity">
    
        <androidx.constraintlayout.utils.widget.ImageFilterView
            android:id="@+id/imageFilterView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:src="@drawable/garden"
            app:contrast="1.0"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:saturation="1.0"
            app:warmth="1.0"
            tools:srcCompat="@tools:sample/avatars" />
    
        <TextView
            android:id="@+id/saturation_key"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="@string/saturation"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageFilterView" />
    
        <SeekBar
            android:id="@+id/saturationSeekBar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/saturation_key"
            app:layout_constraintTop_toBottomOf="@+id/imageFilterView" />
    
        <TextView
            android:id="@+id/warmth_key"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="@string/warmth"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/saturation_key" />
    
        <SeekBar
            android:id="@+id/warmthSeekBar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/warmth_key"
            app:layout_constraintTop_toBottomOf="@+id/saturation_key" />
    
        <TextView
            android:id="@+id/contrast_key"
            android:layout_width="70dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="@string/contrast"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/warmth_key" />
    
        <SeekBar
            android:id="@+id/contrastSeekBar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/contrast_key"
            app:layout_constraintTop_toBottomOf="@+id/warmth_key" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    In our tutorial, we are going to implement a way to change contrast, warmth and saturation of any image using ImageFilterView. So, we have modified our ui accordingly. We need one textView and one seekBar for each filter type.

  • Now, we will access each seekBar in Kotlin file. Then, we will add some listener and modify respective filter when any seekbar value changes. So, open Kotlin file MainActivity.kt and paste below code in it –
    package com.tutorialwing.imagefilterview
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.widget.SeekBar
    import com.tutorialwing.imagefilterview.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		setContentView(binding.root)
    
    		setupSaturationSeekbar()
    		setupContrastSeekbar()
    		setupWarmthSeekbar()
    	}
    
    	private fun setupSaturationSeekbar() {
    		binding.saturationSeekBar.setOnSeekBarChangeListener(object :
    			SeekBar.OnSeekBarChangeListener {
    			override fun onProgressChanged(seekbar: SeekBar?, progress: Int, p2: Boolean) {
    				val percentage = (progress / 100.0f)
    				binding.imageFilterView.saturation = (percentage) + 1
    			}
    
    			override fun onStartTrackingTouch(seekbar: SeekBar?) {
    
    			}
    
    			override fun onStopTrackingTouch(seekbar: SeekBar?) {
    
    			}
    		})
    	}
    
    	private fun setupContrastSeekbar() {
    		binding.contrastSeekBar.setOnSeekBarChangeListener(object :
    			SeekBar.OnSeekBarChangeListener {
    			override fun onProgressChanged(seekbar: SeekBar?, progress: Int, p2: Boolean) {
    				val percentage = (progress / 100.0f)
    				binding.imageFilterView.contrast = (percentage) + 1
    			}
    
    			override fun onStartTrackingTouch(seekbar: SeekBar?) {
    
    			}
    
    			override fun onStopTrackingTouch(seekbar: SeekBar?) {
    
    			}
    		})
    	}
    
    	private fun setupWarmthSeekbar() {
    		binding.warmthSeekBar.setOnSeekBarChangeListener(object :
    			SeekBar.OnSeekBarChangeListener {
    			override fun onProgressChanged(seekbar: SeekBar?, progress: Int, p2: Boolean) {
    				val percentage = (progress / 100.0f)
    				binding.imageFilterView.warmth = (percentage) + 1
    			}
    
    			override fun onStartTrackingTouch(seekbar: SeekBar?) {
    
    			}
    
    			override fun onStopTrackingTouch(seekbar: SeekBar?) {
    
    			}
    		})
    	}
    }
    

    Here, we have added seekBar value change listener. So, whenever we scroll any seekBar to left or right, respective filter is applied with selected value.

Now, run application. We will get output as below –
Tutorialwing Kotlin Android ImageFilterView Using Kotlin With Example

Different Attributes of ImageFilterView in XML

Now, we will see how to use different attributes of Android ImageFilterView using Kotlin to customise it –

Set Id of ImageFilterView

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of ImageFilterView using android:id attribute like below –

    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/imageFilterView_ID"
        />

Here, we have set id of ImageFilterView as imageFilterView_ID using android:id=”” attribute. So, if we need to reference this ImageFilterView, we need to use this id – imageFilterView_ID.

Set Width of ImageFilterView

We use android:layout_width=”” attribute to set width of ImageFilterView.
We can do it as below –

    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/imageFilterView_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).

Set Height of ImageFilterView

We use android:layout_height=”” attribute to set height of ImageFilterView.
We can do it as below –

    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/imageFilterView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.

Set Padding of ImageFilterView

We use android:padding=”” attribute to set padding of ImageFilterView.
We can do it as below –

    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/imageFilterView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in ImageFilterView using android:padding=”” attribute.

Set Margin of ImageFilterView

We use android:layout_margin=”” attribute to set margin of ImageFilterView.
We can do it as below –

    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/imageFilterView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in ImageFilterView using android:layout_margin=”” attribute.

Set Background of ImageFilterView

We use android:background=”” attribute to set background of ImageFilterView.
We can do it as below –

    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/imageFilterView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in ImageFilterView using android:background=”” attribute.

Set Visibility of ImageFilterView

We use android:visibility=”” attribute to set visibility of ImageFilterView.
We can do it as below –

    <androidx.constraintlayout.utils.widget.ImageFilterView
        android:id="@+id/imageFilterView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of ImageFilterView using android:visibility=”” attribute. Visibility can be of three types – gone, visible and invisible

We have seen different attributes of ImageFilterView and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is ImageFilterView, how can we use android ImageFilterView using Kotlin ? etc. We also went through different attributes of android ImageFilterView.

Leave a Reply