在Android的的LinearLayout缩放图像并将其放置在特定的位置(坐标) [英] Scaling an image and place it in the specific location (coordinates) in Android's LinearLayout

查看:494
本文介绍了在Android的的LinearLayout缩放图像并将其放置在特定的位置(坐标)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是很熟练的Andr​​oid应用程序开发和我正在一个测试应用程序。我侦测到的脸部和眼睛,现在我要根据眼睛的坐标来绘像在脸上的青春痘或瘢痕(例如眼睛下方的面颊)。后来,我打算把上的相应位置眼玻璃或帽子。

I'm not very skilled in android application developing, and I'm working on a test app. I detected the face and the eyes, now I'm going to draw something like acne or scar on the face (e.g below the eyes on the cheek) based on the coordinates of the eyes. Later, I'm going to put eye-glass or hat on the appropriate locations.

我知道的左侧和(在code leftEyePosx ...等等])右眼的坐标。例如136x168 [左]和216x168 [右](图中反映)。现在,我计算规模:玻璃应位约80像素(216-136)或更大的宽度,以及80像素乘以原始图像的长宽比为高度(例如80 * 0.7)进行缩放。我与code位图:

I know the coordinates of the left and the right eye (leftEyePosx... [and so on] in the code). For example 136x168 [left] and 216x168 [right] (mirrored in picture). Now, I'm calculating the scale: glass bitmap should be scaled around 80 pixels (216-136) or bigger for the width, and 80 pixels multiplied with the original image's aspect ratio for the height (e.g. 80 * 0.7). I have the bitmap with the code:

glassBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.glass_01);

现在,我怎么能再次大规模眼玻璃和使用的方法canvas.drawBitmap()和用眼坐标和吸取脸上的玻璃?或者我应该用另一种方式?

Now, how can I re-scale the eye-glass and use the method canvas.drawBitmap() and use eye coordinates and draw the glass on the face? Or should I use another way?

在此先感谢,为我的英语不好对不起:)

Thanks in advance, and sorry for my bad English :)

推荐答案

首先,你缩放​​位图:

First you scale the bitmaps:

Bitmap scaledBitmap = Bitmap.createScaledBitmap(originalBitmap, targetWidth, targetHeight, false);

然后在另一个的上面画一个位图,在XPOS和yPos:

Then you draw one bitmap on top of the other, at xPos and yPos:

Canvas canvas = new Canvas(baseBitmap);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(overlayBitmap, xPos, yPos, paint); 

XPOS和yPos是覆盖位图的左上角。例如,如果基本图像的眼睛是应该在覆盖位图的中间显示,则必须减去的宽度和高度的一半,以找到所需的XPOS和yPos来调整此

xPos and yPos are the upper left corner of the overlay bitmap. For example, if the base image eye is supposed to show in the middle of the overlay bitmap, you have to adjust for this by deducting half the width and height to find the desired xPos and yPos.

这篇关于在Android的的LinearLayout缩放图像并将其放置在特定的位置(坐标)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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