package com.example.playplane;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.widget.Toast;
public class BackGround {
Bitmap bgMap1,bgMap2;
float bg1Width,bg1Height;
float left;
float top,top2;
public BackGround(Context context){
bgMap1=BitmapFactory.decodeResource(context.getResources(), R.drawable.bg1);
bgMap2=BitmapFactory.decodeResource(context.getResources(), R.drawable.bg1);
bg1Width=bgMap1.getWidth();
bg1Height=bgMap1.getHeight();
Toast.makeText(context, "背景图片高度:"+bg1Height, 0).show();
left=top=0;
top2=-bg1Height;
}
public void drawBg(Canvas canvas){
//设置源图片的矩形类
Rect rect1=new Rect();
rect1.set(0, 0, (int)bg1Width, (int)bg1Height);
Rect rect2=new Rect();
rect2.set(0, 0, (int)bg1Width, (int)bg1Height);
//设置目的矩形类(显示到屏幕上的位置)
RectF dis1=new RectF(0,top,MainActivity.screenWidth,top+(int)bg1Height);
RectF dis2=new RectF(0,top2,MainActivity.screenWidth,top2+(int)bg1Height);
canvas.drawBitmap(bgMap1, rect1, dis1, null);
canvas.drawBitmap(bgMap2, rect2, dis2, null);
moveBg();
}
public void moveBg(){
top+=5;
top2+=5;
if(top>=MainActivity.screenHeight){
top=top2-bg1Height;//第一张图片的左上角的Y坐标必须等于第二张图片的左上角的Y坐标+图片的高度,
//这样就绝对能保证两张图片是紧贴在一起,没有交错也不会出现缝隙
}
if(top2>=MainActivity.screenHeight){
top2=top-bg1Height;//第一张图片的左上角的Y坐标必须等于第二张图片的左上角的Y坐标+图片的高度
//这样就绝对能保证两张图片是紧贴在一起,没有交错也不会出现缝隙
}
}
}
原图片的矩形类是图片的宽度高度,目的矩形类是在屏幕上绘制图片的位置和大小,高度应该一致不然会有图片的缩放,宽度必须是屏幕的宽度