Skip to content

Instantly share code, notes, and snippets.

@kanytu
Created November 4, 2014 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanytu/2c80cff9b571f042b8f1 to your computer and use it in GitHub Desktop.
Save kanytu/2c80cff9b571f042b8f1 to your computer and use it in GitHub Desktop.
imgUsrClrId.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
//***********************Get the position of the screen on image***************************//
float eventX = event.getX();
float eventY = event.getY();
float[] eventXY = new float[] {eventX, eventY};
//***********************Get the position of the screen on image***************************//
//***********************Get the position of the image on image***************************//
Matrix invertMatrix = new Matrix();
((ImageView)imgUsrClrId).getImageMatrix().invert(invertMatrix);
invertMatrix.mapPoints(eventXY);
int x = Integer.valueOf((int)eventXY[0]);
int y = Integer.valueOf((int)eventXY[1]);
//***********************Get the position of the image on image***************************//
//***********************Get the Size of the Drawable image***************************//
Drawable imgDrawable = ((ImageView)imgUsrClrId).getDrawable();
Bitmap bitmap = ((BitmapDrawable)imgDrawable).getBitmap();
//***********************Get the Size of the Drawable image***************************//
//***********************Get the Color code of the image*****************************//
//Limit x, y range within bitmap
if(x < 0){
x = 0;
}else if(x > bitmap.getWidth()-1){
x = bitmap.getWidth()-1;
}
if(y < 0){
y = 0;
}else if(y > bitmap.getHeight()-1){
y = bitmap.getHeight()-1;
}
//Get the hexadecimal value w.r.t x and y Co-ordinates
int touchedRGB = bitmap.getPixel(x, y);
txtColorCodeId.setText("#" + Integer.toHexString(touchedRGB));
txtColorCodeId.setTextColor(touchedRGB);
//***********************Get the Color code of the image****************************//
return true;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment