I wrote a game using absolute positions for a specific device. I want
to target other resolutions however I want to minimize work. I just
want to scale the whole view down and maintain aspect ratio. I can see
in 3.0 there is a setScale function added that will do the job however
I want to target 1.5 onwards. I wrote this class that works ok
visually however the events are not working 100% i.e if you press dead
in the center of the button it will work if you touch a bit up it
presses the button on top even. Also it does draw the image button
pressed states correctly. However visually it is just what I want.

public class ScaledLinearLayout extends LinearLayout {

        private static final float OPTIMUM_HEIGHT = 1024;
        private static final float OPTIMUM_WIDTH = 768;
        private float offsetX;
        private float offsetY;
        private float scale = 1.0f;
        private Context context;
        private Display display;

        public ScaledLinearLayout(Context cxt, AttributeSet attrs) {
                super(cxt, attrs);
                this.context = cxt;

                WindowManager wm = (WindowManager)
context.getSystemService(Context.WINDOW_SERVICE);
                display = wm.getDefaultDisplay();
                float scaleX = display.getWidth() / OPTIMUM_WIDTH;
                float scaleY = display.getHeight() / OPTIMUM_HEIGHT;
                scale = Math.min( scaleX, scaleY );

                offsetX = (display.getWidth() - (scale * OPTIMUM_WIDTH)) / 2;
                offsetY = (display.getHeight() - (scale * OPTIMUM_HEIGHT)) / 2;
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {

                float newX = (event.getX() - offsetX) / scale;
                float newY = (event.getY() - offsetY) / scale;
                event.setLocation(newX, newY);
                return super.dispatchTouchEvent(event);
        }

        @Override
        public void dispatchDraw(Canvas canvas) {

                canvas.save();
                canvas.scale(scale, scale);
                canvas.translate(offsetX, offsetY);
                super.dispatchDraw(canvas);
                canvas.restore();
        }
}

I make ScaledLinearLayout the parent viewgroup of my view and it will
then scale the views such that it best fits the screen and centers the
game on the screen. Only issue is the events are a bit whack. Any
ideas or a better way to do what I'm trying to achieve? Rewriting the
layouts are out of the question.

-- 
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