开发者

Put together tiles in android sdk and use as background

开发者 https://www.devze.com 2022-12-25 09:38 出处:网络
In a feeble attempt to learn some Android development am I stuck at graphics. My aim here is pretty simple:

In a feeble attempt to learn some Android development am I stuck at graphics. My aim here is pretty simple:

  1. Take n small images and build a random image, larger than the screen with possib开发者_如何学Pythonility to scroll around.
  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. 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
  2. Make a large Drawable and draw the tiles on it
  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

The problem is the hands on things. I load the small images with "Bitmap img1 = BitmapFactory.decodeResource (res, R.drawable.img1)", but then what? Should I make a canvas and draw the images on it with "canvas.drawBitmap (img1, x, y, null);"? If so how to get a Drawable/Bitmap from that?

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). 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:

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. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号