繁体   English   中英

将一些间隔图像水平添加到画布中的正确方法是什么?

[英]What is the correct way to horizontally add some spaced image into a Canvas?

我绝对是 Android 开发的新手,我有以下疑问。

我必须将一个相邻的图像绘制到一个Canvas对象中。

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

在此处输入图片说明

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

所以我做了这样的事情:

// 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();

所以我认为我可以将之前的图像添加到Canvas 中,执行如下操作:

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

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

那么,如何将这些图像中的 3 个彼此相邻添加,在图像和下一个图像之间留出一些空白?

这样的事情应该工作:

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

这应该可以完成工作,例如对于具有水平空间的两个图像

public static mergeImages(Bitmap firstImage, Bitmap secondImage)
{
 Bitmap cs;
 int width, height;
 float space = 60f; // space between image horizontal

 if(firstImage.getWidth() > secondImage.getWidth()) {
 width = firstImage.getWidth() + secondImage.getWidth();
 } else {
 width = s.getWidth() + secondImage.getWidth();
 }
 height = firstImage.getHeight();

 cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

 Canvas comboImage = new Canvas(cs);
 comboImage.drawBitmap(firstImage, 0f, 0f, null);
 comboImage.drawBitmap(secondImage, firstImage.getWidth()+space, 0f, null);

 return cs; // result image
}

暂无
暂无

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

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