[mono-android] DrawLine problem with Paint.StrokeWidth = 1

Jonathan Pryor jpryor at novell.com
Mon Mar 21 15:33:22 EDT 2011


On Mar 21, 2011, at 12:43 PM, Narcís Calvet wrote:
> Finally came up with the code example below which reproduces the text
> problem with minSdkVersion attribute. At
> http://www.steema.us/files/public/support/TextIssue.zip you can download the
> two screenshots I took. One has the minSdkVersion attribute and the other
> not. Initializing the Paint object using the antialias flag doesn't help
> either. Do you have any idea on what may be going on here?

Repeat after me: Android hates me.

Wait, wrong koan.

As suggested earlier, I surmised that Android goes to some degree of backward compatibility so that older apps will look "right" on more recent platforms, and that providing //uses-sdk/@android:minSdkVersion "turns off" this backward compatibility.

Eureka! Urls!

	http://developer.android.com/guide/practices/screens_support.html#attrs
	http://developer.android.com/guide/topics/manifest/supports-screens-element.html

And probably hundreds of others that I'm not aware of.

That being the case, pay particular attention to the first URL above, particularly the table header:

	Attribute
	Description
	Default value, when minSdkVersion or targetSdkVersion is 4 or lower
	Default value, when minSdkVersion or targetSdkVersion is 5 or higher

In short, when it comes to screen handling, _lots_ of things changed at API level 5 (which you're targeting), in particular the //supports-screen/@android:anyDensity attribute (and others).

This is why when you're missing //uses-sdk you have ~52 lines shown, and when you have //uses-sdk you have ~77 lines shown: Android thinks you're an older program, and scales your output accordingly.

That background out of the way, what's the fix?

Manually manage DPI yourself, by scaling your output appropriately. For example, this works for me in MyControl.OnDraw, taking the number of lines shown down from ~72 to ~48:

	protected override void OnDraw(Canvas canvas)
	{
		base.OnDraw(canvas);

		Display disp = GetDisplay();

		var metrics = new DisplayMetrics();
		disp.GetMetrics (metrics);

		Paint p = new Paint();
		p.Color = Color.Lime;
		p.TextSize *= metrics.Density;

		int count = 0;

		for (int y = 0; y < disp.Height; y=y+(int) (10*metrics.Density))
		{
			Random rand = new Random();
			canvas.DrawText(count.ToString() + " - " + rand.Next().ToString(), 100, y, p);
 			count++;
		}
	}

 - Jon



More information about the Monodroid mailing list