直接上代码,终于好了
/**
* Created by QYBM_Android_two on 2017/3/23.
* 水平平铺
*/
public class Linear_bitmap
{
public static Bitmap createRepeater(Context context, @DrawableRes int drawable, int width)
{
//获取bitmap
Resources res = context.getResources();
Bitmap srcLast = BitmapFactory.decodeResource(res, drawable);
//缩放bitmap的宽度
Bitmap src = resizeImage(srcLast,100,srcLast.getHeight());
int count = (width + src.getWidth() - 1) / src.getWidth(); //计算出平铺填满所给width(宽度)最少需要的重复次数
Bitmap bitmap = Bitmap.createBitmap(src.getWidth()*count, src.getHeight(), Bitmap.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;
}
//缩放bitmap
public static Bitmap resizeImage(Bitmap bitmap, int w, int h)
{
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleWidth = ((float) w) / width;
float scaleHeight = ((float) h) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// if you want to rotate the Bitmap
// matrix.postRotate(45);
return Bitmap.createBitmap(bitmap,0, 0, width,height, matrix, true);
}
}