gpt4 book ai didi

Android - 在 Canvas 上定位位图因设备而异。如何处理?

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

我的情况涉及绘制一个背景,其中其他对象包含在背景区域中,以便进行边界。我设置了我的表面 View 绘图以将我的位图布置在正确的位置,但是当我在不同的设备上进行测试时,事情并不在正确的位置。

我重构了我的代码,使其不那么具体,并将我的所有计算都基于 Canvas 中心(这是我给定图像的要求)。鉴于我的其他对象必须保留在图像的特定区域,我认为我可以计算出 mdpi 图像上的 x-y 位置并尝试根据 Canvas 的比例缩放我的位置。没用。我不太确定此时还能尝试什么。

//Center of canvas
float centerX = canvas.getWidth() / 2;
float centerY = canvas.getHeight() / 2;

mDrawingMatrix.reset();
mDrawingMatrix.postTranslate(-(mBackground.getWidth() / 2), -(mBackground.getHeight() / 2));
mDrawingMatrix.postScale(mScale, mScale);
mDrawingMatrix.postTranslate(centerX, centerY);
canvas.drawBitmap(mBackground, mDrawingMatrix, null);

mDrawingMatrix.reset();
mDrawingMatrix.postTranslate(-(mFlatBobble.getWidth() / 2), -(mFlatBobble.getHeight() / 2));
mDrawingMatrix.postScale(mScale, mScale);
mDrawingMatrix.postTranslate(centerX-(10 * mScale), centerY + (50 * mScale));
canvas.drawBitmap(mFlatBobble, mDrawingMatrix, null);

mDrawingMatrix.reset();
mDrawingMatrix.postTranslate(-(mBobble.getWidth() / 2), -(mBobble.getHeight() / 2));
mDrawingMatrix.postScale(mScale, mScale);
mDrawingMatrix.postTranslate(centerX + (68 * mScale), centerY + (78 * mScale));
canvas.drawBitmap(mBobble, mDrawingMatrix, null);

mDrawingMatrix.reset();
mDrawingMatrix.postTranslate(-(mMask.getWidth() / 2), -(mMask.getHeight() / 2));
mDrawingMatrix.postScale(mScale, mScale);
mDrawingMatrix.postTranslate(centerX, centerY);
canvas.drawBitmap(mMask, mDrawingMatrix, null);

EDIT2:以下代码已过时。请参阅我在下面添加的答案。

编辑:工作代码现在看起来像:

//In the constructor...
DisplayMetrics dm = new DisplayMetrics();
((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
mTargetScreenXRatio = (float)(dm.widthPixels / 480);
mTargetScreenYRatio = (float)(dm.heightPixels / 320);

//Center of canvas
float centerX = canvas.getWidth() / 2;
float centerY = canvas.getHeight() / 2;

mDrawingMatrix.reset();
mDrawingMatrix.postTranslate(-(mBackground.getWidth() / 2), -(mBackground.getHeight() / 2));
mDrawingMatrix.postScale(mScale, mScale);
mDrawingMatrix.postTranslate(centerX, centerY);
canvas.drawBitmap(mBackground, mDrawingMatrix, null);

mDrawingMatrix.reset();
mDrawingMatrix.postTranslate(-(mFlatBobble.getWidth() / 2), -(mFlatBobble.getHeight() / 2));
mDrawingMatrix.postScale(mScale, mScale);
mDrawingMatrix.postTranslate(centerX-(10 * mScale * mTargetScreenXRatio), centerY + (50 * mScale * mTargetScreenYRatio));
canvas.drawBitmap(mFlatBobble, mDrawingMatrix, null);

mDrawingMatrix.reset();
mDrawingMatrix.postTranslate(-(mBobble.getWidth() / 2), -(mBobble.getHeight() / 2));
mDrawingMatrix.postScale(mScale, mScale);
mDrawingMatrix.postTranslate(centerX + (68 * mScale * mTargetScreenXRatio), centerY + (78 * mScale * mTargetScreenYRatio));
canvas.drawBitmap(mBobble, mDrawingMatrix, null);

mDrawingMatrix.reset();
mDrawingMatrix.postTranslate(-(mMask.getWidth() / 2), -(mMask.getHeight() / 2));
mDrawingMatrix.postScale(mScale, mScale);
mDrawingMatrix.postTranslate(centerX, centerY);
canvas.drawBitmap(mMask, mDrawingMatrix, null);

最佳答案

如果你的问题和我平时遇到的一样,我通过根据当前屏幕分辨率自己计算比例来解决它。

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);

screenW = dm.widthPixels;
screenH = dm.heightPixels;

X = (float) screenW / 480;
Y = (float) screenH / 800;

480 * 800 是我的引用屏幕尺寸,您可以将其更改为您喜欢的引用屏幕尺寸。

此时,您只需将坐标乘以 X 和 Y 即可获得正确的比例。

关于Android - 在 Canvas 上定位位图因设备而异。如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15211863/

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