1

我想在自定义视图中加载一定大小的位图。作为第一步,我想在 400 像素 x 400 像素的视图中加载 400 像素 x 400 像素的位图……但加载的位图尺寸较小。

我发现了一些有类似问题的线程。建议使用Options.inScale = false. 我使用了它,并且得到了更好的日志消息(位图输出正确的大小为 400px x 400px),但它仍然被渲染得更小:

在此处输入图像描述

(黄色是自定义视图的背景 - 400px x 400px,红色是位图)。

有什么建议吗?这是代码:

自定义视图:

class TestView extends View {
    private Bitmap bitmap;
    private Paint paint = new Paint();

    public TestView(Context context) {
        super(context);
    }

    public TestView(Context context, AttributeSet attr) {
        super(context, attr);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inScaled = false;

        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.overlay, options).copy(Config.ARGB_8888, true);  
        Log.d("test6", "width: " + bitmap.getWidth() + " height: " + bitmap.getHeight());
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap, 0, 0, paint);
    }
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    >
    <com.example.TestView
        android:id="@+id/view"
        android:layout_width="400px"
        android:layout_height="400px"
        android:background="#ffff00"
        />
</RelativeLayout>

位图已经是 400px x 400px ...这就是为什么我认为我不必使用缩放选项等。我只想在屏幕上以这个大小显示它...

4

1 回答 1

2

您很可能Bitmap不在drawable-nodpi文件夹中,这就是它被缩放的原因。

根据Canvas.drawBitmap的文档:

如果位图和画布具有不同的密度,此函数将负责自动缩放位图以与画布相同的密度进行绘制。

于 2013-03-14T14:32:51.397 回答