2016-08-15 63 views
0

我正在一个简单的记忆游戏中,我从服务器获取9个图像,并在3X3网格视图中显示它们。一旦加载图像,用户可以看到图像15秒。 15秒后,所有图像应翻转。现在15秒后,我在网格视图下方显示一个图像视图。在此图像视图中,我显示了上述9个图像中的一个随机选取的图像。现在用户需要在网格视图中选择正确的图像位置。如果用户在网格视图中选择正确的位置,那么我需要用正确的图像翻转图像。当用户翻转所有图像时,游戏将结束。如何在记忆游戏中实现鳍状​​肢?

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

回答

0

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

Camera | Android Developers

这是graphicsCamera。鉴于标准缩放,旋转和平移动画仅在x-y平面中工作,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

此外,通过翻转,你会想要动画第一图像从0度到-90度,然后隐藏该图像,并显示第二个图像,同时将剩余路径从90度动画到0度。

最后一件事。你有一个3x3网格。为了获得逼真的三维翻转效果,您需要使用不同的x和y值来实验mCamera.setLocation(),具体取决于拼贴,以便顶部拼贴看起来像是从底部查看翻盖,底部拼贴看起来像你正在查看从头顶上翻转等。