gpt4 book ai didi

java - 如何在支持多种屏幕尺寸的Android中截图后立即添加图标或水印图像

转载 作者:行者123 更新时间:2023-12-01 20:18:21 25 4
gpt4 key购买 nike

请在投票前重新阅读我的问题,我想在拍摄后立即将图标或 png 图像添加到屏幕截图图像

我不知道这是不是一个好方法,但我尝试过

private static Bitmap addWaterMark(Bitmap src, Context context, String waterMarkImage) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Log.d(TAG, "Width X Height " + width + " X " + height);

Point point = new Point();
display.getSize(point);
int maxX = point.x;
int maxY = point.y;
point.set(maxX, maxY);

int maxWidth = (70 * width) / 100;
int maxHeight = (80 * height) / 100;
Log.d(TAG, "maxWidth X maxHeight " + maxWidth + " X " + maxHeight);

Bitmap waterMark = null;
if (waterMarkImage != null) {
byte[] decodedByte = Base64.decode(waterMarkImage, 0);
waterMark = BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
waterMark = Bitmap.createScaledBitmap(waterMark, 250, 250, true);
} else {
waterMark = BitmapFactory.decodeResource(context.getResources(), R.drawable.sharelogo1);
}
canvas.drawBitmap(waterMark, maxWidth, maxHeight, null);

return result;
}

它工作正常,但水印图像大小和位置未按我想要的方式工作(不支持所有屏幕尺寸或分辨率)

最佳答案

在您的代码 fragment 中进行更改

waterMark = Bitmap.createScaledBitmap(waterMark, 250, 250, true);

waterMark = Bitmap.createScaledBitmap(waterMark, getpixels(250), getpixels(250), true);

并添加以下方法将 dp 转换为像素

private float getpixels(float dipValue) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, Resources.getSystem().getDisplayMetrics());
}

关于java - 如何在支持多种屏幕尺寸的Android中截图后立即添加图标或水印图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58947787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com