Android 如何在不使用遮罩的情况下创建拼图?
我正在尝试创建一个拼图游戏,我想知道不使用面具创建拼图块的替代方法。目前,我有一个完整的图像拼图块,把图像分成四块(假设拼图是2x2),然后存储和应用一个面具到每一块。它看起来像下图Android 如何在不使用遮罩的情况下创建拼图?,android,image-processing,bitmap,Android,Image Processing,Bitmap,我正在尝试创建一个拼图游戏,我想知道不使用面具创建拼图块的替代方法。目前,我有一个完整的图像拼图块,把图像分成四块(假设拼图是2x2),然后存储和应用一个面具到每一块。它看起来像下图 // create standard puzzle pieces arryPieceEndPos = new int[mCols][mRows]; arryPieceImg = new Bitmap[mCols * mRows]; arryIsPieceLocked = new bo
// 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++;
}
}
我看到了这篇文章>,用于将各个部分连接在一起,但是,这并没有涉及到在没有遮罩的情况下以编程方式生成各个部分。有人能提供如何实现这一点的代码示例吗?我唯一的线索是我应该使用Path,但是,我仍然不知道如何使用。提前谢谢 拼图是一个非常复杂的视图,但我可以帮助您了解如何使用路径。以下是开发人员网站的链接: 查看此链接。我做了一件小事让你开始。你需要弄清楚的一件事是如何在路径上切出一个小圆圈,我不知道。我认为你必须研究裁剪,使你的路径沿着一个圆(你也可以在工件外创建一个圆,我只是以前没有做过裁剪) 我希望这足以开始这项工作
祝你好运 谢谢你的回答。哇,要得到一个完整的拼图块形状并让它看起来很好是非常复杂的。我现在的问题是从整个图像中创建片段,而不仅仅是图像的一部分,因为我需要能够移动这些片段等等。一般来说,对于android来说,使用掩码是更好的选择吗?你可以使用它来获取位图的一部分。我不知道什么更好,但使用面膜接缝要容易得多。如果您在使用路径等方面有很多问题,那么如果它与mask配合良好,可能就不值得这么麻烦了
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;
}
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);
}