随时随地选择自己喜欢的图片玩拼图益智游戏
本教程实现拼图,并且可以选择图片的来源,通过摄像头照相获得,也可以通过本地文件夹获得。
废话不多说,请看下面主要代码讲述:
为了使各种大小的图片都能使用,需要对图片进行缩放:
// 缩放图片
public void scaleImage(float w) {
int bw = bmpResources.getWidth();
int bh = bmpResources.getHeight();
float scaleW = w / bw;
float scaleH = w / bh;
Matrix matrix = new Matrix();
if (scaleH > scaleW) {
matrix.postScale(scaleW, scaleW);
} else {
matrix.postScale(scaleH, scaleH);
}
bmpResources = Bitmap.createBitmap(bmpResources, 0, 0, bw, bh, matrix,
true);
}
一块完整的图片需要将其分成n*n等份:
// 将图片分割
private void initPuzzleLayout() {
setPatchHightAndWidth();
puzzleId = new ArrayList<Map<String, Object>>();
puzzleIndex = new int[rows * cols];
imageOrder = new int[rows * cols];
// 保存碎片的ID
for (int i = 0; i < rows; i++) {
Map<String, Object> map = new HashMap<String, Object>();
Bitmap bmp;
for (int j = 0; j < cols; j++) {
map = new HashMap<String, Object>();
bmp = Bitmap.createBitmap(bmpResources, j * ConstValue.width, i
* ConstValue.height, ConstValue.width,
ConstValue.height);
map.put("Id", (i * rows + j));
puzzleIndex[i * rows + j] = i * rows + j;
map.put("image", bmp);
puzzleId.add(map);
}
}
for (int i = 0; i < imageOrder.length; i++) {
imageOrder[i] = puzzleIndex[i];
}
}
对用户ontouch事件的处理:
@Override
public boolean onTouch(View v, MotionEvent event) {
if (touchable) {
if (MotionEvent.ACTION_DOWN == event.getAction()) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (v == imagesArray[i][j]) {
first = second;
second = System.currentTimeMillis();
if (second - first <= 500) {
// 第一次时间、第二次时间清空
first = 0;
second = 0;
imagesArray[blankRow][blankCol]
.setImageBitmap(btmpArray[i][j]);
imagesArray[i][j]
.setImageBitmap(btmpArray[blankRow][blankCol]);
Bitmap tempBmp = btmpArray[blankRow][blankCol];
btmpArray[blankRow][blankCol] = btmpArray[i][j];
btmpArray[i][j] = tempBmp;
int tempInt = result[i * rows + j];
result[i * rows + j] = result[blankRow * rows
+ blankCol];
result[blankRow * rows + blankCol] = tempInt;
isWin = true;
isComplete();
}
blankRow = i;
blankCol = j;
}
}
}
}
}
return false;
}
当用户看到自己喜欢的一幕,想拍摄下来玩拼图,调用摄像头代码如下:
Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mContext.startActivityForResult(intent1, 1);
调用本地文件夹,浏览本地文件选择图片:
// 初始化路径列表
private void initList()
{
fileList = new ArrayList<Folder>();
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
{
// 存在SD卡
File path = Environment.getExternalStorageDirectory();
path.getPath();
sdPath = path.getPath();
Folder sdFolder = new Folder();
sdFolder.setFileName(path.getName());
sdFolder.setFilePath(path.getPath());
fileList.add(sdFolder);
}
// 创建 工程目录文件
fileList = FileManager.getFileList("/");
// 初始化适配器
filePathAdapter = new FilePathAdapter(this, fileList);
((FilePathAdapter) filePathAdapter).superListView = null;
fileListView.setAdapter(filePathAdapter);
MyListView.superAdapter = filePathAdapter;
filePath = null;
}
// 初始化界面元件
private void initialView()
{
title_path = (TextView) findViewById(R.id.titlePath);
backPath = (ImageView) findViewById(R.id.infoback01);
backPath.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (filePath != null && filePath.lastIndexOf("/") > 0
&& !filePath.equals("/data/data/com.WeatherDemo3")
&& !filePath.equals(sdPath))
{
String fatherPath = filePath.substring(0, filePath
.lastIndexOf("/"));
curAdpter.fileList = FileManager.getFileList(fatherPath);
curAdpter.notifyDataSetChanged();
filePath = fatherPath;
title_path.setText(filePath);
}
else if (filePath != null && filePath.lastIndexOf("/") == 0
&& !filePath.equals("/data/data/com.WeatherDemo3")
&& !filePath.equals(sdPath))
{
initList();
filePathAdapter.notifyDataSetChanged();
title_path.setText("/");
filePath = null;
}
else
{
Bundle bundle = new Bundle();
Intent it = new Intent();
bundle.putString("newPath", "no");
// it.putExtra("added", "no");
it.putExtras(bundle);
setResult(2, it);
finish();
}
}
});
ok = (ImageView) findViewById(R.id.infopreview01);
ok.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (filePath != null)
{
Intent intent = new Intent();
intent.putExtra("OpenFile", filePath);
setResult(0, intent);
String str = filePath.substring(
filePath.lastIndexOf(".") + 1, filePath.length());
if (str.equals("jpg") || str.equals("png"))
{
showDialog(1);
}
else
{
Toast.makeText(FileBrowserActivity.this, "请选择正确的图片格式",
Toast.LENGTH_SHORT).show();
}
}
else
{
setResult(2);
}
}
});
fileListView = (ListView) findViewById(R.id.file_list);
}
由于附件过大,上传不成功,所以我删去了图片声音文件,但是其中包含所有代码部分,需要的地方请参考,谢谢各位支持!
https://files.cnblogs.com/feifei1010/PuzzleAnimal.rar
欢迎热爱安卓开发的朋友加入群一起交流进步。南京群 220818530,成都群 252743807,西安群252746034,杭州群253603803
厦门群253604146,湖南群217494504,大连群253672904,青岛群 257925319

浙公网安备 33010602011771号