android - 清楚地了解矩阵计算

标签 android math android-bitmap

我有这个代码 fragment 。我不明白 Matrix.preScaleBitmap.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 矩阵有 3 行,每行 3 个数字。每个数字告诉 Android 图形函数如何缩放(变大/变小)、平移(移动)、旋转(转动)或倾斜(在 2D 平面中扭曲)应用矩阵的“事物”。

矩阵看起来像这样(参见此处的 docs)。

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

好消息是您不需要知道任何矩阵数学,实际上几乎不需要数学,就可以在 Android 中使用矩阵。这就是 preScale() 之类的方法为您所做的。理解背后的数学并不难,对于大多数事情你只需要加、乘和SOHCAHTOA。 .

matrix-transform-for-the-mathematically-challenged/

当您阅读 Matrix 文档时,您会看到带有“set”、“post”或“pre”前缀的旋转、翻译等方法。

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

为了回答您的具体问题,new Matrix() 创建了“单位矩阵”

{1, 0, 0
 0, 1, 0
 0, 0, 1}

按 1 缩放(因此大小相同)并且不进行平移、旋转或倾斜。因此,应用单位矩阵将无济于事。下一个方法是 preScale() ,它应用于这个单位矩阵,在你展示的情况下,会产生一个可缩放的矩阵,并且什么都不做,所以也可以使用 setScale() 或 postScale() 来完成。

希望这对您有所帮助。

关于android - 清楚地了解矩阵计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13246415/

相关文章:

android - 恰好在午夜更新应用程序

android - Ionic Build 抛出以下错误 : "could not find any matches for com.android.tools.build:gradle:+"

algorithm - 给定平面上的两条线,如何找到最接近它们交点的整数点?

c++ - 仅使用两个操作遍历二叉树以从一个数字获取另一个数字

android如何将pdf转换为图像并在imageview中加载

android - 特定 Activity 被销毁时释放内存

android - 卢比符号未在 android 中显示

android - 使用 CursorLoader 查询 SQLite 数据库并填充 AutoCompleteTextView

math - UTC 时区的午夜,带有 unix 时间戳

安卓:内存不足