SDK2.1, WVGA800 return wrong getWidth(), getHeight()

Common bugs/problems with the Android SDK the Emulator and the ADT-Plugin.

Postby pskink » Thu Apr 08, 2010 8:20 pm

ok

remove

opts.inScaled = false

and add

opts.inDensity = 240
opts.inTargetDensity = 240
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top

Postby sblantipodi » Thu Apr 08, 2010 9:09 pm

pskink wrote:ok

remove

opts.inScaled = false

and add

opts.inDensity = 240
opts.inTargetDensity = 240


you are a friend, thanks for your help dear pskink.
Can you explain me why it works well in this way please?
opts.inScaled = false; would be more convenient because
in this way I will have to manage all the density...
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Postby pskink » Thu Apr 08, 2010 9:22 pm

sblantipodi wrote:
pskink wrote:ok

remove

opts.inScaled = false

and add

opts.inDensity = 240
opts.inTargetDensity = 240


you are a friend, thanks for your help dear pskink.
Can you explain me why it works well in this way please?
opts.inScaled = false; would be more convenient because
in this way I will have to manage all the density...


well, to be honest, i dont know, i just made "try & error" scheme

basically its rather rare situation to have image not scaled since with bigger density and the same physical screen size your image will be physically smaller - do you really want this?

but if you really do want that behavior its easy to obtain density from some class - is it DisplayMetrics or something...
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby sblantipodi » Thu Apr 08, 2010 10:04 pm

pskink wrote:
well, to be honest, i dont know, i just made "try & error" scheme

basically its rather rare situation to have image not scaled since with bigger density and the same physical screen size your image will be physically smaller - do you really want this?

but if you really do want that behavior its easy to obtain density from some class - is it DisplayMetrics or something...


Yes I want this because the android auto-scaling is really bad, image scaled looks terrible,
I prefer to use two separate images than a scaled one.

This piece of code will solve the initial problem.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.content.res.Resources;
  2. import android.graphics.Bitmap;
  3. import android.graphics.BitmapFactory;
  4. import android.graphics.BitmapFactory.Options;
  5. import android.os.Build;
  6. import android.util.DisplayMetrics;
  7.  
  8. public abstract class BitmapFactoryAbstract {
  9.  
  10.     /**
  11.      * Classe creata per gestire le immagini unscaled sia su Level3 che su Level4
  12.      * @author sblantipodi
  13.      */
  14.     public static final BitmapFactoryAbstract instance;
  15.  
  16.     // Se livello 3, se livello 4
  17.     static {
  18.         if (Integer.parseInt(Build.VERSION.SDK) < 4) {
  19.             instance = new Level3();
  20.         } else {
  21.             instance = new Level4_or_higher();
  22.         }
  23.     }
  24.  
  25.     // Carico la risorsa
  26.     abstract Bitmap load(Resources resources, int resId, BitmapFactory.Options options);
  27.  
  28.     // Questo è il medoto che chiamo dalle varie classi al posto del metodo originale decodeResource()
  29.     public static Bitmap decodeResourceUnscaled(Resources resources, int resId, BitmapFactory.Options options) {
  30.         return instance.load(resources, resId, options);
  31.     }
  32.  
  33.     // Siamo sul livello 3, chiamo il metodo decodeResource liscio senza modifiche
  34.     private static class Level3 extends BitmapFactoryAbstract {
  35.  
  36.         @Override
  37.         Bitmap load(Resources resources, int resId, Options options) {
  38.             return BitmapFactory.decodeResource(resources, resId, options);
  39.         }
  40.     }
  41.  
  42.     // Siamo sul livello 4, chiamo il metodo decodeResource dicendogli di non scalare.
  43.     private static class Level4_or_higher extends BitmapFactoryAbstract {
  44.  
  45.         @Override
  46.         Bitmap load(Resources resources, int resId, Options options) {
  47.             if (options == null) {
  48.                 options = new BitmapFactory.Options();
  49.             }
  50.             // Non so perchè non funziona
  51.             // options.inScaled = false;
  52.             // Prendo i DPI del display
  53.             DisplayMetrics metrics = new DisplayMetrics();
  54.             MyMainClass.instance.getWindowManager().getDefaultDisplay().getMetrics(metrics);
  55.             // Imposto i dpi
  56.             options.inDensity = metrics.densityDpi;
  57.             options.inTargetDensity = metrics.densityDpi;
  58.             return BitmapFactory.decodeResource(resources, resId, options);
  59.         }
  60.     }
  61. }
  62.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Simply call
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. myImage = BitmapFactoryAbstract.decodeResourceUnscaled(getResources(), R.drawable.myImage, null);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


