Repeat Image in with ImageView in RelativeLayout - android

I want to repeat the image with ImageView with in RelativeLayout. Can it be possible to use the Bitmap xml and use tilemode "repeat" with RelativeLayout, because what I have seen things on Internet they all dealing with LinearLayout.
Any help or tip will be warmly welcomed.

Create an XML file named background in Drawable folder
background.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/imagename"
android:tileMode="repeat" />
In your Relative Layout add the above XML as background
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:background="#drawable/background" />

Yes of Course, You can use it with relative layout. Use it as background of your RelativeLayout. I also used it in my project.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/top_header_bg_repeat"
android:gravity="center">
top_header_bg_repeat.xml (in drawable folder)
----------------------------------------------
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/top_header"
android:tileMode="repeat"
android:dither="true"/>
Set Bitmap in ImageView
----------------------
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="#drawable/top_header_bg_repeat"
android:scaleType="fitXY"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/top_header_bg_repeat"/>

Yes, its possible. Define your repeating image as Drawable (bitmap) with android:tileMode="repeat" in XML and use it as background of your RelativeLayout.

In all requests exists one small problem if you image will be great and smaller what size your layout, image will not resize, only repeat by X.
For repeat by Y you can do it only programmly.
I solve my problem like this (set gravity fill)
drawable bg for image
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/table_cells_bg_img"
android:tileMode="repeat" android:gravity="fill" />
and on layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="#dimen/table_cell_height">
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="#dimen/table_cell_height"
android:scaleType="fitXY"
android:src="#drawable/table_cells_bg"/>
<TextView
android:id="#+id/txtActivityTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/avatarImage"
android:text="TextView"
android:textColor="#color/white"
android:textSize="#dimen/font_size_middle" />
</RelativeLayout>
And table_cells_bg_img it's image with width=1px and height = 1px
and etc of layout, so now your image it's like background and it's will resized for any size of parent layout
Try, maybe help. To be cleared in my case i need tiled background image to full size of tablecell repeated by X coordinate (like i can do it on iOS). So i solve it like i describe.

Related

Dissolve a background drawable

