Get color from ImageView Download Source code here

activity_main.xml




    

    





MainActivity.java
package com.canvas;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    ImageView iv_image;
    TextView tv_text;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

    }

    private void init() {

        iv_image = (ImageView) findViewById(R.id.iv_image);
        tv_text = (TextView) findViewById(R.id.tv_text);

        iv_image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int x = (int) event.getX();
                int y = (int) event.getY();

                iv_image.buildDrawingCache();
                Bitmap bitmap = iv_image.getDrawingCache();
                int projectedX = (int) ((double) x * ((double) bitmap.getWidth() / (double) iv_image.getWidth()));
                int projectedY = (int) ((double) y * ((double) bitmap.getHeight() / (double) iv_image.getHeight()));

                int pixel = bitmap.getPixel(projectedX, projectedY);
                tv_text.setBackgroundColor(pixel);

                return false;
            }
        });
    }
}

Advertisements