简体   繁体   English

位图密度导致问题

[英]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). 我正在做自己的像素计算,以便在屏幕上绘制图像,并且具有精灵位图(一个文件中包含4x2图像)。 Each image is 100x100, and the overall file is 400x200. 每个图像为100x100,整个文件为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: 使用以下方法,我的绘图未拉到位图的正确部分,并且在scren上的放置不太正确:

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

Since: API Level 1 由于:API级别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. 如果源矩形不为null,则它指定要绘制的位图的子集。

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. 注意:如果绘画包含的maskfilter生成的蒙版超出了位图的原始宽度/高度(例如BlurMaskFilter),则将绘制位图,就好像它处于具有CLAMP模式的Shader一样。 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. src>可能为null。 The subset of the bitmap to be drawn 要绘制的位图的子集

dst > The rectangle that the bitmap will be scaled/translated to fit into dst>位图将被缩放/转换以适合的矩形

paint > May be null. paint>可能为空。 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] ? 您到底如何加载pics[0] Have you tried getResources().openRawResource(imageId) ? 您是否尝试过getResources().openRawResource(imageId)

This solved my problem. 这解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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