2015-03-13 88 views
5

我正在尝试创建一个拼图游戏,并且我想知道创建拼图时不使用蒙版的替代方法。目前,我通过拍摄完整图像来制作拼图碎片,将该图像分成四部分(可以说拼图是2×2),然后在每个部分存储并应用一个蒙版。它看起来像下面如何在不使用面具的情况下制作拼图碎片?

// create standard puzzle pieces 
    arryPieceEndPos = new int[mCols][mRows]; 
    arryPieceImg = new Bitmap[mCols * mRows]; 
    arryIsPieceLocked = new boolean[mCols * mRows]; 

    int pos = 0; 
    for (int c = 0; c < mCols; c++) { 
     for (int r = 0; r < mRows; r++) { 
      arryPieceImg[pos] = Bitmap.createBitmap(mBitmap, 
      c * mPieceWidth, r * mPieceHeight, 
      mPieceWidth, mPieceHeight); 

      arryIsPieceLocked[pos] = false; 
      arryPieceEndPos[c][r] = pos; 
      pos++; 
     } 
    } 

然后我用一个辅助方法,口罩适用于每一块

private Bitmap maskMethod(Bitmap bmpOriginal, Bitmap bmpMask) { 

    // adjust mask bitmap if size is not the size of the puzzle piece 
    if (bmpMask.getHeight() != mPieceHeight || 
     bmpMask.getWidth() != mPieceWidth) { 
     Log.e("TEST", "Resize Error :: H (mask): " + bmpMask.getHeight() + " // W (mask): " + 
      bmpMask.getWidth()); 
     Log.d("TEST", "Resize Error :: H (norm): " + mPieceHeight + " // W (norm): " + 
      mPieceWidth); 

    } 

    Canvas canvas = new Canvas(); 
    Bitmap combine = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(), Bitmap.Config.ARGB_8888); 
    canvas.setBitmap(combine); 
    Paint paint = new Paint(); 
    paint.setFilterBitmap(false); 

    canvas.drawBitmap(bmpOriginal, 0, 0, paint); 
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); 
    canvas.drawBitmap(bmpMask, 0, 0, paint); 
    paint.setXfermode(null); 

    return combine; 
} 

我看到这个帖子>http://java.dzone.com/news/connect-pictures-android连接拼在一起,但是,这不走了以编程方式生成部件,无需掩码。任何人都可以提供如何完成这些代码的例子吗?我唯一的线索是我应该使用Path,但是,我仍然不确定如何。提前致谢!

回答

4

拼图是一个非常复杂的视图来创建,但我可以帮助您了解如何使用路径。以下是开发者网站的链接:http://developer.android.com/reference/android/graphics/Path.html

查看此链接。我为你做了一件小事。你需要弄清楚的一件事是如何从道路上划出一个小圈子,我不知道。我认为你必须考虑裁剪才能让你的路径沿着一个圆圈(你也可以在裁片之外做裁剪来创建圆圈,我之前没有裁剪过)。

private Bitmap getPuzzleBitmap(Bitmap bitmap) 
{ 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int color = 0xff424242; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 

    calculatePuzzlePath(bitmap.getWidth(), bitmap.getHeight()); 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 
    canvas.drawPath(puzzlePath, paint); 

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 

    return output; 
} 

private void calculatePuzzlePath(int width, int height) 
{ 
    float radius = (height/2) - 5; 
    float smallRadius = radius/3; 
    radius -= smallRadius * 2; 
    float centerX = width/2; 
    float centerY = height/2; 
    puzzlePath = new Path(); 
    // Bottom right 
    puzzlePath.moveTo(centerX + radius, centerY + radius); 
    // Top right 
    puzzlePath.lineTo(centerX + radius, centerY - radius); 
    // Center top 
    puzzlePath.lineTo(centerX, centerY - radius); 
    // Add outside circle to center top 
    puzzlePath.addCircle(centerX, centerY - radius - ((radius/3)/2), radius/3, Path.Direction.CCW); 

    // Top left 
    puzzlePath.lineTo(centerX - radius, centerY - radius); 
    // Bottom left 
    puzzlePath.lineTo(centerX - radius, centerY + radius); 
    //Bottom right 
    puzzlePath.lineTo(centerX + radius, centerY + radius); 
} 

我希望这是足够的开始与此。

祝你好运!

+0

谢谢你的回答。哇,为了获得整个拼图的形状,并且看起来很好,这是非常复杂的。我现在遇到的问题是从整个图像创建碎片,而不仅仅是图像的一部分,因为我需要能够移动这些碎片。一般来说,对于android来说,使用mask是更好的选择? – portfoliobuilder 2015-03-17 23:47:00

+0

您可以使用此http://stackoverflow.com/a/8180576/2767703获取位图的一部分。我不确定哪种更好,但使用面罩接缝更容易。如果您在使用路径等方面遇到很多问题,如果它可以很好地适用于遮罩,则可能不值得一提 – 2015-03-18 07:07:31

相关问题