如何在记忆游戏中实现鳍状​​肢?

时间:2016-08-15 06:23:55

标签: android android-layout gridview android-animation

我正在开发一个简单的记忆游戏,我从服务器上取9张图像,然后在3X3网格视图中显示它们。加载图像后,用户可以看到图像15秒。 15秒后,应翻转所有图像。 15秒后,我在网格视图下方显示一个图像视图。在此图像视图中,我显示了来自上述9个图像的一个随机拾取的图像。现在用户需要在网格视图中选择图像的正确位置。如果用户在网格视图中选择正确的位置,那么我需要使用正确的图像翻转图像。当用户翻回所有图像时,游戏将结束。

我已经获取图像并以网格视图显示,但我不知道如何翻转和翻转图像。 我检查了Android中的翻转动画,但是它用于旋转图像。在我的情况下,图像翻转时不应该是可见的,当它翻转时它应该是可见的。在这种情况下如何实现翻转和翻转?

1 个答案:

答案 0 :(得分:0)

请允许我向您介绍Camera课程。不,不是硬件相机。

Camera | Android Developers

这是graphics Camera。标准缩放,旋转和平移动画仅在xy平面中工作,Camera引入了一个z轴,它在图像上提供了透视,并允许您在三维上进行变换两个。

您最感兴趣的是rotateY()方法。这种方法可以帮助您创建正在翻转的卡片或图块的动画。

以下是我使用Camera类进行平铺翻转的方法:

    private Matrix mMatrix;

    private Camera mCamera;

    // val is animation value between -1.0 and 1.0
    private void updateImageMatrix(float val) {

        // turn the animation value into an angle from 
        // 0 to -90, then +90 to 0 degrees
        float degrees = 90.0F * (Math.signum(val) - val);

        mCamera.save();               // save initial state
        mCamera.rotateY(degrees);     // this does the flip
        mCamera.getMatrix(mMatrix);   // write the transform into the matrix
        mCamera.restore();            // now we can reuse the Camera without re-constructing

        float centerX = imageWidth / 2.0F;
        float centerY = imageHeight / 2.0F;

        // Y-axis is at the left edge, so to get rotation
        // around the center, we have to move the image over
        // first, then move it back after we rotate
        mRotateMatrix.preTranslate(- centerX, - centerY);
        mRotateMatrix.postTranslate(centerX, centerY);
        // this could probably be eliminated by initializing mCamera with
        // mCamera.setLocation(centerX, centerY, mCamera.getLocationZ())

        // the image probably isn't exactly the same size as the
        // view, so we have to handle scaling the image to the view
        mRotateMatrix.preScale(mScaleFactor, mScaleFactor);
    }

现在你有了一个矩阵来进行变换,你可以通过多种方式使用它:

  1. 您可以将此矩阵设置为ImageView作为图像矩阵

  2. 您可以使用覆盖View的自定义onDraw并致电canvas.drawBitmap(mBitmap, mMatrix, null)

  3. 您可以拥有一个覆盖Animation的自定义applyTransformation(float interpolatedTime, Transformation t)类,并根据矩阵返回更新的Transformation

  4. 此外,通过翻转,您可能希望将第一个图像设置为0到-90度,然后隐藏该图像,并显示第二个图像,同时将剩余的图像从90度设置为0度。

    最后一件事。你有一个3x3网格。为了获得逼真的三维翻转效果,您需要使用不同的x和y值来尝试mCamera.setLocation(),具体取决于图块,以便顶部图块看起来像是从下面看底部,底部瓷砖看起来就像是从头顶上看翻转等。