I am trying to do something similar to this with the background image dissolving:
This is the code I'm using:
<LinearLayout
android:id="#+id/layout"
android:background="#color/yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizonal">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
I'm programmatically setting the background image like this:
((ImageView)findViewById(R.id.image)).setBackground(ContextCompat.getDrawable(this, R.drawable.bg_image);
If I had to guess, I need to set the opacity of the layout as it's not the image that is dissolving but the layout surrounding it. I've searched and I think I need to use setAlpha but I don't want the entire image transparent.
You can make use of Gradient in XML resource.
bg_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#color/blue"
android:endColor="#android:color/transparent"/>
</shape>
activity_main.xml
Set Image on the background layout and on top of that set gradient.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_200dp"
android:orientation="vertical"
android:background="#drawable/image"
app:layout_constraintTop_toBottomOf="#id/textView_version">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#drawable/bg_gradient"/>
</LinearLayout>
Result

Gravity of BitmapDrawable doesn't work

I have a BitmapDrawable in xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:antialias="true"
android:autoMirrored="true"
android:dither="true"
android:gravity="bottom"
android:filter="true"
android:src="#drawable/test"
/>
I used this bitmap for a Imageview like below:
<ImageView android:id="#+id/detail_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorAccent"
android:src="#drawable/bitmap_drawable"/>
The result:
You can see there is some padding in top and bottom. And the bitmap didn't aligned to bottom of the imageview, but I set android:gravity="bottom" in the bitmap xml.
I also tried android:gravity="top", It also didn't work.
What's wrong with my code. I googled, but didn't find anything about it.
I just want to know why the gravity property in bitmap xml didn't work.
Thanks in advance.
I know I can set android:scaleType="fitEnd" to the imageview which can let the imageview alignd to bottom.
Try this:  android:adjustViewBounds.
Set this to true, if you want the ImageView to adjust its bounds to preserve.So, android:adjustViewBounds="true" which fixes the issue.
Put your ImageView inside any LinearLayout and set linearlayout gravity to center or anyone you want Like Wise..
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/iv"
android:layout_width="50dp"
android:layout_height="50dp"
/>
</LinearLayout>
This should be work..
Thank you

Android background animation issue

I have linear layout and background drawable on it, and ImageView inside which I'm animating. If I remove that background image from linear layout animation is very smooth, but when I put the background image back on, it's very very slow. Any idea why and how to solve this?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#drawable/background"
android:orientation="vertical" >
<ImageView
android:id="#+id/demoImage"
android:layout_width="wrap_content"
android:src="#drawable/temp"
android:maxWidth="200dp"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_height="wrap_content"/>
</LinearLayout>
And I'm applying only these two animations to image:
AnimationUtils.loadAnimation(this, R.anim.zoomin);
AnimationUtils.loadAnimation(this, R.anim.zoomout);
And they work fine without this part, but extremely slowly (frame by frame) with this part:
android:background="#drawable/background"
This background.png is 750x600px png file ~40kb size.
ANSWER
For some reason (don't know why), after creating custom drawable like this animation is smooth again:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<color android:color="#FFFFFF"></color>
</item>
<item>
<bitmap
android:src="#drawable/background"
android:gravity="top"
/>
</item>
</layer-list>
Try switching the scaletype in xml code to fitCenter. centerInside doesn't reduce the amount of data that is being processed and just zooms the image in to fit in the container.
Here:
<ImageView
android:id="#+id/demoImage"
android:layout_width="wrap_content"
android:src="#drawable/temp"
android:maxWidth="200dp"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_height="wrap_content"/>

How to set the ScaleType on a LinearLayout's background image

I have a linear layout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#drawable/bgapp" >
What I need now is the possibility to set the background image ScaleType value in order to fill the screen everytime, this because I'm going to download the image and then I'm going to set it as background but without ScaleType the images will not keep it's aspect ratio based on the screen densities.
What I found is this solution:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/bgapp"
android:scaleType="centerCrop"
android:gravity="bottom|left" />
But It doesn't work as expected.. Is there a way to set a LinearLayout scaleType property or do I need to change my layout?
View's background is stretched depending on the size of the View.
Image scaling is supported by ImageView, and to use it together with the LinearLayout you need an additional container, that will overlay the 2 children (e.g. a FrameLayout):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/bgapp"
android:scaleType="centerCrop"/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
...
</LinearLayout>
</FrameLayout>

Bitmap gravity set to center not filling the screen even though the image is big enough

I have a RelativeLayout in my Android project. This has it's background set to a Bitmap:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:src="#drawable/splash2" />
splash2 is a PNG image sized 2560x1440 pixels. I haven't set the background of the layout directly to the image, because the default scale mode (or gravity) is fill which stretches the image to fit the screen. With center it should take correct size image from the center and show it unscaled. In case of a vertical 1080x1920 screen, it should take that big piece and center it in the layout.
However, I have a problem. The image is bigger than any screen out in the market today. Still, with my Nexus 7, which has a 1920x1080 screen, it has borders around the image. The layout is set to full screen. The image is shrinked vertically.
How do I fix this?
You can use android:gravity="fill" to cover vertical & horizontal direction
For splash image try gravity="center|fill"
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/black" />
<item>
<bitmap
android:gravity="center|fill"
android:src="#drawable/fondo" />
</item>
</layer-list>
ImageView's scale type centerCrop was what I wanted. Unfortunately I couldn't specify this property for bitmaps. I changed my splash screen layout to FrameLayout and added an ImageView and TextView overlapping each other. This way I was able to achieve what I wanted.
<FrameLayout 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"
tools:context=".SplashScreen"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/splash2"
android:scaleType="centerCrop" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/roadSignName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#FFF"
android:gravity="center_vertical|center_horizontal"
android:padding="10dp"
android:text="My program"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="40sp" />
</RelativeLayout>
</FrameLayout>
Try adding this to the bitmap:
android:gravity="fill_vertical"
This should fix it.
P.S. Sorry for the first answer, I edited it now.

Categories

Resources