0

我知道还有很多关于此的其他线程。在过去的两天里,我一直在检查它们,但无济于事,现在它让我发疯了。所以基本上我有一个精灵表,我将其加载到位图中,将帧分开,对它们执行缩放并将它们加载到列表中以供以后制作动画。从 ldpi 到 xhdpi 的缩放看起来不错,动画也是如此。

但还有一件事让我很烦恼,那就是在不同设备上的定位。我用 canvas.drawBitmap(bitmap, srcRect, destRect, paint) 绘制它们。好吧,我猜代码能说出一千多个单词。

//converting dps to pixels
int xCoord = convertToPixels(100);
int yCoord = convertToPixels(150);

//defining destination rectangle; sprite is a Bitmap object
Rect destRect = new Rect(xCoord, yCoord, xCoord+sprite.getWidth(), yCoord+sprite.getHeight());

//drawing
canvas.drawBitmap(sprite, null, destRect, null);

convertToPixels(float dp) 只返回带有公式 Math.round(dp*DENSITY) 的像素数。

我相信,如果我指定 dps,所有设备上的纵横比应该是相同的,例如,如果我在一个设备的屏幕尺寸的 10% 上放置一些东西,它应该在另一个更小/更大/不那么密集/上保持这 10%更密集的屏幕。但我想我的逻辑是有缺陷的,因为它不起作用。它在不同地方的不同设备上绘制精灵。

总结一下:用 destRect 绘制一个位图对象,x 和 y 坐标为 100 dps,应该在所有设备上保持与画布大小的比例,或者我应该这样想。

我想请你帮我解决这件事,因为我现在迷路了,更多的是字面意思而不是比喻。并且请不要只给我一个链接到开发者网站或类似网站上的“支持多个屏幕”主题,因为我已经阅读了很多次,但似乎我不理解它们。谢谢!

4

1 回答 1

0

Are you sure the dip to px calculation is correct? I wrote the following code to do this:

public class ViewHelper {
    public static int getPxFromDip(int dips){
        return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dips, Application.getContext().getResources().getDisplayMetrics());
    }
}

Where Application.getContext() is the Application wide context, or provide the local Activity context.

于 2012-07-09T21:56:34.840 回答