Android Image positioning to support multiple screen resolutions - android

I've had the same problem for a long time now, which is basically what the title says. I have drawn some pictures to visualize what I'm trying to achieve.
Picture of man on ground
So on this first pickture, we have a man(player) standing on the GROUND of the Background image. In a 1024x728 Device this player is positioned somewhere around the Screen Height - 120px. I wan't to support multiple screen resolutions, but when I run the App on a bigger screen, this happens of course:
picture of man not on ground
The height of the background is bigger, therefor the ground is futher away from the bottom and -120px is not enough.
(In the app; pictures are drawn with Canvas as bitmaps)
So my question is, how do I achieve to get this man to always have the position on the ground? What's best practise? And can you show me some examples?
Thanks a lot.

There are two aproaches:
1) Using RelativeLayout child of this layout can be specified in relation to other childern
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/name"> <<<< look at this
</RelativeLayout>
2) Using just added percentage support library. You can specify % position on the screen. Read this for it

Related

Putting drawables in front of one another causes dramatic used memory increase

Usual memory footprint of my app is around 20MB. However, when I put drawables one in front of the other (for the sake of animating the each individual element)...
There are actually two drawables in this picture: the green base and the pin (with its shadow)
...my memory usage goes up by ~40MB!The layout is using xhdpi drawables, which have around 2.1MB in total. The layout of these drawables is:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.2">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/serbia"
android:src="#drawable/serbia"
android:adjustViewBounds="true" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pin"
android:src="#drawable/pin"
android:adjustViewBounds="true"/>
</RelativeLayout>
What is causing such sudden memory increase? Is there a way to bypass such practice, but still be able to animate each part of the image separately?
Note that I still have to properly align these elements (the pin isn't in the center of the layout), because the right edge of the shadow is cropped to the shape of the base)
The pin and the shadow may be small but the view size is the same as the parent as you layout them so the bigger one (the parent) sets the size of all them three buffers for painting. So the biggest size multiplied by 3 (or four, I donno how actually the container paints itself) is the memory used. And by biggest I mean biggest in screen format, not in the file compressed.
Best thing to do could be to paint them yourself or stack them up using a FrameLayout and then setting x and y possition... Donno right now if u can set thrm in the xml directly but, if not, set them in code elsewhere.

Surfaceview is not being drawn completely when created initially

I am using surfaceview in my android app using following XML code.
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
The surfaceview is supposed to be drawn initially when created and when user touches it. However, when the surfaceview is created initially , it is not drawn completely. There exists a small black strip at the bottom of surfaceview. However on subsequent re-drawing, when user touches the surface, it is completely drawn. Why is this issue occuring ?
That issue happens when the layout initially is not covering the entire screen. That can either be a problem in:
The drawing
The layout
In this case, I would have to say there is something wrong with the layout. It doesn't cover the screen propperly because of height = 0dp
yes I see the weight, but if you are to make a game it is easier to use FrameLayout and put buttons on top of the canvas.
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
This would have been easier to answer with the full xml layout and the rendering code.

Supporting multiple Screen sizes with Button design

I have created a button in Photoshop. The problem is that when I test app from a big screen phone everything is good, but when I test in smaller phone then button is very big. How can I make good size button for every phone sizes?
I suppose by "I designed a button in Photoshop" you mean that you designed its background-image.
The size of a Button is actually not necessarily dependent on the size of the background image. You can control the size of your button via its properties that can be set in the layout.xml file (or in code).
In this simple example the Button will always use up exactly half of the parent layouts width (LinearLyout).
(Watch the "weightSum" and "weight" properties).
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:weightSum="2">
<Button
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Button"
android:layout_weight="1" />
</LinearLayout>
Of course, there are many more ways and properties of how to adjust your layout to different screen sizes, this should just give you a brief example of how it could be done.
For more information, check out this tutorial from the Google-Android-Developer page:
Supporting Multiple Screen Sizes (especially Density-Independence should be interesting for you)
http://developer.android.com/guide/practices/screens_support.html

how to keep aspect ratio with different android devices

So I'm trying to put an image inside a scrollview and have the image stretch to fit different sized screens. It'll stretch horizontally, but vertically it always comes out messed up.
I've read so many pages of people with similar problems, but their solutions never work for me. I've tried every different combination possible using different scaleType,layout_width and layout_height, along with trying changing other things around. The closest I've gotten to getting it to work is shown in the code below, using scaleType="centerCrop". It's the only one that stretches the image vertically in ratio, but it cuts off a big chunk of the image from the top and bottom, it'll scroll up and down the image, but only the middle part shows.
The image is a 480x5500 jpeg, if that matters. Originally before I started messing with all that, the app worked just fine on my phone, but then later I realized the image was crunched when I tried it on a tablet. There's gotta be a way to do this in the xml right? I really don't want to have to do things with the image in the java code part. Also I'm hoping to be able to do this using just one image, I don't want to have use a different image for different screen sizes. Please help, thanks.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/paper" />
</ScrollView>
may be this help you,
make both height and width wrap_content of ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
and no need ScrollView here.
Try using custom ImageView like AspectRatioImageView mentioned here:
https://stackoverflow.com/a/4688335/944070
the next sample is more than what you ask for :
http://www.androidviews.net/2012/11/photoview/
https://github.com/chrisbanes/PhotoView/tree/master/sample
https://play.google.com/store/apps/details?id=uk.co.senab.p
hotoview.sample
it fits the image to the screen , plus it allows to zoom in/out using pinching gestures.

how to display multiple image on one screen in android like layer of photoshop

I am new to android development so I don't have much experience.
I want to develop an app for children in which I want display images (png or bitmap) of a sky, mountain, river in a combined manner and children can change the color of the sky ( that means only the sky bitmap will change ).
I want to display all bitmaps one over another like a Photoshop layer so children can add a tree or a small mountain to the view by clicking a button.
Please guide me on how I can work on such image manipulation. Which techniques do I have to use for this?
Just a starting point:
All your bitmaps must have a transparent background.
Use a RelativeLayout and add your Bitmap on it (RelativeLayout allows you to place childs overlapping each other)
You can use margin to position bitmaps where you want in your RelativeLayout
EDIT
Here is a simple layout with 2 bitmaps (image1 and image2 both have a transparent background) overlapping each other.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/relativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:background="#drawable/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageView
android:background="#drawable/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
It depends on your requirements. If you really want to create complex application I would recommend you to use a game engine. Like cocos-2dx, for example. It's free, cross-platform and there are a lot of graphics processing features out of box. There are layers, filters, animation... and it works very fast.
If you are android SDK fan then create a custom view and use it's canvas. It wont be easy. But if your app is simple enough you better stick with canvas.

Categories

Resources