我的情况是绘制一个背景,其中其他对象包含在背景的区域内,以便于边界。我设置了曲面视图绘图,以便将位图布局在正确的位置,但当我在另一台设备上进行测试时,东西就没有处于正确的位置。
我重构了我的代码,使其不那么具体,并且所有的计算都基于画布的中心(这是我给定图像的要求)。考虑到我的其他对象必须保留在图像的特定区域,我想我可以计算出mdpi图像上的x-y位置,并尝试根据画布的比例缩放我的位置。没用。我真的不确定在这一点上还能尝试什么。
//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);
编辑2:下面的代码已过时。请看我在下面添加的答案。
编辑:
现在的工作代码如下所示:
//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);