主要实现原理为:将图片切割为N片,存入数组,然后通过不断的重新排序、绘制。实现图片移动并循环播放的观感。
具体代码如下:
Bitmap backGround ;
int count = 20;//切割碎片的个数
Bitmap[] backGroundes = new Bitmap[count];//装分割以后背景的图片
int eachwidth = 50;//分割的碎片的宽度
backGround = BitmapFactory.decodeResource(this.getResources(), R.drawable.background);
/*
初始化背景图片
*/
//将背景图片进行切割;
for (int i = 0; i < bmpScrolles.length; i++) {//切成小图片
backGroundes[i] = Bitmap.createBitmap(backGround, eachwidth * i, 0, 50, 900);
//Bitmap.createBitmap(要切割的Bitmap,x坐标,y坐标,碎片的宽度,碎片的高度);
}
backGround = null;
//释放背景图片的内存,后边都用不上了。
绘制过程的代码:
public void doDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
int temp = 0;
for (int start = a, i = 0; start < cutCount; b++, i++) {
canvas.drawBitmap(backGroundes[start], i * eachwidth, 60, paint);
temp = i;
}
/*
从变量a(start)开始遍历Bitmap集合,并逐一绘制。遗漏的元素交由下方的循环,将元素绘制在后方,
形成图片连续不断的观感
*/
for (int c = 0; c < a; c++) {
temp++;
canvas.drawBitmap(backGroundes[c], temp * eachwidth, 60, paint);
}
/*
将图片的顺序打乱,Bitmap数组将从a开始排列
(如:当a = 2,图片切分个数为4时,上边的两个循环将形成:2,3,1,0)
*/
if (a < count - 1) {
a++;
} else {
a = 0;
}
}