Android Canvas.drawPoint() : what does it do?

DrMrLordX

Lifer
Apr 27, 2000
22,547
12,413
136
I've been trying to figure out what's going on with the drawPoint() method from android.graphics.Canvas. No matter what I do with it, the method does not actually draw any points on the screen (I even tried 0 brushwidth in the Color object, but it would not draw a pixel as advertised).

I even pored over the source for Canvas, but it did not prove to be terribly enlightening (not sure if that posting is complete, either).

Anyone know what's up with drawPoint()? Or can you show me a working implementation somewhere that I can play with? Thanks!
 

purbeast0

No Lifer
Sep 13, 2001
53,478
6,317
126
i don't know what it does, but i can say that without a doubt, the android documentation (as well as the whole development on android platform, especially if you want to start using google play services) is the worst development process/experience i've ever had.

i know this may sound kinda obvious, but are you sure you are using the correct coordinate system, and that it's not "drawing" the point off the screen?
 

DrMrLordX

Lifer
Apr 27, 2000
22,547
12,413
136
Fairly certain. Right now I'm using it on a Nexus 10 image under Genymotion, which gives me a 2560x1600 screen. I've tried drawing to 0,0 , 1,1, 1280,800, etc. So far, no luck. I haven't tried drawpoints() yet, but I'm trying to avoid that for a variety of reasons (the main one being the memory footprint involved with using a float array).
 

riahc3

Senior member
Apr 4, 2014
640
0
0
Surprised me that Ive been told that there are a lot of experts on here and no one knows the answer to the question....

Anyways, DrMrLordX,

First, Im not sure what code you have so you might be missing something important. Everytime you draw something, you actually have to "repaint" everything. This is true for a lot of languages (although some graphics frameworks do it automatically)

For Android is a bit different. Did you read this: http://developer.android.com/reference/android/graphics/Canvas.html

This will probably tell you a bit more how to do it and how to repaint everything correctly.

Also, tell me what version you are targeting. I don't have the dev kit installed right now but I don't mind firing up a VM and testing it out myself.

Tell us how it went so we can know how you solved it :)
 
Last edited:

DrMrLordX

Lifer
Apr 27, 2000
22,547
12,413
136
I'll put together some sample code to isolate the problem ASAP.

As far as version targeting, I create all my projects with min SDK version 14 to avoid the annoying appcompat bug in Eclipse (I later go back and set min SDK to version 10). Targeting SDK version 19.

I have read through the android.graphics.Canvas documentation many times. This is the insightful commentary it has about drawPoint() :

Helper for drawPoints() for drawing a single point.

Based on other documentation I've gone through, it would appear that drawPoint() will draw a single pixel if the StrokeWidth in the supplied Paint object is set to 0 (unless you set anti-aliasing to be on, then it draws more than one pixel). Not sure if the StrokeCap has any meaningful effect under those circumstances.
 

DrMrLordX

Lifer
Apr 27, 2000
22,547
12,413
136
Here is some sample code. On my system, it does not produce the expected black square of 120 pixels by 120 pixels that it should produce.

Code:
package examplepackage;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Cap;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class DrawPointTest extends Activity
{
	class RenderView extends View
	{		
		Paint pixelpaint;	
		
		public RenderView(Context context)
		{
			super(context);
			pixelpaint = new Paint();
			pixelpaint.setStrokeCap(Cap.ROUND);			
			pixelpaint.setStrokeWidth(0);
			pixelpaint.setColor(Color.BLACK);
		}
		
		protected void onDraw(Canvas canvas)
		{			
			for (byte i = 0; i < 120; i++)
			{
				for (byte j = 0; j < 120; j++)
				{
					canvas.drawPoint(1280 + i, 800 + j, pixelpaint);					
				}
			}			
			invalidate();
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				     WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(new RenderView(this));
	}
}
 
Last edited:

purbeast0

No Lifer
Sep 13, 2001
53,478
6,317
126
couple things...

1. i'd try it out without the 1280x800 offset. just to be sure it's not doing some kind of scaling and drawing it off the screen.
2. could the fact that you are setting pixel width to 0 be the problem? maybe it's drawing a 0 pixel wide stroke at each point you are trying to draw at.
3. check out the logger for the device you are running this on. it spits out a lot of information, and it could be spitting out some information telling you why it's not showing up on the screen.

again, these are just things to try out. i've never used the drawing stuff with android but these were just things that popped out to me from looking at it and where i would start if it were me.
 

DrMrLordX

Lifer
Apr 27, 2000
22,547
12,413
136
I have tried different offsets, such as no offset (which should draw starting in the upper-left corner). That didn't help.

I have tried it with no width data . . . I suppose I could try different width numbers to see if that's the issue. And, yeah, I should probably check logcat.

I'll get back to you after I try different widths.

edit: btw, the reason I'm doing this may seem a bit silly, but I'm trying to use png-8 files with no alpha channel. It is easy to get Android to handle png-8 files by setting the ALPHA_8 option and loading files that way. They show up just fine with drawBitmap(). The problem is that no alpha channel means ugly background pixels showing up where you don't want them. So, I had planned to grab each pixel's color code individually, check the color code for the uniform background color (in my case, I'm using a tacky pink that maps to EC008C in a 24-bit palette. Not sure what its 8-bit code is), and then selectively not draw those pixels. I'd draw everything else by way of drawPoint(). Only . . . drawPoint() ain't playin ball.

I can find no other way of selectively ignoring a particular color within a png-8 that does not involve dumping so much crud into memory that it offsets all the benefits of going with png-8 over png-24 + alpha in the first place. I suppose I could create separate 8-bit pngs containing nothing but alpha channel data and then apply them, which would double the amount of data I'd have to load into memory per bitmap . . . but I'd still use less memory than if I used an ARGB_8888 file.
 
Last edited:

DrMrLordX

Lifer
Apr 27, 2000
22,547
12,413
136
Switching width to 1 did not help, and logcat isn't showing anything out of the ordinary. So, no progress on that front.