简体   繁体   English

将图块放到android sdk中并用作背景

[英]Put together tiles in android sdk and use as background

In a feeble attempt to learn some Android development am I stuck at graphics. 为了学习一些Android开发,我做得很乏味,但我坚持使用图形技术。 My aim here is pretty simple: 我的目标很简单:

  1. Take n small images and build a random image, larger than the screen with possibility to scroll around. 拍摄n张小图像并构建一个随机的图像,该图像大于屏幕并可能滚动。
  2. Have an animated object move around on it 让动画对象在其上四处移动

I have looked at the SDK examples, Lunar Lander especially but there are a few things I utterly fail to wrap my head around. 我查看了SDK示例,尤其是Lunar Lander,但有些事情我完全没能确定。 I've got a birds view plan (which in my head seems reasonably sane): 我有一个鸟瞰图的计划(在我看来,这很合理):

How do I merge the tiles into one large image? 如何将图块合并为一个大图像? The background is static so I figure I should do like this: 背景是静态的,所以我认为我应该这样做:

  1. Make a 2d array with refs to the tiles 制作2D阵列,并参照瓷砖
  2. Make a large Drawable and draw the tiles on it 制作一个大型Drawable并在其上绘制瓷砖
  3. At init draw this big image as the background 在初始化时,将此大图像绘制为背景
  4. At each onDraw redraw the background of the previous spot of the moving object, and the moving object at its new location 在每个onDraw上重绘运动对象上一个斑点的背景,并将运动对象放置在其新位置

The problem is the hands on things. 问题出在事情上。 I load the small images with "Bitmap img1 = BitmapFactory.decodeResource (res, R.drawable.img1)", but then what? 我使用“位图img1 = BitmapFactory.decodeResource(res,R.drawable.img1)”加载小图像,但是那又是什么呢? Should I make a canvas and draw the images on it with "canvas.drawBitmap (img1, x, y, null);"? 我是否应该制作画布并使用“ canvas.drawBitmap(img1,x,y,null);”在其上绘制图像? If so how to get a Drawable/Bitmap from that? 如果是这样,如何从中获取Drawable / Bitmap?

I'm totally lost here, and would really appreciate some hands on help (I would of course be grateful for general hints as well, but I'm primarily trying to understand the Graphics objects). 我在这里完全迷路了,非常感谢您的帮助(我当然也会感谢一般提示,但是我主要是想了解Graphics对象)。 To make you, dear reader, see my level of confusion will I add my last desperate try: 亲爱的读者,为了让您满意,请看我的困惑程度,我将最后一次拼死的尝试加进去:

Drawable drawable;
Canvas canvas = new Canvas ();
Bitmap img1 = BitmapFactory.decodeResource (res, R.drawable.img1); // 50 x 100 px image
Bitmap img2 = BitmapFactory.decodeResource (res, R.drawable.img2); // 50 x 100 px image
canvas.drawBitmap (img1, 0, 0, null);
canvas.drawBitmap (img2, 50, 0, null);

drawable.draw (canvas); // obviously wrong as draw == null
this.setBackground (drawable); 

Thanks in advance 提前致谢

I finally figured it out, what I need for the basic problem of tiling a bunch of Bitmaps together was a BitmapDrawable: 我终于弄清楚了,对于将一堆位图拼接在一起的基本问题,我需要一个BitmapDrawable:

Bitmap myImage = Bitmap.createBitmap(450, 300, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(myImage);
Bitmap img1 = BitmapFactory.decodeResource (res, R.drawable.img1); // 50 x 100 px image
Bitmap img2 = BitmapFactory.decodeResource (res, R.drawable.img2); // 50 x 100 px image
canvas.drawBitmap (img1, 0, 0, null);
canvas.drawBitmap (img2, 50, 0, null);
this.setBackgroundDrawable(new BitmapDrawable(myImage)); 

I obviously had it backwards with the relationship between Canvas and Bitmap, I thought Canvas was a blank screen on which to paint on. 我显然对Canvas和Bitmap之间的关系不满意,我认为Canvas是一个可在其上绘画的空白屏幕。 Apparently (if I got it right this time) it's connected to a Bitmap, and if having that one it's possible to use it. 显然(如果这次我做对了),它已连接到位图,并且如果有该位图,则可以使用它。

I still wouldn't know how to get it in the onDraw(Canvas) function though, but this solves the problem I had. 我仍然不知道如何在onDraw(Canvas)函数中获取它,但这解决了我遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM