如何在Android中获取显示的像素值?

时间:2014-04-13 14:10:08

标签: android android-layout

我一直在寻找一种如何从我的屏幕中提取所选(x,y)像素值(RGB)的方法

(我的显示器会计算许多图像,而不仅仅是背景)

感谢

1 个答案:

答案 0 :(得分: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;
}