I have a background on my android app with is a png image. It is 800 x 480 px in size.
It is being stretched width wise and hence does not look as I intend it to. As the image is actually the background of the app and not just an imageView I was unable to use the android:scaleType="centerCrop" to get the image to scale equally.
I need the image to fill the background equally.
I have also tried using NinePatch drawable but the results were not good at all on this - The image became blurred or was OK when being stretched vertically but not width wise.
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/menu"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
Is there any other command I can use to get the background to display correctly?
to resolve this use draw9patch
android-sdk/tools/draw9patch
https://developer.android.com/tools/help/draw9patch.html
use imageview
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/your_image"
android:scaleType="fitCenter"
/>
check this also Link
ImageView.ScaleType CENTER_CROP
Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding).
Related
I'm creating an app that uses an ImageView in the main activity that covers the whole width of the screen, the height should fit the image's resolution.
Here's my xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:adjustViewBounds="true"
android:id="#+id/bgImage"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/bg_home_blue"/>
</RelativeLayout>
Here's what my layout looks like:
Everything seems to work fine but I don't know where these paddings are coming from, I've tried to change the scaleType but nothing seems to work.
Edit:
If I put instead of src just a blue background, the paddings go away and it fills the whole width (than I need to set a specific height as well)
I guess your image is png and have that space around it, even when your scaleType is FitXY it doesn't mean that it will remove the space around the png file, check that out and let us know what happened, or at least upload your image file
I want to display an image as background in my app. Now I used this statement: android:background="#drawable/background" in <LineraLayout>. Here is the .xml Code:
<LinearLayout 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:orientation="vertical"
tools:context="overbit.user.MainActivity"
android:background="#drawable/background">
</LinearLayout>
Now I get this output:
But I want it like this:
Maybe someone of you can help me. Thanks.
There is BitmapDrawable, which allows to do it. First you need to create a drawable resource as follows (let it be a file in the project resources res/drawable/mybg.xml ):
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:gravity="center" />
And then specify it as a background resource in your layout:
<LinearLayout 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:orientation="vertical"
tools:context="overbit.user.MainActivity"
android:background="#drawable/mybg">
</LinearLayout>
You need to change the image's size itself in photoshop or something to achieve the desired result (still this can be very difficult because of various screen sizes of android smartphones). A workaround can be to change your parent layout to Relativelayout and then use a an imageview to show your picture like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent" //make sure its match parent
android:layout_height="match_parent"// and this one also
android:src="#drawable/yourBackgroundImage"
android:scaleType="..."/>
<LinearLayout>put your childviews here</LinearLayout>
there are 4-5 options available for android:scaleType... experiment with them, till you get your desired result. Also note that you need to use android:src rather than android:background attribute of the imageview
You can accomplish what you're trying to do by using BitmapRegionDecoder.
From the docs:
BitmapRegionDecoder can be used to decode a rectangle region from an
image. BitmapRegionDecoder is particularly useful when an original
image is large and you only need parts of the image.
It allows you to decode a Rect area of a particular Bitmap fairly easily, the only thing you need to do is calculate your Rect region to decode relative to the Bitmap.
In android the position of an ImageView is determined by the top left side of the image. I am assuming that the x direction is 1/3rd of the width from the start point. Now we can set the pivot. Basically the location of the pivot point, is a location around which the view will rotate and scale.
int displacementX = ImageView.getWidth() / 3;
int displacementY = 10;
imgview.setPivotX((float)displacementX);
imgview.setPivotY((float)displacementY);
Now that we have set the pivot we can use a scale type to fit the image.
imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);
I am using AndroidSlidingUpPanel library.
https://github.com/umano/AndroidSlidingUpPanel
My activity_main.xml has two children. The first child is the main layout. The second child is the layout for the sliding up panel.
here I have posted the layout of the Sliding Up Panel.
There are 4 LinearLayouts defined in the ratio percentage of 15 : 15 : 60 : 10 %.
The first layout will be visible in the panel. Now I want the panel height to be 3/4th of the first layout. So here, the first layout is taking 15% of the screen but the panel height should be around 10-12 % of the screen.
Is there any way or formula that I can use to get the screen size and that sets the panel Height according the 1st layout of the xml file provided here.
I have tried to define the panel height in the Java code. but no success till now.
Sometimes the 2nd layout is also shown in the panel, sometimes only half or 1/4th of the 1st layout.
PS : Is there any way to find a height of a view BEFORE setContentView()?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_weight="1.5"
android:background="#666666" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_weight="1.5"
android:background="#888888" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="6"
android:background="#eeeeee" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#202033" >
</LinearLayout>
It is possible to get the height of the Screen in Pixels. It is also possible to get the density of the screen(DPI).
The ratio of the two will give you the physical height of the screen.
DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
float dpWidth = displayMetrics.widthPixels / displayMetrics.density;
After getting the dpWidth (which is a physical quantity independent of density), you can easily apply percentages to it.
I need to change a image width
original image like the first line below:
but when I change the width it keeps the ratio resize the original image
I need to do something like the third image
can change the width and don't modify original image
How can I do this through Java or XML in android?
xml code:
<ImageView
android:id="#+id/fill"
android:layout_width="150dp"
android:layout_height="32dp"
android:layout_alignLeft="#+id/BGfill"
android:layout_centerVertical="true"/>
Can you help me to configure my layout? I don't achieve what I'm looking for:
I've put the imageview background to red for explain what I want:
The image's width is smaller than the layout width. I'd like the image grow proportionally keeping its aspect ratio until reach the aspect at the first image (I modified it manually with photoshop) but when setting width="fill_parent" the result is the second image and setting both width and height to "fill_parent" the result is the third image.
I tried to combine with ScaleType with no success
How can I achieve the first option? Thanks
Use android:adjustViewBounds=true attr in xml for ImageView
This question is solved here by extending the ImageView class.
Android ImageView adjusting parent's height and fitting width
I created a sample. It works for me. Here is the xml layout file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:src="#drawable/hydrangeas"
android:layout_height="wrap_content" android:id="#+id/imageView1"
android:scaleType="centerInside"
android:adjustViewBounds="true"
android:background="#android:color/white"
android:layout_width="wrap_content"/>
</LinearLayout>
It works for fitCenter too. Have a look at the snapshot.
Use the attribute fit_center into your ImageView tag.
<ImageView ... android:scaleType="fitCenter" .. />