Thanks everybody.  I'm using the following, inspired by the replies
above, that seems to work on 1.5 as well
without reflection.  I define MIN_FONT_DIPS to be 14.0f.

        final float scale =
getResources().getDisplayMetrics().density;
        m_defaultFontHt = (int)(MIN_FONT_DIPS * scale + 0.5f);

So I can initialize a Paint() to a font size I set in a resource?
That would be cool;
I'll look for examples.


On Jun 2, 3:31 pm, Dianne Hackborn <hack...@android.com> wrote:
> It's not a bug; you need to go through the Context/Resource to find the
> correct app scaling, and a plain constructor does not have this so can not
> take such things into account.
>
> You should define attributes like text sizes in resources, where you can use
> units like "dp" and use those values to set your size.  This is essentially
> how TextView works, where it gets a style array of the attributes (size,
> color, etc) it uses to initialize its paint.
>
> On Wed, Jun 2, 2010 at 8:30 AM, String <sterling.ud...@googlemail.com>wrote:
>
>
>
> > I think it is a bug, personally. Paint has a similar problem with
> > measureText(); it doesn't account for different screen densities
> > correctly.
>
> > My solution is similar to Kostya's: I handle it manually using the
> > reported density of the display.
>
> > String
>
> > On Jun 2, 1:38 pm, Kostya Vasilyev <kmans...@gmail.com> wrote:
> > > Eric,
>
> > > I ran into same issues with my app.
>
> > > First of all, small text can be made more readable by calling
> > > paint.setAntiAlias(true);
>
> > > Second, I do my own text scaling based on the device's screen density.
> > > Something like: textSize = unscaledSize * screenDensity / 160.0f. Note
> > > that default density for pre-1.6 devices is 160 dpi.
>
> > > Screen density is available as canvas.getDensity(), which first appeared
> > > in API 4 (Android 1.6).
>
> > > Since I want my app run on Android 1.5, I wrote a simple helper method
> > > using Java reflection that works on all versions of Android.
>
> > > public static int getScreenDensity(Canvas canvas) {
> > > if (gScreenDensity == 0) {
> > > gScreenDensity = 160;
>
> > > Class<? extends Object> canvasClass = canvas.getClass();
> > > try {
> > > Method getDensityMethod = canvasClass.getMethod("getDensity");
> > > gScreenDensity = (Integer) getDensityMethod.invoke(canvas);
>
> > > } catch (NoSuchMethodException x) {
> > > } catch (InvocationTargetException x) {
> > > } catch (IllegalArgumentException e) {
> > > } catch (IllegalAccessException e) {
> > > }
> > > }
>
> > > return gScreenDensity;
>
> > > }
>
> > > static int gScreenDensity = 0;
>
> > > Hope this helps.
>
> > > 02.06.2010 16:24, eehouse пишет:
>
> > > > My app uses a custom View that does a lot of text drawing.  To ensure
> > > > my text is always readable I've been using the result of calling
> > > > getTextSize() on a newly-created Paint() instance as the minimum
> > > > size.  Docs say calling 'new Paint()' sets attributes like text size
> > > > to default values.
>
> > > > One of my European users just got a Motorola Milestone phone and
> > > > reports that some text is unreadably small.  I believe I've duplicated
> > > > this using the WVGA854 skin in a 2.1 emulator.  Run in that emulator
> > > > my app's smallest TextViews look fine, but the text I'm drawing myself
> > > > using Paint instances where I haven't changed the text size from the
> > > > default is too small.  I confirmed this by commenting out all
> > > > setTextSize() calls in my View subclass: the text becomes too small to
> > > > read comfortably.
>
> > > > It feels like a bug to me that the default text size on Paint can be
> > > > significantly smaller than default TextView size on some devices.  Is
> > > > it a known bug?
>
> > > > In the meantime, can someone suggest a way to size Paint text so that
> > > > it's readable on any device regardless of screen resolution?  I
> > > > believe there are APIs to translate inches to pixels, so perhaps I
> > > > could hard-code a size in inches.  Or I could measure the laid-out
> > > > height of a TextView and store that as an invisible preference.  But
> > > > is there a better way, say an API I haven't found?  (I suspect it'd be
> > > > enough if Paint were like TextView in allowing you to specify the
> > > > units in which text size is set, but it doesn't.)
>
> > > > Thanks,
>
> > > > --Eric
>
> > > --
> > > Kostya Vasilev -- WiFi Manager + pretty widget --
> >http://kmansoft.wordpress.com
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
> > For more options, visit this group at
> >http://groups.google.com/group/android-developers?hl=en
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com
>
> Note: please don't send private questions to me, as I don't have time to
> provide private support, and so won't reply to such e-mails.  All such
> questions should be posted on public forums, where I and others can see and
> answer them.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to