Getting the Pixel Color Value of a Point on an Android View That Includes a Bitmap-Backed Canvas

Getting the pixel color value of a point on an Android View that includes a Bitmap-backed Canvas

How about load the view to a bitmap (at some point after all your drawing/sprites etc is done), then get the pixel color from the bitmap?

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

then use getPixel(x,y) on the result?

http://developer.android.com/reference/android/graphics/Bitmap.html#getPixel%28int,%20int%29

Can a pixel also store information regarding strokes drawn on that part of the image?

Any shape in a bitmap is a combination of pixels and every pixel has a specific position(x,y) in a plane. A pixel only has a color code information. To get a color code of a specific pixel:

int color = bitmap.getPixel(x,y)

How to get the pixel value of the display in Android?

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

Getting the color of a point on the screen

In the end I used getDrawingCache on the view and then getPixel on the bitmap with the coordinates received in the onTouchEvent function.

View Select a background image and draw on it

You just need to draw the selected image on mExtraCanvas

mdb.mExtraCanvas.drawBitmap(image, 0, 0, null)

And for the scaled part create a scaled bitmap like.

Bitmap.createScaledBitmap(image, 
mdb.mExtraBitmap.getWidth(),
mdb.mExtraBitmap.getHeight(), false);


Related Topics



Leave a reply



Submit