安卓:绘制多个位图上的另一个位图 [英] Android: Draw multiple bitmaps on another bitmap

查看:137
本文介绍了安卓:绘制多个位图上的另一个位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从多个较小的位图试图复合背景位图。我使用的Eclipse的Andr​​oid SDK。

I am trying to composite a background bitmap from multiple smaller bitmaps. I am using Eclipse with the Android SDK.

我已经加载了(大部分)空白背景位图(像素尺寸小320x480)像这样:

I have loaded a (mostly) blank background bitmap (pixel dimensions are 320x480) like so:

Bitmap mBackground = BitmapFactory.decodeResource( res, R.drawable.background );

然后我打开一个位图砖(128×128)以同样的方式。

Then I load a bitmap 'tile' (128x128) the same way.

我曾尝试画在瓷砖上的位图,但是每一个方法是行不通的,我想的方式多种方式/期望。我要绘制瓦片位图缩小到64×64,并在一个特定的位置(象素偏移)上的背景的位图。

I have tried multiple ways of drawing the tile onto the bitmap but every single method does not work the way I would like/expect. I want to draw the tile bitmap scaled down to 64x64 and at a specific location (pixel offset) on the background bitmap.

1)用帆布...

Canvas c = new Canvas( mBackground );
c.drawBitmap( mTile, null, new Rect( 10, 10, 73, 73 ), null );

这吸引了瓷砖太大了(没有原图,不是64×64,但在两者之间,约66%的原某处)

This draws the tile way too big (not fullsize, not 64x64, but somewhere in between, about 66% of original)

2)使用可绘制(基本上似乎是相同的):

2) Using drawables (which basically seems to be the same):

Canvas c = new Canvas( mBackground );
c.translate( 10, 10 );
d.setBounds( 0, 0, 63, 63 );
d.draw( c );

相同的结果,实验#1 ...

Same result as experiment #1...

在文档中的很多地方提到(无简要说明任何地方),这必须与设备无关'密度'因此,我试了下方法...

The documentation mentions in numerous places (without a concise explanation anywhere) that this has to do with the device independent 'densities' thus I tried the next approach...

3)使用固定比例的资源。我创建了/ RES /绘制-nodpi(我也尝试过/ RES /绘制)文件夹,并动了我的资源(背景和瓷砖PNG图片)到它。这会导致产生的背景位图的画布时,因为未缩放的位图显然变得不可变的,因此不能被分配​​到一个Canvas例外!

3) Using fixed scale resources. I created the /res/drawable-nodpi (I also tried just /res/drawable) folder and moved my resources (background and tile png images) into it. That causes an exception when creating the Canvas from the background Bitmap because an unscaled bitmap apparently becomes immutable and therefore cannot be assigned to a Canvas!

4)我试着用BitmapFactory.Options和设置inScaled为false,并通过这些选项,两者的日codeResource呼吁的背景和瓷砖。这有完全相同的效果#3(抛出异常)。

4) I tried using BitmapFactory.Options and setting inScaled to false, and passing those options to both of the decodeResource calls for the background and the tile. This has the exact same effect as #3 (exception thrown).

没有奏效它变得非常沮丧。我相信,我只是缺少一些细节所特有的位图,当涉及到不同的坐标空间,但我找不到它的任何地方的文件中或其他地方(我试过多个Android开发网站无济于事)。

Nothing has worked and its becoming quite frustrating. I am sure I am just missing some detail that is peculiar to bitmaps when it comes to the different coordinate spaces but I cannot find it anywhere in the documentation or elsewhere (I've tried multiple android development sites to no avail).

希望有人谁做这个会看到这样的恳求! 谢谢,杰森

Hopefully someone who has done this will see this plea! Thanks, jason

推荐答案

Canvas.drawBitmap()不执行缩放AFAIK。

Canvas.drawBitmap() does not perform scaling afaik.

由于您的平铺图像原本是128×128,你想在64×64画的,最简单的窍门是将来电德codeResource()的选项,以减少它的大小:

Since your tile image is originally 128x128 and you want to draw it at 64x64, the simplest trick would be to call decodeResource() with the option to reduce it in size:

BitmapFactory.Options options = new BitmapFactory.Options()
options.inSampleSize = 2; // downscale by 50%
Bitmap mTile = BitmapFactory.decodeResource( res, R.drawable.tile, options );

要做到更强大的比例(如绘制图像在任何规模的),你应该把它包在一个的 BitmapDrawable

To do more powerful scaling (e.g. drawing your image at any size) you should wrap it in a BitmapDrawable.

这篇关于安卓:绘制多个位图上的另一个位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