简体   繁体   中英

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.

If you are trying to "fill" the ImageView:

android:scaleType="fitXY"

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

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM