简体   繁体   中英

Bitmap density causing problems

I'm doing my own pixel calculations for drawing on the screen, and have a sprite bitmap (4x2 images in one file). Each image is 100x100, and the overall file is 400x200. Using the following method my drawing is not pulling the right section of the bitmap, and the placement on the scren isn't quite right:

public void drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)

Since: API Level 1

Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle. If the source rectangle is not null, it specifies the subset of the bitmap to draw.

Note: if the paint contains a maskfilter that generates a mask which extends beyond the bitmap's original width/height (eg BlurMaskFilter), then the bitmap will be drawn as if it were in a Shader with CLAMP mode. Thus the color outside of the original width/height will be the edge color replicated.

This function ignores the density associated with the bitmap. This is because the source and destination rectangle coordinate spaces are in their respective densities, so must already have the appropriate scaling factor applied.

Parameters

bitmap > The bitmap to be drawn

src > May be null. The subset of the bitmap to be drawn

dst > The rectangle that the bitmap will be scaled/translated to fit into

paint > May be null. The paint used to draw the bitmap

Rect source = new Rect();
    source.left = (this.currentFrame % 4) * 100;
    source.right = source.left + 100;
    source.top = (this.currentFrame / 4) * 100;
    source.bottom = source.top + 100;

    Rect dest = new Rect();
    dest.top = (int) top;
    dest.left = (int) left;
    dest.right = dest.left + 100;
    dest.bottom = dest.top + 100;

    // draw current frame onto canvas
    //canvas.drawBitmap(pics[this.currentFrame], left, top, painter);
    canvas.drawBitmap(pics[0], source, dest, painter);

The documentation explicity says densities are ignored, but why is my image being scaled?

As per Renard

How exactly are you loading pics[0] ? Have you tried getResources().openRawResource(imageId) ?

This solved my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM