Layout problems with 9-patch background image - android

I have a 9-patch background image for my layout and it works fine when the display resoluton is smaller then the image (800x480px). But when I test it on a device with a bigger resoluton, for example 1280x800px, than the layout is broken. Here are two pictures:
The normal and broken layout
The 9-patch image
Here is the relevant layout-code:
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/chalkboard"
android:gravity="top|center"
android:orientation="vertical" >
All in all, the 9-patch doesn't work when the image scales up. But what's the reason and how can I handle it?
Best regards!

You are stretching the frame as well. Just don't stretch the frames on the side. This should do the work:

Related

Enlarge image without blur

I have got a very tiny image:
(image made on gimp, it would be invisible if I pasted it here)
It's 5x5px in size and it has some pixels with 100% transparency and others with 80%.
I'm trying to use it as a background for an ImageView. I want to scale it to higher dimensions. However, when I do that, the overall effect becomes blurry and not what I truly want.
XML Layout
<?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"
android:background="#000">
<ImageView
android:layout_width="500dp"
android:layout_height="500dp"
android:background="#drawable/boxes"/>
</RelativeLayout>
Unwanted result
Question
How do I make it look good? That is, with sharp corner squares.
Investigate using an SVG for the background - these should appear sharp at all sizes
At first, try to resize it MS paint. If this process fails, take a screenshot of that picture to get a more clear view and then resize it again.

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: Using 9patch to create a frame around an imageview

I've been trying to create a frame around my imageview using a picture of a wooden frame. I turned the woodenframe picture into a 9patch and its still not wrapping around the imageview.
<RelativeLayout
android:id="#+id/ChosenPic"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_weight="3"
android:orientation="vertical"
android:padding="10dp" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:baselineAlignBottom="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/ImageView02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill_vertical|fill_horizontal"
android:scaleType="fitXY"
android:src="#drawable/woodenframe" >
</ImageView>
</RelativeLayout>
So this is the ninepatch and i want it to fit around a picture of my choosing no matter the size.
EDIT:
This is what i want.
You have two options. I recommended the first one.
First option:
Put each image view in a separate LinearLayout and apply the 9patch to the LinearLayout it self not the image view
Second option:
Use or set the background resource of the image view in Java to the 9patch resource or the background attr in xml to the 9 patch resource and the src to the image it self.
Note that it better to use a Layer-List and add a combination of two shapes or whatever rather than using a 9patch resource.
Reference:
https://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList
The "nine-patch" frame image that you upload is not a proper nine-patch.
First, it is a jpg. The extension should be .9.png.
Second, the contents are not correct. Specifically:
You need to have a 1px transparent border around the entire image. I do not see that in your jpg, though that just be an artifact of exporting it as a jpg.
You need to define a stretchable region by drawing black pixels in that 1px border. I see no black pixels defining such a region in the uploaded image.
I highly recommend going back and reading the nine-patch documentation again, and trying the draw 9-patch tool for creating a proper nine-patch.
Your 9-Patch is not configured correctly.
It isn't enough to just rename your file. To get it working properly, you have to define certain areas that should be stretched to fit the dimensions of your view.
You can also define paddings for that 9-Patch to inset the content when applying the 9-Patch to your view.
There's a handy tool called "Draw 9-Patch" in the android SDK that helps you defining these areas:
http://developer.android.com/tools/help/draw9patch.html
To start it, simply run the
draw9patch.bat
located in
...\sdk\tools\

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

Android inset transparent drawable

So I have a PNG as shown below. The white inner in the image is transparent.
My problem is that this image isn't really scalable for different size screens on Android devices.
I would like to know how I can do this either in code or preferably, xml. Basically I am looking for a grey square with a transparent circle inset.
Also, there is a nice effect on the inner border of the grey, is this possible in code?
I know how to do the inverse (bitmap with rounded corners) but how would I do this? Any ideas appreciated, thanks.
Edit:
So, the only thing I have tried is android:scaleType="centerCrop"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:scaleX="0.7"/>
</LinearLayout>
Can you try this one.
It's not clear from your question why the image is not scaling properly for different screen sizes.
Using NinePatch PNG format you have full control how the image scales. For example, this enables you to force that only the image gray areas are extended, keeping the circle format, if that is your problem.
The documentation can be found here: Draw 9-patch
Regards.

Categories

Resources