Background resized by Navigationbar - android

i am developing an Android App and want to use a picture as background in my Activity. On devices without the Navigationbar (Devices with a Homebutton) it looks great. When the device has no hardware buttons, Android scales the backgroundpicture because the height of the screen is reduced by the Actionbar.
My Question is not about xhdpi, hdpi and so on, but about handling for 16:9, 16:10...
How can I solve this?

How are you specifying the background image in your layout? If you are using an android:background attribute on a View then you will have no control over image scaling. It sounds like this is what you are doing.
Instead create an ImageView with appropriate scale type. For example I use the following structure:
<FrameLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:focusable="false"
android:src="#drawable/bg" />
<!-- Rest of your layout -->
</FrameLayout>

Related

how to fit imageview to screen in xml? centerCrop vs fitXY

So I'm working on Pacman for Android as a learning exercise. I've got the background set in my activity layout, but I'm having trouble making the background fit properly.
I've tried centerCrop and fitXY, with different results, neither of which work exactly. I want to stretch the view to the dimensions of the screen. It appears however that the image view is restricted to the center, like I have 3 imageviews ... but I have only one.
Any thoughts? Do I have to do some of this in code?
Thanks!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000FFF"
tools:context="srg.pmd.GameActivity"
>
<ImageView
android:id="#+id/emptyPacman"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/blank_screen"
android:scaleType="fitXY"
/>
First screenshot is fitXY
Second is centerCrop
The image you are using must be 320x480 then only the image will fit the screen other wise it will get distorted here is a link for more info about the image size Android: Background Image Size (in Pixel) which Support All Devices .hope it works for u
Well, nuts to me. The actual png file was wrong -- it was a lot wider than I assumed. Taking a closer look with an image editor revealed the problem.

Android Full Screen Image Handling

I'm trying to display an image but for some reason it's only giving me a smaller (unreadable) version. Is there a way to display the image full screen and enable zooming in/out? I used a fragment to display the image--do I need to create another class to extend the particular fragment...? Here is the imageview:
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="5dp"
android:scaleType="centerInside"
android:src="#drawable/rules" />
If the image is not appering in the full screen, probably there is an issue with the ImageView container. The ImageView may be "filling the parent" but the parent is not filling the screen.
Other issue may be the scaleType, are you sure your image is the correct size? If you want to stretch it you have to change the scaleType.
How about this :
Get screen dimensions in pixels
and then something like this (IF you want to resize the image):
Android image on tablets and phones
This is not a complete answer to your question but the programming should be straightforward once you know the size of the screen and the size of the image.

The background distortion when the screen turn from vertical to horizontal?

In my fragment , I set the background to a Relativelayout.
But when the screen turn from vertical (Portrait) to horizontal (Landscape) , the background image seems like distorted.
It can not set scaleType to Relativelayout.
Does there has any method can make the background auto scaling when the screen turn from vertical to horizontal?
(Does not need to detect the screen size , just let the background image auto scaling and no distortion)
Thanks in advance!
Put an Image-view inside your Relative Layout and use it as a background, like this:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/myBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/my_bg_image" >
</RelativeLayout>
This way you can put other views on top of your bg and it will scale properly.
If your background image is created for Portrait mode and if it has graphics or texture involved then it is obvious that it will be distorted when seen in Landscape mode.
In this scenario, instead of using one background images,you can use two different background images one for Portrait mode and one for Landscape mode.
Create two different folders of layout
/res/layout/
/res/layout-land/
Put you layout xml in both of these folders with different background images
For Example
1. /res/layout/
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background-port">
<!-- layout code will go here -->
</RelativeLayout>
Second layout will have different background image (created for landscape mode)
2. /res/layout-land/
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background-land">
<!-- layout code will go here -->
</RelativeLayout>
This is not direct answer of your question which is about using scaleType in RelativeLayout but the standard way by which your issue can be resolved
Also if your background image does not contain complex graphics or textures, think about using Nine-patch drawables for background. In this case you won't have to create different folders as mentioned above. Please check This for more information

Custom progressbar not fitting properly on screens with different resolution and density

Hi I am stuck with an issue and need some expert suggestions. I have a custom circular progress par whose boundary rotates as an animating light in my splash screen
It looks like this:
The boundary light continuously rotates till splash screen is there.
Now my splash screen has a background image which has a circle drawing at upper half of image:
It looks like this:
Now my circular progress bar should fit on that background circle boundary. What ever i do, it fails for some phone screens (circle is misplaced).
How can i fit into background circle and it can work on all phones and tablets.
Please let me know if further clarification is needed.
This is how I have defined circular progressbar:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/my_splash_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/splashscreen" />
<ProgressBar
android:id="#+id/progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="170dp"
android:layout_height="170dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminateDrawable="#drawable/my_progress_indeterminate"
android:visibility="visible"/>
</RelativeLayout>
I think you need to change the style attribute and set your android:layout_width="match_parent". The problem is you are setting the style to large which by default has its size pre-defined. Further in your width and height attribute you are trying to resize it. So this might be the problem.
Try out with the HoloCircular ProgressBar is a Custom View implementation for Android.
Checkout the HoloCircular ProgressBar which might help you.
Other Library that you can use are as below:
1) Progress Wheel
2) ProgressBar
3) Customize the Spinner
Thanks.
This link helped me resolve the issue to some extent as it now suppots majority of devices. Still I could get a general solution to the problem. But I can consider this as answer:
Application Skeleton to support multiple screen

Why does my striped background scale with such a bad quality?

I have an activity that includes a striped background image. Click to enlarge – this is the original image I'm using. There's also a version with a logo for another activity.
It looks fine as is. I set it as a background in the activity through XML, something like this.
I need the hack as an ImageView to be able to properly centerCrop a logo in the background, but this is irrelevant to the issue, as it persists when setting the background for the RelativeLayout and removing the ImageView.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/background_striped" />
Note that since there has to be a logo inside the image, I can't stretch it artificially or have it repeat.
In the layout preview screen, it looks like this:
In the emulator, it looks like this:
As you can see, it looks quite bad, compared to how I'd expect it:
Naturally, I've created versions of the striped background for all display resolutions (from ldpi to xhdpi), but they always seem to be scaled in a pretty bad way.
Is there any way to get the background scaled in a higher quality? Just using the xhdpi version won't result in better quality—it will just make the scale result worse and more washed out.
For scalable drawables it is better to use a 9-patch. You can create one with your small striped background image using the android 9-patch tool:
http://developer.android.com/tools/help/draw9patch.html
Try using repeat. I tested it and it seems to look fine.
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background_striped"
android:tileMode="repeat" />

Categories

Resources