代码之家  ›  专栏  ›  技术社区  ›  DavidAndroidDev

Android-在画布上定位不同设备的位图。如何处理?

  •  1
  • DavidAndroidDev  · 技术社区  · 13 年前

    我的情况是绘制一个背景,其中其他对象包含在背景的区域内,以便于边界。我设置了曲面视图绘图,以便将位图布局在正确的位置,但当我在另一台设备上进行测试时,东西就没有处于正确的位置。

    我重构了我的代码,使其不那么具体,并且所有的计算都基于画布的中心(这是我给定图像的要求)。考虑到我的其他对象必须保留在图像的特定区域,我想我可以计算出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);
    
    2 回复  |  直到 13 年前
        1
  •  3
  •   BrainCrash    13 年前

    如果你的问题和我通常遇到的问题一样,我会根据当前屏幕分辨率计算比例来解决。

    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,就可以得到正确的比例。

        2
  •  0
  •   DavidAndroidDev    13 年前

    虽然我不会改变公认的答案,但实际上我的做法略有不同。而不是通过检查 dm.widthPixels 因此,我找到了一个更好的方法。

    代码应该是:

    //Trimmed for brevity.
    //In the constructor...
    //mDisplayMetrics is a private variable in the class.
    this.mDisplayMetrics = new DisplayMetrics();
    ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
    //I no longer need the target ratio variables anymore.
    
    float bobblePosX = centerX - (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, this.mDisplayMetrics) * mScale);
    float bobblePosY = centerY + (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, this.mDisplayMetrics) * mScale);
    
    mDrawingMatrix.postScale(mScale, mScale);
    mDrawingMatrix.postTranslate(bobblePosX, bobblePosY);
    canvas.drawBitmap(mFlatBobble, mDrawingMatrix, null);
    

    在上面的代码中,我改变了将之前的目标屏幕比例值乘以我想要的mdpi值的位置。我还需要稍微缩放位置,因为图像并不总是与屏幕分辨率匹配。

    希望这能帮助到别人!