安卓背景图片移动(全屏)

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坐标+图片的高度
								//这样就绝对能保证两张图片是紧贴在一起,没有交错也不会出现缝隙
		}
	}
}
原图片的矩形类是图片的宽度高度,目的矩形类是在屏幕上绘制图片的位置和大小,高度应该一致不然会有图片的缩放,宽度必须是屏幕的宽度
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值