Open In App

Pick Color From Image with TouchEvents in Android

Last Updated : 25 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how to get touched pixel color on an image. A Screenshot is given below to get an idea about what we are going to do in this article. Note that we are going to implement this project using the Kotlin language. 

Step by Step Implementation

Step 1:  To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language. 

Step 2:  Set any image in the layout

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">
  
    <!--Image form which we will pick color-->
    <ImageView
        android:id="@+id/pickColorImage"
        android:layout_width="match_parent"
        android:layout_height="255dp"
        android:layout_marginTop="10dp"
        android:contentDescription="Image to pick color"
        android:scaleType="fitStart"
        android:src="@drawable/gfg"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal"
        app:layout_constraintTop_toBottomOf="@id/pickColorImage">
  
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Picked Color: "
            android:textColor="@color/black"
            android:textSize="20sp" />
  
        <!-- color name in Hexadecimal -->
        <TextView
            android:id="@+id/colorInHex"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="#fffffff"
            android:textSize="20sp" />
  
        <!-- show the picked color -->
        <View
            android:id="@+id/fillColor"
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_marginStart="8dp"
            app:layout_constraintTop_toBottomOf="@id/pickColorImage" />
    </LinearLayout>
  
</androidx.constraintlayout.widget.ConstraintLayout>


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




import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.graphics.Color
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
  
    lateinit var image: ImageView
    lateinit var bitmap: Bitmap
    lateinit var colorView: View
    lateinit var colorString: TextView
  
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        image = findViewById(R.id.pickColorImage)
        colorView = findViewById(R.id.fillColor)
        colorString = findViewById(R.id.colorInHex)
  
        image.isDrawingCacheEnabled = true
        image.buildDrawingCache(true)
  
        // on touch listener on image view
        image.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_DOWN || event.action == MotionEvent.ACTION_MOVE) {
                bitmap = image.drawingCache
  
                // get touched pixel
                val pixel = bitmap.getPixel(event.x.toInt(), event.y.toInt())
  
                // get RGB values from the touched pixel
                val r = Color.red(pixel)
                val g = Color.green(pixel)
                val b = Color.blue(pixel)
  
                // color name in Hexadecimal(#RRGGBB)
                colorString.text = "#${Integer.toHexString(pixel)}"
  
                // fill the color in the view
                colorView.setBackgroundColor(Color.rgb(r, g, b))
            }
            true
        }
    }
}


Now, run the app

Output:

Source Code: Click Here



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads