开发者

How to tile a drawable file in ImageView?

开发者 https://www.devze.com 2023-01-15 00:28 出处:网络
Like the title. I have 开发者_JS百科a small drawable file, but the ImageView is much larger than it. How I can fill it without left any extra space?

Like the title. I have 开发者_JS百科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);
0

精彩评论

暂无评论...
验证码 换一张
取 消