I thought I'd create my own specialized LinearLayout so that I can
calculate the aspect ratio and center vertically. My game is only
designed to be played in portrait mode. However this code doesn't seem
to work. Spot what I am doing wrong?

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class SpecialLinearLayout extends LinearLayout {

    private final Context context;

    public SpecialLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    protected void onLayout(boolean changed, int l, int t, int r, int
b) {
        int width = r - l;
        int height = b - t;
        float aspect = 480.0f / 320.0f;
        int newheight = (int) Math.floor((float) width * (float)
aspect);
        if(newheight != height) {
            // create top margins to center vertically
            ViewGroup.MarginLayoutParams margin = new
ViewGroup.MarginLayoutParams(this.getLayoutParams());
            margin.setMargins(0, 40, 0, 50);
            this.setLayoutParams(new LinearLayout.LayoutParams
(margin));
        }
    }
}


On Nov 22, 11:26 am, Mark Murphy <[email protected]> wrote:
> rukiman wrote:
> > This is working almost 90% for my game except when testing in WVGA
> > screen resolution, all my images are scaled incorrectly, it is longer
> > vertically than horizontally. I was expecting the screen compatibility
> > to maintain aspect ratios as that of HVGA.
>
> No.
>
> "DROID has been optimized to display wide-screen multimedia (movie)
> content at its native aspect ratio of 16/9. This is different from the
> HVGA aspect ratio of 3/2, which is the traditional computer screen
> format. What this means is that when content is scaled up to "full
> screen", the horizontal (X*1.5) and vertical (Y*1.77) scaling factors
> are different. As a result, when displaying the same bitmap as a full
> screen background, round circles can appear as ovals, and squares are
> elongated to rectangles."
>
> http://developer.motorola.com/docstools/library/Support_for_Multiple_...
>
> I have no reason to believe this is DROID-specific, but rather is how
> Android scales things in WVGA800/WVGA854, when you do not supply your
> own pre-scaled resources.
>
> > Also in my layout I have a scrollview and it is designed in such a way
> > that in HVGA when it is not scrolled down, the items are hidden away,
> > however now in WVGA those things are shown. If the aspect ratio of
> > HVGA is maintained I won't have this issue.
>
> > How can I make sure the aspect ratio is maintained?
>
> You cannot control the aspect ratio, as that is dictated by the physical
> parameters of the screen. AFAIK, there is no Android equivalent of
> "letterbox" that would put black bars on either side of your app and
> give you a smaller virtual screen with 3/2 aspect ratio.
>
> > Also in my layout I am positioning some items using pixel positions.
> > Should I convert these to dpi?
>
> If you mean dip (density-independent pixels), perhaps. It depends on
> what the pixels represent. There is no hard-and-fast rule.
>
> > How do I calculate what dpi they should be?
>
> 1px = 1dip at 160dpi (i.e., traditional "normal" screen density).
>
> --
> Mark Murphy (a Commons 
> Guy)http://commonsware.com|http://twitter.com/commonsguy
>
> _Android Programming Tutorials_ Version 1.0 Available!

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

Reply via email to