canvas.drawBitmap和canvas.drawRect,绘制对象的大小不一样

时间:2011-11-23 16:08:02

标签: java android canvas drawing

我在使用canvas.drawRect时遇到canvas.drawBitmap和canvas.drawRect的问题。一切正常,但是当我使用canvas.drawBitmap时,事情开始变得奇怪。在屏幕上绘制的对象的大小与DrawRect的大小不同于DrawProcentObject的大小。我无法在代码中找到数学问题所以我猜问题就在于canvas.drawBitmap的工作原理。

如果有人能告诉我问题是什么以及如何解决,我将不胜感激。

宽度,高度是此处对象的大小,bmp是位图。

DrawRect(canvas, width, height, x, y, Color.BLACK);
DrawProcentObject(canvas, bmp, x, y, width, height);

此方法的宽度,高度是屏幕分辨率。

public void DrawObject(Canvas canvas, Bitmap bmp, int X, int Y) {
    canvas.save();
    canvas.drawColor(Color.TRANSPARENT);
    Paint paint = new Paint();
    canvas.drawBitmap(bmp, X, Y, paint);
    canvas.restore();
}
public void DrawProcentObject(Canvas canvas, Bitmap bmp, int X, int Y, int SizeWidth, int SizeHeight)
{
    double x = width * ((double)X/1000);
    double y = height * ((double)Y/1000);
    double sizeWidth = width * ((double)SizeWidth/1000);
    double sizeHeight = height * ((double)SizeHeight/1000);
    bmp = Bitmap.createScaledBitmap(bmp, (int) sizeWidth, (int) sizeHeight, true);
    DrawObject(canvas, bmp, (int) x, (int) y);
}
public void DrawRect(Canvas canvas, int SizeWidth, int SizeHeight, int X, int Y, int rectColer)
{
    canvas.save();
    canvas.drawColor(Color.TRANSPARENT);
    Paint paint = new Paint();
    paint.setColor(rectColer);
    canvas.drawRect(ProcentRect(SizeWidth, SizeHeight, X, Y), paint);
    canvas.restore();
}
public Rect ProcentRect(int SizeWidth, int SizeHeight, int X, int Y)
{
    double x = width * ((double)X/1000);
    double y = height * ((double)Y/1000);
    double sizeWidth = width * ((double)SizeWidth/1000);
    double sizeHeight = height * ((double)SizeHeight/1000);
    Rect rect = new Rect((int)x, (int)y, (int)x + (int)sizeWidth, (int)y + (int)sizeHeight);
    return rect;
}

0 个答案:

没有答案