ixx
ixx

Reputation: 32269

Custom view bitmap doesn't have correct size

I want to load a bitmap with a certain size in a custom view. As a first step I wanted to load a 400px x 400px bitmap in a 400px x 400px view... but the bitmap is loaded with a smaller size.

I found some threads with similar issues. It was adviced to use Options.inScale = false. I used it, and got better log messages (bitmap outputs correct size 400px x 400px), but it still is rendered smaller:

enter image description here

(yellow is the background of the custom view - 400px x 400px, and red is the bitmap).

Any advices? Here is the code:

Custom view:

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>

The bitmap is already 400px x 400px ... that's why I think I don't have to use scaling options, etc. I just want to show it with this size in the screen...

Upvotes: 1

Views: 855

Answers (1)

Dmitry Zaytsev
Dmitry Zaytsev

Reputation: 23972

Most likely your Bitmap is located not in drawable-nodpi folder and that's why it's getting scaled.

According to documentation of Canvas.drawBitmap:

If the bitmap and canvas have different densities, this function will take care of automatically scaling the bitmap to draw at the same density as the canvas.

Upvotes: 2

Related Questions