将一些间隔图像水平添加到画布中的正确方法是什么? [英] What is the correct way to horizontally add some spaced image into a Canvas?

查看:25
本文介绍了将一些间隔图像水平添加到画布中的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Android 开发完全陌生,我有以下疑问.

I am absolutly new in Android development and I have the followind doubt.

我必须将一个相邻的图像绘制成一个画布对象.

I have to draw images one next to each other into a Canvas object.

举个例子:我有这个图标(它非常大,我必须调整它的大小):

So let to an example: I have this icon (it is pretty huge and I have to resize it):

所以我必须将其中的 3 个图标并排放置(在图像和下一个图像之间添加一些空白).

So I have to put 3 of these icon one next to each other (adding some white space between an image and the next one).

所以我做了这样的事情:

So I have done something like this:

// Load the 2 images for the creation of the "difficulty graphic":
Bitmap chefHatOk = BitmapFactory.decodeResource(getResources(), R.drawable.chef_hat_ok);

// Where the previus image will be drawn:
Canvas canvas = new Canvas();

所以我认为我可以将上一张图片添加到画布,这样做:

So I think that I can add the previous image to the Canvas doing something like this:

canvas.drawBitmap(smallImage, 0f, 0f, null); 

我认为第一个 0f 值代表插入图像之前的水平空间(偏移量),如果我做错了断言,请纠正我.

I think that the first 0f value represent the horizontal space before the inserted image (the offset), correct me if I am doing wrong assertion.

那么,如何将其中 3 张图像并排添加,在图像和下一张图像之间留出一些空白?

So, how can I add 3 of these images one next to each other leaving some white space between an image and the next one?

推荐答案

这样的事情应该可行:

Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);

int space = 10; // the space between images

for(int i = 0; i < 3; i++) {
    canvas.drawBitmap(smallImage, i * (smallImage.getWidth() + space), 0, null);
}

// do whatever you want with output

这篇关于将一些间隔图像水平添加到画布中的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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