清楚了解矩阵计算

问题描述 投票:16回答:1

我有此代码段。我不理解Matrix.preScaleMatrix.preScale与传递的矩阵。 是什么意思?是否有任何模拟网站可以了解矩阵计算?您能给我一些有关用于图形的数学的网站吗?对不起,我不擅长数学。 :)

Bitmap.createBitmap
android math android-bitmap
1个回答
93
投票

[不要考虑得太辛苦,至少在初期不要考虑。

只需将矩阵视为数字数组。在这种情况下,Android矩阵具有3行3个数字。每个数字都告诉Android图形功能如何对应用矩阵的“事物”进行缩放(更大/更小),平移(移动),旋转(旋转)或倾斜(在2D平面中变形)。

矩阵看起来像这样(请参见此处的Bitmap.createBitmap

public Bitmap createReflectedImages(final Bitmap originalImage) {
    final int width = originalImage.getWidth();
    final int height = originalImage.getHeight();
    final Matrix matrix = new Matrix();
    matrix.preScale(1, -1);
    final Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, (int) (height * imageReflectionRatio),
            width, (int) (height - height * imageReflectionRatio), matrix, false);
    final Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (int) (height + height * imageReflectionRatio + 400),
            Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(originalImage, 0, 0, null);
    final Paint deafaultPaint = new Paint();
    deafaultPaint.setColor(color.transparent);
    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
    final Paint paint = new Paint();
    final LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
    return bitmapWithReflection;
}

好消息是,您无需了解任何矩阵数学,甚至几乎不需要数学,就可以在Android中使用矩阵。那就是像preScale()这样的方法为您做的。理解后面的数学并不难,对于大多数事情,您只需要加,乘和docs

{Scale X, Skew X, Transform X Skew Y, Scale Y, Transform Y Perspective 0, Perspective 1, Perspective 2}

当您阅读Matrix文档时,将看到带有'set','post'或'pre'前缀的旋转,平移等方法。

想象您创建了一个新矩阵。然后,使用setRotate()设置矩阵进行旋转。然后,您可以使用preTranslate()进行翻译。因为您使用的是“ pre”,所以翻译发生在旋转之前。如果您使用“发布”,则轮换将首先发生。 'set'清除矩阵中的所有内容,然后重新开始。

为了回答您的特定问题,新的Matrix()创建了“身份矩阵”

SOHCAHTOA

将按比例缩放1(因此大小相同),并且不进行平移,旋转或倾斜。因此,应用身份矩阵将无济于事。下一个方法是preScale(),该方法将应用于该标识矩阵,并且在您已显示的情况下,该结果将生成一个可缩放的矩阵,并且不执行其他任何操作,因此也可以使用setScale()或postScale()来完成。

希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.