and the image will be drawn without scaling on both Level3 and Level4 with the same .apk.

Really thanks pskink. Really thanks.
PS: I'm sorry for the italian //comment, simply remove it.
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Postby pskink » Fri Apr 09, 2010 5:26 am

sblantipodi wrote:Yes I want this because the android auto-scaling is really bad, image scaled looks terrible,
I prefer to use two separate images than a scaled one.

and the image will be drawn without scaling on both Level3 and Level4 with the same .apk.

Really thanks pskink. Really thanks.


i still don't know how do you want to get 800x480 with lvl3

i think that all you can get on WVGA in lvl3 is 480x320
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby sblantipodi » Fri Apr 09, 2010 10:04 am

pskink wrote:i still don't know how do you want to get 800x480 with lvl3

i think that all you can get on WVGA in lvl3 is 480x320


That class is born to display an unscaled image of "230 pixel, or smaller or bigger" in width,
in an unscaled way on Level 3 or higher.
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Top

Postby pskink » Fri Apr 09, 2010 10:27 am

sblantipodi wrote:
pskink wrote:i still don't know how do you want to get 800x480 with lvl3

i think that all you can get on WVGA in lvl3 is 480x320


That class is born to display an unscaled image of "230 pixel, or smaller or bigger" in width,
in an unscaled way on Level 3 or higher.


i mean if you want to use different densities/screen sizes you need lvl4 and screens-supports tag

if you use screens-supports you cannot run your app in device with lvl3
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby sblantipodi » Fri Apr 09, 2010 10:31 am

pskink wrote:
i mean if you want to use different densities/screen sizes you need lvl4 and screens-supports tag

if you use screens-supports you cannot run your app in device with lvl3


the
<supports-screens>
tag is simply ignored on level3 phone, so I can add this XML attribute also on Level3.
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Postby pskink » Fri Apr 09, 2010 10:37 am

sblantipodi wrote:
the
<supports-screens>
tag is simply ignored on level3 phone, so I can add this XML attribute also on Level3.


did you check it?

what about api that is introduced in lvl4?

is it also ignored?

i don't think so...
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby sblantipodi » Fri Apr 09, 2010 10:40 am

pskink wrote:did you check it?

what about api that is introduced in lvl4?

is it also ignored?

i don't think so...


Yes, I know it. I don't use level4 API on Level3 thanks to the abstract class.
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Postby pskink » Fri Apr 09, 2010 10:49 am

ok

what is your target in your default.properties (file in the top of your eclipse project)

target=android-X
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby sblantipodi » Fri Apr 09, 2010 10:59 am

pskink wrote:ok

what is your target in your default.properties (file in the top of your eclipse project)

target=android-X


I use netbeans and I haven't a target.
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Postby pskink » Fri Apr 09, 2010 11:24 am

ok, but even using netbeans you have to specify which android version you build your project against?

what version is it?
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Postby sblantipodi » Fri Apr 09, 2010 12:12 pm

pskink wrote:ok, but even using netbeans you have to specify which android version you build your project against?

what version is it?


Level3 is ok, it compile and run if I use abstract class and <supports-screens> xml tag.
sblantipodi
Senior Developer
Senior Developer
 
Posts: 122
Joined: Fri Mar 26, 2010 1:41 pm

Postby pskink » Fri Apr 09, 2010 12:16 pm

do you compile your project against level == 3 or level >= 4 ?
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Top
PreviousNext

Return to SDK/ADT/Emulator Problems

Who is online

Users browsing this forum: No registered users and 1 guest