2014-04-13 19 views
0

我一直在尋找一種方式,如何從我的屏幕如何獲取Android中顯示的像素值?

(我的顯示器countains許多圖像,不僅背景)

感謝

提取像素值的選擇(X,Y)的(RGB)
+0

的解決方案是在這裏:HTTP://計算器.com/questions/6272859 /越來越像素顏色價值的點上一安卓視圖,包括一個bitm – eleven

+0

我已經檢查過它,但我沒有找到如何獲得值 –

+0

添加解釋。 – eleven

回答

3

首先你需要你的活動的根容器。您的活動的假設XML看起來是這樣的:

<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> 

然後在活動中找到根源,並使用jkhouw1方法得到Bitmap其中有方法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; 
} 
+0

這個方法在可變像素中返回一個空值,任何想法爲什麼? –

+0

@ user3501635 hm,你確定'getPixel'返回null嗎?實際上它不能返回null。 – eleven