android屏幕的实现方式,可以让同一张图片按顺序排列成一个整张大的图片,如何实现呢?请看一下三种实现方式,建议使用第二种实现方式,简单容易维护。
(1)第一种利用系统提供的api实现
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
//bitmap = Bitmap.createBitmap(200, 30, 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)第三种自己画出来
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;
}
(1)第一种利用系统提供的api实现
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
//bitmap = Bitmap.createBitmap(200, 30, 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)第三种自己画出来
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;
}
本文介绍了三种在Android中实现同一张图片重复排列的方法:使用系统API、XML配置及自定义绘制。推荐使用XML配置方法,因为它简单且易于维护。
251

被折叠的 条评论
为什么被折叠?



