水印来自整个图像Android

时间:2016-04-19 06:24:17

标签: android bitmap android-canvas

我想将水印放在图片的右下角。但是,在一些手机中,它完美地出现在一些手机中,水印正在整个图像中出现。

这是我的代码:

rotatedBitmap = Bitmap.createBitmap(loadedImage, 0, 0,
                            loadedImage.getWidth(), loadedImage.getHeight(),
                            rotateMatrix, false);

                    int w = rotatedBitmap.getWidth();
                    int h = rotatedBitmap.getHeight();
                    Bitmap result = Bitmap.createBitmap(w, h, rotatedBitmap.getConfig());
                    Canvas canvas = new Canvas(result);
                    canvas.drawBitmap(rotatedBitmap, 0, 0, null);

                    Bitmap waterMark = BitmapFactory.decodeResource(getResources(), R.drawable.watermark2);
                int ww = waterMark.getWidth();
                int hh = waterMark.getHeight();
                    canvas.drawBitmap(waterMark,(w-ww),(h-hh), null);

编辑:以下是结果的屏幕截图。在第二张图片中,水印完美无缺,在第一张图片中,它正在全局展现。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

利用宽高比。计算设备的纵横比,并相应地计算水印的合适尺寸。

如何计算宽高比?

检查屏幕的宽度和高度,以较大的值除以较小的一个 例如:

if(screen_width > screen_height){ aspectRatio = screen_width/screen_height }else{ aspectRatio = screen_height/screen_width }

现在您已经计算出宽高比乘以水印的大小并相应地进行缩放。

像:

float ww = watermark.getWidth()* aspectRatio; float wh = watermark.getHeight()* aspectRatio;

并使用这些值。

希望这会有所帮助......