landry
landry

Reputation: 561

How to tile a drawable file in ImageView?

Like the title. I have a small drawable file, but the ImageView is much larger than it. How I can fill it without left any extra space? Thanks in advance.

Upvotes: 1

Views: 5168

Answers (3)

sud007
sud007

Reputation: 6151

Create a drawable in res folder, name it tile_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/zig_zag"
    android:tileMode="repeat" />

Use this drawable as the Background for your ImageView, Done!

<ImageView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/tile_drawable" />

That's the way to do it via XML

Upvotes: 5

VSB
VSB

Reputation: 10405

You can use below code snippet:

BitmapDrawable bitmapDrawable  = new BitmapDrawable(getResources(),BitmapFactory.decodeResource(getResources(), R.drawable.tile_image));
bitmapDrawable.setTileModeX(TileMode.REPEAT);
imageView.setImageDrawable(bitmapDrawable);

this sample fills imageView with tile_image and repeate it in X-Direction. You can use other method to tile in Y-Direction or both:

bitmapDrawable.setTileModeY(TileMode.REPEAT);
bitmapDrawable.setTileModeXY(TileMode.REPEAT,TileMode.REPEAT);

Upvotes: 2

Paul Burke
Paul Burke

Reputation: 25584

If you are trying to "fill" the ImageView:

android:scaleType="fitXY"

If you are trying to "tile" look into BitmapDrawable and setTileModeXY()

Upvotes: 5

Related Questions