android – 如何在不使用面具的情况下创建拼图游戏?

前端之家收集整理的这篇文章主要介绍了android – 如何在不使用面具的情况下创建拼图游戏?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在尝试创建一个拼图益智游戏,我想知道在不使用面具的情况下创建拼图的替代方法.目前我通过拍摄完整的图像拼图碎片,将图像分成四个部分(假设拼图是2×2),然后存储并对每个部分应用遮罩.它看起来像下面

  1. // create standard puzzle pieces
  2. arryPieceEndPos = new int[mCols][mRows];
  3. arryPieceImg = new Bitmap[mCols * mRows];
  4. arryIsPieceLocked = new boolean[mCols * mRows];
  5. int pos = 0;
  6. for (int c = 0; c < mCols; c++) {
  7. for (int r = 0; r < mRows; r++) {
  8. arryPieceImg[pos] = Bitmap.createBitmap(mBitmap,c * mPieceWidth,r * mPieceHeight,mPieceWidth,mPieceHeight);
  9. arryIsPieceLocked[pos] = false;
  10. arryPieceEndPos[c][r] = pos;
  11. pos++;
  12. }
  13. }

然后我使用辅助方法将遮罩应用于每个部分

  1. private Bitmap maskMethod(Bitmap bmpOriginal,Bitmap bmpMask) {
  2. // adjust mask bitmap if size is not the size of the puzzle piece
  3. if (bmpMask.getHeight() != mPieceHeight ||
  4. bmpMask.getWidth() != mPieceWidth) {
  5. Log.e("TEST","Resize Error :: H (mask): " + bmpMask.getHeight() + " // W (mask): " +
  6. bmpMask.getWidth());
  7. Log.d("TEST","Resize Error :: H (norm): " + mPieceHeight + " // W (norm): " +
  8. mPieceWidth);
  9. }
  10. Canvas canvas = new Canvas();
  11. Bitmap combine = Bitmap.createBitmap(bmpOriginal.getWidth(),bmpOriginal.getHeight(),Bitmap.Config.ARGB_8888);
  12. canvas.setBitmap(combine);
  13. Paint paint = new Paint();
  14. paint.setFilterBitmap(false);
  15. canvas.drawBitmap(bmpOriginal,paint);
  16. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
  17. canvas.drawBitmap(bmpMask,paint);
  18. paint.setXfermode(null);
  19. return combine;
  20. }

我看过这篇文章>但是,如果没有掩码,这不会以编程方式生成片段.任何人都可以提供如何实现这一目标的代码示例吗?我唯一的线索是我应该使用Path,但是,我仍然不确定如何.提前致谢!

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

看看这个链接.我为你开始做了一件小事.你需要弄清楚的一件事是如何从路径中切出一个小圆圈,这是我不知道的.我认为你必须研究剪裁以使你的路径遵循一个圆圈(你也可以做剪辑以创建一个圆形外面的圆圈,我之前没有做过剪裁).

  1. private Bitmap getPuzzleBitmap(Bitmap bitmap)
  2. {
  3. Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
  4. Canvas canvas = new Canvas(output);
  5. final int color = 0xff424242;
  6. final Paint paint = new Paint();
  7. final Rect rect = new Rect(0,bitmap.getWidth(),bitmap.getHeight());
  8. calculatePuzzlePath(bitmap.getWidth(),bitmap.getHeight());
  9. paint.setAntiAlias(true);
  10. canvas.drawARGB(0,0);
  11. paint.setColor(color);
  12. canvas.drawPath(puzzlePath,paint);
  13. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  14. canvas.drawBitmap(bitmap,rect,paint);
  15. return output;
  16. }
  17. private void calculatePuzzlePath(int width,int height)
  18. {
  19. float radius = (height / 2) - 5;
  20. float smallRadius = radius / 3;
  21. radius -= smallRadius * 2;
  22. float centerX = width/2;
  23. float centerY = height/2;
  24. puzzlePath = new Path();
  25. // Bottom right
  26. puzzlePath.moveTo(centerX + radius,centerY + radius);
  27. // Top right
  28. puzzlePath.lineTo(centerX + radius,centerY - radius);
  29. // Center top
  30. puzzlePath.lineTo(centerX,centerY - radius);
  31. // Add outside circle to center top
  32. puzzlePath.addCircle(centerX,centerY - radius - ((radius / 3) / 2),radius / 3,Path.Direction.CCW);
  33. // Top left
  34. puzzlePath.lineTo(centerX - radius,centerY - radius);
  35. // Bottom left
  36. puzzlePath.lineTo(centerX - radius,centerY + radius);
  37. //Bottom right
  38. puzzlePath.lineTo(centerX + radius,centerY + radius);
  39. }

我希望这足以开始这个.

祝好运!

猜你在找的Android相关文章