Simple draw pixels

You are to first successfully copy, paste, build and run the app before doing anything else. The instructions are at the beginning of the source code below.
/*
	Simple code to draw pixels on the screen

	This code is self-sufficient, that is
	it does not use any external resources.

	To build and run the android app, first
	follow the steps to 'Get everything ready'.

	Then inside the project folder and inside src/com/example/,
	replace the entire code in MainActivity.java file with this code.

	Use at your own risk!
*/

package com.example.myfirstapp;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.view.View;

public class MainActivity extends Activity
{
	/** Called when the activity is first created. */
	@Override
	public void onCreate (Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.main);    // this is the default view but which is replaced.
		setContentView(new MyView(this));   // the activity will have only one view 'MyView'.
	}

	private class MyView extends View       // custom view so to override onDraw()
	{
		public MyView(Context context)      // class constructor that should be present
		{ super(context); }

		@Override                           // here is the purpose of extending the View class
		protected void onDraw(Canvas canvas)
		{
			// Get width and height of the View. These will be for the
			// full area taken by the activity since it has only one view.
			int width = getWidth();
			int height = getHeight();

			// array to store the colours for all the pixels
			int[] pixels_colours = new int[height*width];

			for(int y=0; y<height; y++)
				for(int x=0; x<width; x++)
					pixels_colours[y*width+x] = (x + y<<16); // any colour

			// finally, draw to screen of device
			canvas.drawBitmap (pixels_colours, 0, width, 0, 0, width, height, false, null);

			// more at http://developer.android.com/reference/android/graphics/Canvas.html
		}
	}
}

Result is

Output from executing the code.


A custom view, subclass to the View class and called here MyView, is created so to override the method onDraw(). onDraw() is called whenever a View wants to refresh its painting on the screen. You can manually request this call by calling the method invalidate(). There you are not calling onDraw() but scheduling it for a call.

You can read about the drawBitmap function used in the code. Below is an extract:

(Added in API level 3)
public void drawBitmap (int[] colors, int offset, int stride, float x, float y, int width, int height, boolean hasAlpha, Paint paint)

Treat the specified array of colors as a bitmap, and draw it. This gives the same result as first creating a bitmap from the array, and then drawing it, but this method avoids explicitly creating a bitmap object which can be more efficient if the colors are changing often.

Parameters
colors Array of colors representing the pixels of the bitmap
offset Offset into the array of colors for the first pixel
stride The number of colors in the array between rows (must be >= width or <= -width).
x The X coordinate for where to draw the bitmap
y The Y coordinate for where to draw the bitmap
width The width of the bitmap
height The height of the bitmap
hasAlphaTrue if the alpha channel of the colors contains valid values. If false, the alpha byte is ignored (assumed to be 0xFF for every pixel).
paint May be null. The paint used to draw the bitmap


Contact us to report broken links and errors.