创建重复的背景图片
在drawable目录下创建一个repeat_bg.xml:
然后在布局的xml文件中可以这样引用:
=================================================
tileMode 属性就是用于定义背景的显示模式:
disabled
默认值,表示不使用平铺
clamp
复制边缘色彩
repeat
X、Y 轴进行重复图片显示,也就是我们说要说的平铺
mirror
在水平和垂直方向上使用交替镜像的方式重复图片的绘制
===============================================
图片平铺的三种方式:
1)第一种利用系统提供的api实现
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
//bitmap = Bitmap.createBitmap(100, 20, Config.ARGB_8888);
BitmapDrawable drawable = new BitmapDrawable(bitmap);
drawable.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT );
drawable.setDither(true);
view.setBackgroundDrawable(drawable);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
//bitmap = Bitmap.createBitmap(100, 20, Config.ARGB_8888);
BitmapDrawable drawable = new BitmapDrawable(bitmap);
drawable.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT );
drawable.setDither(true);
view.setBackgroundDrawable(drawable);
2)第二种我们使用xml来轻松实现
< bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/img"
android:tileMode="repeat" />
3)第三种自己画出来
< span style="white-space: normal;">< span style="white-space: pre;">public static Bitmap createRepeater(int width, Bitmap src){< /span>< /span>
int count = (width + src.getWidth() - 1) / src.getWidth();
Bitmap bitmap = Bitmap.createBitmap(width, src.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
for(int idx = 0; idx < count; ++ idx){
canvas.drawBitmap(src, idx * src.getWidth(), 0, null);
}
return bitmap;
}
public static Bitmap createRepeater(int width, Bitmap src){
int count = (width + src.getWidth() - 1) / src.getWidth();
Bitmap bitmap = Bitmap.createBitmap(width, src.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
for(int idx = 0; idx < count; ++ idx){
canvas.drawBitmap(src, idx * src.getWidth(), 0, null);
}
return bitmap;