Open top menu

 Android- Explain how to get Color code from Image programmatically.

First Set Image in Bitmap :-
final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
 
Then Get Touch X and Y coordinate from ImageView.
public boolean onTouch(View v, MotionEvent event) {

    x = (int)event.getX();

    y= (int)event.getY();

    return false;

}
 
Get the color code in Hex String touch on imageView.
Integer.toHexString(bitmap.getPixel(x, y);
 
Get the color code in Hex String touch on imageView.

activity_main.xml
.............................................................................................................................................................

xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:id="@+id/activity_main"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">



    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="Android Beginner point"

        android:textSize="25dp"

        android:textStyle="bold"

        android:id="@+id/textView" />



    <ImageView

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:src="@drawable/multicolor"

        android:layout_below="@+id/textView"

        android:layout_toRightOf="@+id/textView"

        android:layout_toEndOf="@+id/textView"

        android:id="@+id/imageView" />



</LinearLayout>

.............................................................................................................................................................

MAinActivity.java
.............................................................................................................................................................
import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.MotionEvent;

import android.view.View;

import android.widget.ImageView;

import android.widget.TextView;

import android.widget.Toast;



public class MainActivity extends AppCompatActivity {

     int x,y;

    TextView txtView;

    ColorUtils utils=new ColorUtils();

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        txtView=(TextView)findViewById(R.id.textView);

        ImageView imageView=(ImageView)findViewById(R.id.imageView);

        final Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();

         imageView.setOnTouchListener(new View.OnTouchListener() {

             @Override

             public boolean onTouch(View v, MotionEvent event) {

                 x = (int)event.getX();

                 y= (int)event.getY();

                 int pixel = bitmap.getPixel(x,y);

                 txtView.setTextColor(bitmap.getPixel( x,y));

                   Toast.makeText(getApplicationContext(),"#"+Integer.toHexString(bitmap.getPixel(x, y)),Toast.LENGTH_LONG).show();



                 return false;

             }

         });



    }

}
 .............................................................................................................................................................


 


0 comments