我正在尝试在Android中加载我想要平铺的位图.我目前在视图中使用以下内容来显示位图:
canvas.drawBitmap(bitmap, srcRect, destRect, null)
Run Code Online (Sandbox Code Playgroud)
我本质上想在我的应用程序中使用此位图作为背景图像,并希望在X和Y方向上重复位图.
我已经看到了在TileMode.REPEAT不变BitmapShader类,但我不知道这是用于重复实际的位图或用于应用过滤器位图.
bra*_*iel 126
你可以在xml而不是java代码中执行此操作.我自己没有尝试过,但我找到了这个例子.
<xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/MainLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/backrepeat"
>
Run Code Online (Sandbox Code Playgroud)
然后在一个名为backrepeat.xml的xml中
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/back"
android:tileMode="repeat" />
Run Code Online (Sandbox Code Playgroud)
Izk*_*ata 36
找出代码版本:
BitmapDrawable TileMe = new BitmapDrawable(MyBitmap);
TileMe.setTileModeX(Shader.TileMode.REPEAT);
TileMe.setTileModeY(Shader.TileMode.REPEAT);
ImageView Item = new ImageView(this);
Item.setBackgroundDrawable(TileMe);
Run Code Online (Sandbox Code Playgroud)
然后,如果你有一个drawable to tile,可以使用它来代替BitmapDrawable:
BitmapDrawable TileMe = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.tile));
Run Code Online (Sandbox Code Playgroud)
Bo.*_*Bo. 16
上面的backrepeat.xml是错误的
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/tile"
android:tileMode="repeat"
android:dither="true" />
Run Code Online (Sandbox Code Playgroud)
似乎有些人对在onDraw方法中的View中这样做感兴趣.以下代码对我有用:
bgTile = BitmapFactory.decodeResource(context.getResources(), R.drawable.bg_tile);
float left = 0, top = 0;
float bgTileWidth = bgTile.getWidth();
float bgTileHeight = bgTile.getHeight();
while (left < screenWidth) {
while (top < screenHeight) {
canvas.drawBitmap(bgTile, left, top, null);
top += bgTileHeight;
}
left += bgTileWidth;
top = 0;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
<xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/MainLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/back"
android:tileMode="repeat"
>
Run Code Online (Sandbox Code Playgroud)
这对我来说很好。我不必单独创建位图。我在布局中使用了 tileMode 属性。