如何在120dpi屏幕上获得可绘制的宽度和高度?

时间:2014-01-15 16:01:41

标签: android

我想得到我的可绘制的内在宽度和高度。

它在160 dpi的屏幕下运行良好,但是当我以120 dpi的速度运行模拟器时,它返回的小值超过160 dpi。

那么如何获得与160 dpi屏幕相同的正确值?

这是在Drawable对象中初始化我的图像的代码:

Drawable d = getResources().getDrawable(mBitmapIds[index]);

提前感谢。

修改添加样本:

这是160 dpi中的正确值>高度= 726宽度= 602

这是120 dpi中的错误值>>高度= 545宽度= 452

如何使120 dpi与内部宽度和高度的160 dpi相同?

3 个答案:

答案 0 :(得分:0)

如果宽度为100,请使用:

100 px

而不是:

100 dpi

这样android不会应用dpi单位的缩放因子,并且尺寸在任何屏幕都是相同的......

答案 1 :(得分:0)

假设您的图片尺寸为widthlength。 我建议您在自定义视图中绘制页面和突出显示,覆盖onDraw(Canvas),如下所示:

class PageHilghlighter extends View {

    @Override
    protected void onDraw(Canvas canvas) {
        // The Canvas you receive is automatically clipped so that its clip bounds reflect the space dedicated to your view
        Rect bounds = canvas.getClipBounds();
        Drawable myPage = getCurrentPage(); // To implement
        int saveCount = canvas.save();
        // Scale the canvas so that your page fills your view
        canvas.scale(myPage.getIntrisicWidth() / bounds.width(), myPage.getIntrisicHeight() / bounds.height());
        myPage.draw(canvas);
        float x = getLineX(); // To implement
        float y = getLineY(); // To implement
        canvas.drawRect(x, y, x + myLineWidth(), y + myLineHeight(), myPaint); // Use some transparent paint ...
        canvas.restore(saveCount);
    }
}

这样的事情应该有用。我没有测试过它。

答案 2 :(得分:0)

这是获得正确的内在宽度和高度的解决方案:

  Bitmap pageBitmap = BitmapFactory.decodeResource(getResources(), mBitmapIds[index]);    
        DisplayMetrics dm = getResources().getDisplayMetrics();

        if (dm.densityDpi == 120) {
            pageBitmap.setDensity(90);
        } else if (dm.densityDpi == 120) {
            pageBitmap.setDensity(160);
        } else if (dm.densityDpi == 240) {
            pageBitmap.setDensity(360);
        } else if (dm.densityDpi == 320) {
            pageBitmap.setDensity(640);
        }

        Drawable d = new BitmapDrawable(getResources(), pageBitmap);
相关问题