Android仿开心消消乐大树星星无限循环效果
程序员文章站
2024-03-05 09:21:36
啥都不说先上效果图,这个是我项目里的效果:
下面的是我抽取出来的 demo 适配啥的我基本上都做好了没做其他的
ok 下面 说一下思路把
首先 说一下原理 我...
啥都不说先上效果图,这个是我项目里的效果:
下面的是我抽取出来的 demo 适配啥的我基本上都做好了没做其他的
ok 下面 说一下思路把
首先 说一下原理 我是使用bitmap 创建两张图 一开始的时候 一张在下面记为1号 一张在上面 记为2号
当手指向下滑动时 判断 1号 的起点位置 是否完全超出屏幕 如果超出屏幕的话 那么将2号变为下面 1号变为上面 (移动1号的y坐标) 大体思路 是这样. 里面还有许多判断
比如 是否能向下滑动 起点位置, 星星的判定 哎呀 好烦 说的东西太多啦 来张我的草稿纸 哈哈 话说我草稿都写了好几页
先上代码:
/** * created by liuml on 2016/6/11. */ public class myscrollview extends view { // 其资源引用的是同一张图片 private bitmap bmpbackground1; private bitmap bmpbackground2; int speed; int pointx; int tempy1; private context mcontext; private int isfirst = 1; // 按钮的坐标 private int btnx, btny; // 按钮是否按下标识位 private boolean ispress; // 界面背景坐标 private int bg1x, bg1y, bg2x, bg2y; private int cloudx, cloudy; list<map<string, object>> maplist; // 星星坐标 private int stars0x, stars0y, stars1x, stars1y, stars2x, stars2y, stars3x, stars3y, stars4x, stars4y, stars5x, stars5y, stars6x, stars6y, stars7x, stars7y, stars8x, stars8y, stars9x, stars9y, stars10x, stars10y; // //定义第二页的星星坐标 private int maxstars = 80;// 最大的星星数量 80个 private int textcontnt[] = new int[maxstars];// 数字 private int pointsy[]; private int pointsx[]; private int pointy, movey = 0; private int activitynumber = 2; private bitmap treebackground;// 界面背景 private bitmap cloud_bg;// 界面背景 private bitmap starsbg;// 星星 private bitmap starsbg_n;// 星星未做的 // 声明一个resources实例便于加载图片 private resources res = this.getresources(); // 声明一个画笔 private paint paint; private displaymetrics dm; // 获取屏幕分辨率的类 // private scroller mscroller; private boolean isfirst = true; // 点击事件星星相关 int mdownx = 0; int mdowny = 0; int mtempx = 0; int mtempy = 0; private static final int max_distance_for_click = 100; private scrollviewlistener listener; private int screenheight; private int screenwidth; public myscrollview(context context, attributeset attrs) { super(context, attrs); mcontext = context; // 背景加载资源 treebackground = bitmapfactory.decoderesource(res, r.drawable.tree); cloud_bg = bitmapfactory.decoderesource(res, r.drawable.cloud_bg); starsbg = bitmapfactory.decoderesource(res, r.drawable.stars_big); starsbg_n = bitmapfactory.decoderesource(res, r.drawable.startbg_n); this.bmpbackground1 = treebackground; this.bmpbackground2 = treebackground; bg1x = 0; bg2x = 0; cloudx = 0; pointsy = new int[22];// y轴 pointsx = new int[22];// x轴 screenheight = displayutils.getscreenheight(context); screenwidth = displayutils.getscreenwidth(context); initstartsxy(); inittextcontent(); // mscroller = new scroller(context); // logutils.d("屏幕 分别率 高 = " + displayutils.getscreenheight(context) + // " 屏幕 分别率 宽度 = " + displayutils.getscreenwidth(context)); // logutils.d("屏幕 分别率 getdisplaydensity = " + // displayutils.getdisplaydensity(context)); } public void setmaplist(list list) { maplist = list; } // //调用此方法滚动到目标位置 // public void smoothscrollto(int fx, int fy) { // int dx = fx - mscroller.getfinalx(); // int dy = fy - mscroller.getfinaly(); // smoothscrollby(dx, dy); // } // // //调用此方法设置滚动的相对偏移 // public void smoothscrollby(int dx, int dy) { // // //设置mscroller的滚动偏移量 // mscroller.startscroll(mscroller.getfinalx(), mscroller.getfinaly(), dx, // dy); // invalidate();//这里必须调用invalidate()才能保证computescroll()会被调用,否则不一定会刷新界面,看不到滚动效果 // } // // @override // public void computescroll() { // //先判断mscroller滚动是否完成 // if (mscroller.computescrolloffset()) { // // //这里调用view的scrollto()完成实际的滚动 // scrollto(mscroller.getcurrx(), mscroller.getcurry()); // // //必须调用该方法,否则不一定能看到滚动效果 // postinvalidate(); // } // super.computescroll(); // } private void inittextcontent() { for (int i = 0; i < maxstars; i++) { textcontnt[i] = i + 1; } } public void setonclick(scrollviewlistener listener) { this.listener = listener; } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); // 首先让第一张背景底部正好填满整个屏幕 int treeheight = bmpbackground1.getheight() - getheight(); // logutils.d("bg1y = " + bg1y + " bmpbackground1.getheight() = " + // bmpbackground1.getheight() // + " getheight() = " + getheight()); if (treeheight > 0) {// 图高大于屏幕高度 // 第二张背景图紧接在第一张背景的上方 // 虽然两张背景图无缝隙连接但是因为图片资源头尾 // 直接连接不和谐,为了让视觉看不出是两张图连接而修正的位置 bg1y = -(bmpbackground1.getheight() - getheight()); bg2y = bg1y - bmpbackground1.getheight();// 这里可以+100 // 可以控制两张图之间的距离修正位置 } else { bg1y = math.abs(bmpbackground1.getheight() - getheight()); bg2y = bg1y - bmpbackground1.getheight();// 这里可以+100 // 可以控制两张图之间的距离修正位置 } // logutils.d("cloudy = " + cloudy + " bg1y = " + bg1y + " bg2y = " + // bg2y + " screenh = " + getheight()); // 实例一个画笔 paint = new paint(); paint.setantialias(true);// 设置没有锯齿 // 设置画笔颜色为白色 paint.setcolor(color.white); } @override protected void ondraw(canvas canvas) { // logutils.d("屏幕 分别率 h = " + getheight()); // logutils.d("屏幕 分别率 w = " + getwidth()); // 画云背景 canvas.drawbitmap(resizebitmap(cloud_bg, getwidth(), getheight()), cloudx, cloudy, paint); // drawimage(canvas, cloud_bg, cloudx, cloudy, cloud_bg.getwidth(), // cloud_bg.getheight(), cloudx, cloudy, paint); // 绘制两张背景 if (isfirst) { bmpbackground1 = resizebitmap(bmpbackground1, getwidth(), getheight()); isfirst = false; initbgview(); } canvas.drawbitmap(bmpbackground1, bg1x, bg1y, paint); canvas.drawbitmap(bmpbackground1, bg2x, bg2y, paint); // logutils.d("bmpbackground1.getheight() = " + // bmpbackground1.getheight() + " screenh = " + getheight() + // " bg1y = " + bg1y); // logutils.d("bg1x = " + bg1x + " bg1y = " + bg1y + " bg2x = " + bg2x // + " bg2y = " + bg2y + " screenh = " + getheight()); starsdraw(canvas);// 绘制星星 textviewdraw(canvas);// 绘制文本 super.ondraw(canvas); } @override protected void onscrollchanged(int l, int t, int oldl, int oldt) { boolean isdown; // speed = t - oldt; // // //判断向上移动还是向下移动 // if (speed > 0) { // isdown = true; // } else { // isdown = false; // } // logic(isdown);//计算点的距离 super.onscrollchanged(l, t, oldl, oldt); } public bitmap resizebitmap(bitmap bitmap, int w, int h) { if (bitmap != null) { int width = bitmap.getwidth(); int height = bitmap.getheight(); int newwidth = w; int newheight = h; float scalewight = ((float) newwidth) / width; float scaleheight = ((float) newheight) / height; matrix matrix = new matrix(); matrix.postscale(scalewight, scaleheight); bitmap res = bitmap.createbitmap(bitmap, 0, 0, width, height, matrix, true);// oom问题 待解决 return res; } else { return null; } } // 画文字 private void textviewdraw(canvas canvas) { // 判断当前画的是第几个 int c = activitynumber * 11; int k = 0; if (activitynumber > 2) { k = c - ((activitynumber - 1) * 10); } // logutils.d("比例 x 28 = " + (double) 28 / screenwidth); // logutils.d("比例 y 65 = " + (double) 65 / screenheight); // logutils.d("比例 x 40 = " + (double) 40 / screenwidth); for (int i = 0; i < 22; i++) { paint.settextsize((int) (0.037037037037037035 * screenwidth));// 设置字体大小 // paint.settypeface(typeface);//设置字体类型 // typeface.default:默认字体。 // typeface.default_bold:加粗字体。 // typeface.monospace:monospace字体。 // typeface.sans_serif:sans字体。 // typeface.serif:serif字体。 paint.setcolor(color.blue); // 每页第几个 我推算出来的公式 (x-1)*11+i 这是普通的推算 具体问题 需要具体改变 // settextcolorusereflection(color.blue); paint.setstrokewidth(2); // 描边宽度 paint.setfakeboldtext(true); // 外层text采用粗体 if (activitynumber <= 2) { if (textcontnt[i] > 9) { paint.setcolor(res.getcolor(r.color.text_color)); canvas.drawtext( string.valueof(textcontnt[i]), pointsx[i] + (int) (0.025925925925925925 * screenwidth), pointsy[i] + (int) (0.033854166666666664 * screenheight), paint); } else { paint.setcolor(res.getcolor(r.color.text_color)); canvas.drawtext( string.valueof(textcontnt[i]), pointsx[i] + (int) (0.037037037037037035 * screenwidth), pointsy[i] + (int) (0.033854166666666664 * screenheight), paint); } } else { // 下面的都是从在这里做操作对text 数值 // int u = (activitynumber - 2) * 11 + i-11; // 必须分前后 前面的 是一队 后面的是一队 if (activitynumber % 2 != 0) {// 第三个开始 奇数 是第二个页面在下面 第一个页面在上 // 判断上下的 作用 int befo;// (n-1)*11+i int after; if (i < 11) {// 数组前面的 befo = (activitynumber - 1) * 11 + i; if (befo < maxstars) { if (textcontnt[befo] > 9) { paint.setcolor(res.getcolor(r.color.text_color)); canvas.drawtext( string.valueof(textcontnt[befo]), pointsx[i] + (int) (0.025925925925925925 * screenwidth), pointsy[i] + (int) (0.033854166666666664 * screenheight), paint); } else { paint.setcolor(res.getcolor(r.color.text_color)); canvas.drawtext( string.valueof(textcontnt[befo]), pointsx[i] + (int) (0.037037037037037035 * screenwidth), pointsy[i] + (int) (0.033854166666666664 * screenheight), paint); } } } else {// 数组后面的 after = (activitynumber - 3) * 11 + i; if (after < maxstars) { if (textcontnt[after] > 9) { paint.setcolor(res.getcolor(r.color.text_color)); canvas.drawtext( string.valueof(textcontnt[after]), pointsx[i] + (int) (0.025925925925925925 * screenwidth), pointsy[i] + (int) (0.033854166666666664 * screenheight), paint); } else { paint.setcolor(res.getcolor(r.color.text_color)); canvas.drawtext( string.valueof(textcontnt[after]), pointsx[i] + (int) (0.037037037037037035 * screenwidth), pointsy[i] + (int) (0.033854166666666664 * screenheight), paint); } } } } else { int befo;// (n-1)*11+i int after; if (i < 11) {// 数组前面的 befo = (activitynumber - 2) * 11 + i; if (befo < maxstars) { if (textcontnt[befo] > 9) { paint.setcolor(res.getcolor(r.color.text_color)); canvas.drawtext( string.valueof(textcontnt[befo]), pointsx[i] + (int) (0.025925925925925925 * screenwidth), pointsy[i] + (int) (0.033854166666666664 * screenheight), paint); } else { paint.setcolor(res.getcolor(r.color.text_color)); canvas.drawtext( string.valueof(textcontnt[befo]), pointsx[i] + (int) (0.037037037037037035 * screenwidth), pointsy[i] + (int) (0.033854166666666664 * screenheight), paint); } } } else {// 数组后面的 after = (activitynumber - 2) * 11 + i; if (after < maxstars) { if (textcontnt[after] > 9) { paint.setcolor(res.getcolor(r.color.text_color)); canvas.drawtext( string.valueof(textcontnt[after]), pointsx[i] + (int) (0.025925925925925925 * screenwidth), pointsy[i] + (int) (0.033854166666666664 * screenheight), paint); } else { paint.setcolor(res.getcolor(r.color.text_color)); canvas.drawtext( string.valueof(textcontnt[after]), pointsx[i] + (int) (0.037037037037037035 * screenwidth), pointsy[i] + (int) (0.033854166666666664 * screenheight), paint); } } } } } // logutils.d("pointsy [" + i + "]= " + pointsy[i]); } paint.setcolor(color.white); } // 获取当前页数的开始数量 private int getcurrent(int i) { int befo; // 判断上下 if (activitynumber % 2 != 0) {// 奇数 第一页在上 第二页在下 befo = (activitynumber - 1) * 11 + i; // logutils.d("befo = " + befo + " activitynumber = " + // activitynumber + " i = " + i); } else { befo = (activitynumber - 2) * 11 + i; } return befo; } // 获取点击的位置的数字 private int getonclickno(int i) { int befo;// (n-1)*11+i int after; // 判断上下 if (activitynumber % 2 != 0) {// 奇数 第一页在上 第二页在下 if (i < 11) { befo = (activitynumber - 1) * 11 + i; // logutils.d("i 小于11 befo = " + befo + " activitynumber = " // + activitynumber + " i = " + i); } else { befo = (activitynumber - 3) * 11 + i; // logutils.d("i 大于11 befo = " + befo + " activitynumber = " // + activitynumber + " i = " + i); } } else { if (i < 11) { befo = (activitynumber - 2) * 11 + i; // logutils.d(" 偶数的时候 befo = " + befo + " activitynumber = " // + activitynumber + " i = " + i); } else { befo = (activitynumber - 2) * 11 + i; } } return befo; } private void starsdraw(canvas canvas) { // 判断画一页当中 最多的星星 是多少 int starsmax = (activitynumber) * 11; int page; if (starsmax > maxstars) {// 判断当前的最多的星星是否大于定好的星星数量 if (maplist == null) { for (int i = 0; i < 22; i++) { // 判断当前画的是第几个 page = getcurrent(i); // logutils.d("当前画的是第几个 = " + page); canvas.drawbitmap(starsbg_n, pointsx[i], pointsy[i], paint); } } else { for (int i = 0; i < 22; i++) { // 判断当前画的是第几个 page = getcurrent(i); // logutils.d("当前的 最前面的是多少 current = " + page); if (page < maxstars) { if (page < i) { map<string, object> stringobjectmap = maplist .get(page); string type = (string) stringobjectmap.get("type"); if (type.equals("1")) { canvas.drawbitmap(starsbg, pointsx[i], pointsy[i], paint); } else { canvas.drawbitmap(starsbg_n, pointsx[i], pointsy[i], paint); } } else { canvas.drawbitmap(starsbg_n, pointsx[i], pointsy[i], paint); } } // logutils.d("pointsy [" + i + "]= " + pointsy[i]); } } } else { if (maplist == null) { for (int i = 0; i < 22; i++) { // 判断当前画的是第几个 page = getcurrent(i); // logutils.d("当前画的是第几个 = " + page); canvas.drawbitmap(starsbg_n, pointsx[i], pointsy[i], paint); } } else { int size = maplist.size(); for (int i = 0; i < 22; i++) { // 判断当前画的是第几个 page = getcurrent(i); // logutils.d("当前画的是第几个 = " + page); if (page < size) { map<string, object> stringobjectmap = maplist.get(page); string type = (string) stringobjectmap.get("type"); if (type.equals("1")) { canvas.drawbitmap(starsbg, pointsx[i], pointsy[i], paint); } else { canvas.drawbitmap(starsbg_n, pointsx[i], pointsy[i], paint); } } else { canvas.drawbitmap(starsbg_n, pointsx[i], pointsy[i], paint); } } } } } private void canvasstats() { } private void initstartsxy() { stars0x = 340;// stars0y = 1180;// stars1x = 280; stars1y = 1065; stars2x = 500; stars2y = 967; stars3x = 240; stars3y = 842; stars4x = 400; stars4y = 761; stars5x = 540; stars5y = 685; stars6x = 330; stars6y = 526; stars7x = 540; stars7y = 431; stars8x = 375; stars8y = 245; stars9x = 550; stars9y = 113; stars10x = 310; stars10y = 57; speed = 0; pointsx[0] = (int) (screenwidth * 0.39351851851851855); pointsx[1] = (int) (screenwidth * 0.25925925925925924); pointsx[2] = (int) (screenwidth * 0.46296296296296297); pointsx[3] = (int) (screenwidth * 0.2222222222222222); pointsx[4] = (int) (screenwidth * 0.37037037037037035); pointsx[5] = (int) (screenwidth * 0.5); pointsx[6] = (int) (screenwidth * 0.3055555555555556); pointsx[7] = (int) (screenwidth * 0.5); pointsx[8] = (int) (screenwidth * 0.3472222222222222); pointsx[9] = (int) (screenwidth * 0.5092592592592593); pointsx[10] = (int) (screenwidth * 0.28703703703703703); // pointsx[0] = stars0x; // pointsx[1] = stars1x; // pointsx[2] = stars2x; // pointsx[3] = stars3x; // pointsx[4] = stars4x; // pointsx[5] = stars5x; // pointsx[6] = stars6x; // pointsx[7] = stars7x; // pointsx[8] = stars8x; // pointsx[9] = stars9x; // pointsx[10] = stars10x; pointsx[11] = pointsx[0]; pointsx[12] = pointsx[1]; pointsx[13] = pointsx[2]; pointsx[14] = pointsx[3]; pointsx[15] = pointsx[4]; pointsx[16] = pointsx[5]; pointsx[17] = pointsx[6]; pointsx[18] = pointsx[7]; pointsx[19] = pointsx[8]; pointsx[20] = pointsx[9]; pointsx[21] = pointsx[10]; pointsy[0] = stars0y; pointsy[1] = stars1y; pointsy[2] = stars2y; pointsy[3] = stars3y; pointsy[4] = stars4y; pointsy[5] = stars5y; pointsy[6] = stars6y; pointsy[7] = stars7y; pointsy[8] = stars8y; pointsy[9] = stars9y; pointsy[10] = stars10y; // pointsy[0] = (int) (screenheight * 0.6145833333333334); // pointsy[1] = (int) (screenheight * 0.5546875); // pointsy[2] = (int) (screenheight * 0.5036458333333333); // pointsy[3] = (int) (screenheight * 0.43854166666666666); // pointsy[4] = (int) (screenheight * 0.3963541666666667); // pointsy[5] = (int) (screenheight * 0.3411458333333333); // pointsy[6] = (int) (screenheight * 0.27395833333333336); // pointsy[7] = (int) (screenheight * 0.22447916666666667); // pointsy[8] = (int) (screenheight * 0.12760416666666666); // pointsy[9] = (int) (screenheight * 0.058854166666666666); // pointsy[10] = (int) (screenheight * 0.0296875); pointsy[11] = pointsy[0] - (bmpbackground1.getheight()); pointsy[12] = pointsy[1] - (bmpbackground1.getheight()); pointsy[13] = pointsy[2] - (bmpbackground1.getheight()); pointsy[14] = pointsy[3] - (bmpbackground1.getheight()); pointsy[15] = pointsy[4] - (bmpbackground1.getheight()); pointsy[16] = pointsy[5] - (bmpbackground1.getheight()); pointsy[17] = pointsy[6] - (bmpbackground1.getheight()); pointsy[18] = pointsy[7] - (bmpbackground1.getheight()); pointsy[19] = pointsy[8] - (bmpbackground1.getheight()); pointsy[20] = pointsy[9] - (bmpbackground1.getheight()); pointsy[21] = pointsy[10] - (bmpbackground1.getheight()); for (int i = 0; i < 11; i++) { double x = (double) pointsx[i] / screenwidth; decimalformat df = new decimalformat("0.00");// 格式化小数,.后跟几个零代表几位小数 logutils.d("比例 i " + i + " x = " + x); } for (int i = 0; i < 11; i++) { double y = (double) pointsy[i] / screenheight; decimalformat df = new decimalformat("0.00");// 格式化小数,.后跟几个零代表几位小数 logutils.d("比例 i " + i + " y = " + y); } // float y = (float) 10 / screenheight; // float x = (float) 10 / screenwidth; // logutils.d("比例 i 10 x = " + x + "比例 i 10 y = " + // y); } private void speedstarsxy() { for (int i = 0; i < 22; i++) { pointsy[i] += speed; } } private void setstartsxydown(int type) { // logutils.d("pointy 0 之前= " + pointsy[0]); // logutils.d("pointy 11之前 = " + pointsy[11]); if (type == 0) { for (int i = 0; i < 11; i++) { int p = pointsy[i + 11]; pointsy[i] = p - (bmpbackground1.getheight()); // logutils.d("改变 后的 pointsy[" + i + "] = " + pointsy[i]); } } else { for (int i = 0; i < 11; i++) { // logutils.d("向下的第二种 pointsy[i+ 11] " + (pointsy[i] - // (bmpbackground1.getheight()))); int p = pointsy[i]; pointsy[i + 11] = p - (bmpbackground1.getheight()); // logutils.d("改变 后的 向下的第二种 pointsy[" + i + "] = " + // pointsy[i]); } } } private void setstartsxyup(int type) { if (type == 0) { for (int i = 0; i < 11; i++) { pointsy[i] = pointsy[i + 11] + (bmpbackground1.getheight()); } } else { for (int i = 0; i < 11; i++) { pointsy[i + 11] = pointsy[i] + (bmpbackground1.getheight()); } } } @override public boolean ontouchevent(motionevent event) { // 获取当前触控位置 // logutils.d("触摸事件 event.getaction() = " + event.getaction()); int x = (int) event.getx(); int y = (int) event.gety(); boolean isdown; switch (event.getaction()) { case motionevent.action_down:// 当用户是按下 pointx = x; pointy = y; mdownx = (int) event.getx(); mdowny = (int) event.gety(); logutils.d(" 按下的 点 x = " + pointx + " y = " + pointy); // logutils.d(" 按下的 点的比例 x = " + (double) pointx / screenwidth + // " y = " + (double) pointy / screenheight); break; case motionevent.action_move:// 移动 // logutils.d("init movey " + movey); tempy1 = movey; movey = (int) event.gety(); speed = movey - tempy1; // 判断向上移动还是向下移动 if (speed > 0) { isdown = true; } else { isdown = false; } logic(isdown);// 计算点的距离 // logutils.d("pointy = " + pointy + " movey = " + movey + " speed " // + (speed)); break; case motionevent.action_up: movey = 0; mtempx = (int) event.getx(); mtempy = (int) event.gety(); if (math.abs(mtempx - mdownx) > max_distance_for_click || math.abs(mtempy - mdowny) > max_distance_for_click) { // 抬起的距离 和按下的距离太远 不形成点击事件 // logutils.d("起的距离 和按下的距离太远 不形成点击事件"); } else { isonclick(mtempx, mtempy); } break; } invalidate();// 刷新界面 // 使系统响应事件,返回true return true; } // 添加自定义点击事件 private boolean isonclick(int x, int y) { logutils.d("点击了"); if (listener != null) { // 判断是否点击到星星上面 return isstarsonclick(x, y, listener); } else { return false; } } private boolean isstarsonclick(int x, int y, scrollviewlistener listener) { int x1; int y1; int x2; int y2; // logutils.d("点击 x = " + x + " 点击 y = " + y + " 星星 x = " + pointsx[0] // + " 星星 y = " + pointsy[0]); for (int i = 0; i < 22; i++) { x1 = pointsx[i]; y1 = pointsy[i]; // logutils.d("比例 y 100 = " + (double) 100 / screenheight); // logutils.d("比例 x 100 = " + (double) 100 / screenwidth); x2 = pointsx[i] + (int) (screenwidth * 0.09259259259259259); y2 = pointsy[i] + (int) (screenheight * 0.052083333333333336); if (x > x1 && x < x2 && y > y1 && y < y2) { // logutils.d("点击到了"); int current = getonclickno(i) + 1; listener.myonclick(current); // logutils.d("点击的第几位 : " + current); return true; } } // logutils.d("没有点击到"); return false; } // 判断是否到顶部了 private void istop() { if (activitynumber >= 8 && bg1y > getheight()) { // logutils.d("当第1张图片的y坐标超出屏幕, 图片向上的情况 activitynumber 加上的 =======到顶部= bg1y" // + activitynumber); bg1y = getheight(); bg2y = bg1y - bmpbackground1.getheight(); speed = 0; speedstarsxy(); return; } else { speedstarsxy(); } } // 背景滚动的逻辑函数 public void logic(boolean isdown) { if (isdown) {// 手指向下 if (tempy1 != 0) { bg1y += speed; bg2y += speed; // logutils.d("istop = " + istop()); istop(); } // //判断是否到顶部了 // if (activitynumber >= 8 && bg1y > getheight()) { // //logutils.d("当第1张图片的y坐标超出屏幕, 图片向上的情况 activitynumber 加上的 =======到顶部= bg1y" // + activitynumber); // bg1y = getheight(); // bg2y = bg1y - bmpbackground1.getheight(); // return; // } // logutils.d("bg1y = " + bg1y + " bg2y = " + bg2y + // " bg1y - bmpbackground1.getheight() = " + (bg1y - // bmpbackground1.getheight()) + // " -math.abs(bmpbackground1.getheight() - getheight()) = " + // (-math.abs(bmpbackground1.getheight() - getheight()))); // 当第一张图片的y坐标超出屏幕, 手指向下的情况 // 立即将其坐标设置到第二张图的上方 if (bg1y > getheight()) { bg1y = bg2y - bmpbackground1.getheight(); activitynumber += 1; // logutils.d("bg1y = " + bg1y + " getheight = " + getheight() + // " bmpbackground1.getheight() = " + // bmpbackground1.getheight() + " activitynumber 加上的 = " + // activitynumber); setstartsxydown(0); // logutils.d("当第1张图片的y坐标超出屏幕, 图片向上的情况 activitynumber 加上的 = " // + activitynumber); } // 当第二张图片的y坐标超出屏幕,向下的情况 // 立即将其坐标设置到第一张图的上方 if (bg2y > getheight()) { bg2y = bg1y - bmpbackground1.getheight(); activitynumber += 1; // logutils.d("当第1张图片的y坐标超出屏幕, 图片向上的情况 activitynumber 加上的 = " // + activitynumber); // logutils.d("bg1y = " + bg1y + " getheight = " + getheight() + // " bmpbackground1.getheight() = " + // bmpbackground1.getheight() + " activitynumber 加上的 = " + // activitynumber); setstartsxydown(1); } } else { // 当第一张图片的y坐标超出屏幕, 手指向上的情况 // 立即将其坐标设置到第二张图的下方 if (tempy1 != 0) { bg1y += speed; bg2y += speed; // //判断是否到底部 isbottom(); } // logutils.d("bg1y = " + bg2y + " bg2y = " + bg2y + // " bg1y - bmpbackground1.getheight() = " + (bg1y - // bmpbackground1.getheight()) + // " -math.abs(bmpbackground1.getheight() - getheight()) = " + // (-math.abs(bmpbackground1.getheight() - getheight()))); if (bg1y < -math.abs(bmpbackground1.getheight() - getheight())) { if (bg2y < bg1y) { activitynumber -= 1; setstartsxyup(1); // logutils.d("当第1张图片的y坐标超出屏幕, 图片向下的情况 activitynumber 减去的 = " // + activitynumber); } bg2y = bg1y + bmpbackground1.getheight();// 换屏了 首尾相接 } if (bg2y < -math.abs(bmpbackground1.getheight() - getheight())) { if (bg1y < bg2y) {// 当换图的时候 给减一 activitynumber -= 1; setstartsxyup(0); // logutils.d("当第二张图片的y坐标超出屏幕, 图片向下的情况 activitynumber 减去的 = " // + activitynumber); } bg1y = bg2y + bmpbackground1.getheight(); } } } private void isbottom() { int treeheight = bmpbackground1.getheight() - getheight(); if (treeheight < 0) { if (activitynumber <= 2 && bg1y < math .abs(bmpbackground1.getheight() - getheight())) {// 如果是一开始的 // 到底部时 // 不能滑动 // 并且重置 // 坐标点 activitynumber = 2; bg1y = math.abs(bmpbackground1.getheight() - getheight()); // 第二张背景图紧接在第一张背景的上方 // 直接连接不和谐,为了让视觉看不出是两张图连接而修正的位置 bg2y = bg1y - bmpbackground1.getheight(); initstartsxy(); return; } else { speedstarsxy(); // logutils.d("pointy speedstarsxy 11之后 = " + pointsy[11]); } } else { if (activitynumber <= 2 && bg1y < -math.abs(bmpbackground1.getheight() - getheight())) {// 如果是一开始的 到底部时 不能滑动 并且重置 坐标点 activitynumber = 2; bg1y = -math.abs(bmpbackground1.getheight() - getheight()); // 第二张背景图紧接在第一张背景的上方 // +101的原因:虽然两张背景图无缝隙连接但是因为图片资源头尾 // 直接连接不和谐,为了让视觉看不出是两张图连接而修正的位置 bg2y = bg1y - bmpbackground1.getheight(); initstartsxy(); return; } else { speedstarsxy(); // logutils.d("pointy speedstarsxy 11之后 = " + pointsy[11]); } } } private void initbgview() { int treeheight = bmpbackground1.getheight() - getheight(); if (treeheight < 0) { activitynumber = 2; bg1y = math.abs(bmpbackground1.getheight() - getheight()); // 第二张背景图紧接在第一张背景的上方 // 直接连接不和谐,为了让视觉看不出是两张图连接而修正的位置 bg2y = bg1y - bmpbackground1.getheight(); initstartsxy(); return; } else { activitynumber = 2; bg1y = -math.abs(bmpbackground1.getheight() - getheight()); // 第二张背景图紧接在第一张背景的上方 bg2y = bg1y - bmpbackground1.getheight(); initstartsxy(); return; } } }
源码下载:android仿开心消消乐游戏
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: java 创建线程的方法总结