0

I was looking for a way how to extract the pixel value (RGB) of a selected (x,y) from my screen

(My display countains many images, not only the background)

thanks

2
  • The solutions is here: stackoverflow.com/questions/6272859/… Commented Apr 13, 2014 at 14:16
  • i have already checked it, but i didn't find how to get the values Commented Apr 13, 2014 at 14:20

1 Answer 1

3

First of all you need the root container of your activity. Suppose xml of your activity looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/container" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- All your stuff--> </RelativeLayout> 

Then in the activity find the root and using jkhouw1 method get Bitmap which has method getPixel:

@Override protected void onCreate(Bundle savedInstanceState) { //... View container = findViewById(R.id.container); Bitmap bitmap = loadBitmapFromView(container); int pixel = bitmap.getPixel(100, 100); int r = Color.red(pixel); int b = Color.blue(pixel); int g = Color.green(pixel); } public static Bitmap loadBitmapFromView(View v) { Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b); v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height); v.draw(c); return b; } 
Sign up to request clarification or add additional context in comments.

2 Comments

this method returns a null value in the variable pixel, any idea why ?
@user3501635 hm, are you sure getPixel returns null? Actually it can't return null.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.