Reputation: 6211
So I'm really new to the world of graphics and for an app I'm working on I need to convert an 2d array of int values into a 2d image where the int values correspond to a color. For example a value of (1) at spot [0][0] would equate to that square on the image being "Red". I was looking at the Android sample code of Snake, because what they are doing there looks similar to what I want to do, but I have been having trouble figuring out what they are doing. Could you hep me figure out how I can make a class that converts a 2d array of ints into a corresponding 2d image the size of the user's screen?
Thanks
Upvotes: 1
Views: 388
Reputation: 16152
There are two ways you could go:
Simply draw into an android.graphics.Bitmap
using an android.graphics.Canvas
, painting pixels or squares or anything you need into it, then passing the finished bitmap to a View (like an ImageView).
Create an android.graphics.Bitmap
with the dimensions you require, then use either #setPixels() or #copyPixelsFromBuffer() to set the required pixels. Note that you have a 2-dimensional array, so you will need to either have it in a 1-d form, or loop on one dimension to processit in 1-d slices.
Edit: Just noticed that the integers correspond to colors - You want to use an indexed mode for the bitmap you created, and give the color maps at creation time.
Upvotes: 1