gpt4 book ai didi

Android 图像调整大小基本

转载 作者:行者123 更新时间:2023-11-29 02:09:33 24 4
gpt4 key购买 nike

我喜欢调整位图的大小,如果它很大就把它变小并将它放在表面 View 中的特定位置,我需要获取设备宽度和高度然后位图大小并将它们放在表面 View 中然后拍摄另一张图片调整大小并将其放置在我喜欢的任何位置。如何首先知道位置坐标(放置第二张图片 - 如果大/小屏幕布局不应该改变,这应该与设备无关)

以及如何缩放大位图并将其设置为背景以便我可以在其上绘制图像。我的代码:

final int canvasWidth = getWidth();
final int canvasHeight = getHeight();

int imageWidth = img.getWidth();
int imageHeight = img.getHeight();

float scaleFactor = Math.min( (float)canvasWidth / imageWidth,
(float)canvasHeight / imageHeight );
Bitmap scaled = Bitmap.createScaledBitmap( img,
(int)(scaleFactor * imageWidth),
(int)(scaleFactor * imageHeight),
true );
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(scaled, 10, 10, null);

这会缩放大图像,但它不适合整个屏幕“img - 位图就像背景图像”

有人可以帮助我了解调整大小的基础知识(我是新手,所以很难理解调整大小)以调整图像大小以适合屏幕并将任何图像调整为较小的图像并将其放置在我喜欢的任何位置。

最佳答案

将位图存储为操作源并使用 ImageView 显示它:

Bitmap realImage = BitmapFactory.decodeFile(filePathFromActivity.toString());

Bitmap newBitmap = scaleDown(realImage, MAX_IMAGE_SIZE, true);


imageView.setImageBitmap(newBitmap);


//scale down method
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize,
boolean filter) {
float ratio = Math.min(
(float) maxImageSize / realImage.getWidth(),
(float) maxImageSize / realImage.getHeight());
int width = Math.round((float) ratio * realImage.getWidth());
int height = Math.round((float) ratio * realImage.getHeight());

Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width,
height, filter);
return newBitmap;
}

并将您的 ImageView 的宽度和高度设置为“fill_parent”。

关于Android 图像调整大小基本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8268517/

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