gpt4 book ai didi

android - 如何在不使用 mask 的情况下创建拼图 block ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:44:03 24 4
gpt4 key购买 nike

我正在尝试创建一个拼图游戏,我想知道在不使用 mask 的情况下创建拼图的替代方法。目前,我通过拍摄完整图像、将该图像分成四 block (假设拼图是 2x2)然后存储并为每 block 应用蒙版来制作拼图 block 。看起来像下面

    // 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然而,为了将 fragment 连接在一起,这并没有超越以编程方式生成没有掩码的 fragment 。任何人都可以提供如何实现这一点的代码示例吗?我唯一的线索是我应该使用路径,但是,我仍然不确定如何使用。提前致谢!

最佳答案

拼图 block 是一个创建起来相当复杂的 View ,但我可以帮助您了解如何使用路径。这是开发者网站的链接: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);
}

我希望这足以开始。

祝你好运!

关于android - 如何在不使用 mask 的情况下创建拼图 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29039640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com