package com.example.pintu;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.myzhipai.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class PintuActivity extends Activity {
private ImageView imgv;
private Bitmap bitmap;
private Button xc_but, jiandan_but, zhongji_but, kunnan_but, yanzheng_but;
private GridView gv;
private List
private ImageSplitter imagesplittter;
int size = 3;
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int i = (Integer) msg.obj;
switch (i) {
case 1:
BitmapGoAway(size);
break;
case 2:
imgv.setVisibility(View.VISIBLE);
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pintu);
// 竖屏
if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
InItViews();
}
private void BitmapGoAway(int size) {
imgv.setVisibility(View.VISIBLE);
CountDownTimer count = new CountDownTimer(size * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// getNum.setText(millisUntilFinished/1000+"秒");
}
@Override
public void onFinish() {
// getNum.setClickable(true);
// getNum.setText("获取验证码");
imgv.setVisibility(View.INVISIBLE);
}
};
count.start();
}
private void InItViews() {
// TODO Auto-generated method stub
gv = (GridView) findViewById(R.id.pintu_grod);
imgv = (ImageView) findViewById(R.id.image);
jiandan_but = (Button) findViewById(R.id.pintu_jiandan_but);
zhongji_but = (Button) findViewById(R.id.pintu_zhongji_but);
kunnan_but = (Button) findViewById(R.id.pintu_kunnan_but);
xc_but = (Button) findViewById(R.id.act_xc_but);
yanzheng_but = (Button) findViewById(R.id.act_yanzheng_but);
imagesplittter = new ImageSplitter();
ViewOper();
}
private static final int PHOTO_REQUEST_CAMER = 1;// 用相机拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
private void ViewOper() {
xc_but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 激活系统图库,选择一张图片
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
// Intent intent = new
// Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
//
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
// intent.putExtra("return-data", true);
// intent.setType("image/*");
// //剪裁
// intent.putExtra("crop", "circleCrop");
// //比例
// intent.putExtra("aspectX",1);
// intent.putExtra("aspectY",1);
// //大小
// intent.putExtra("outputX", 50);
// intent.putExtra("outputY", 50);
// startActivityForResult(intent, 1);
//
//
// startActivityForResult(intent, 1);
}
});
jiandan_but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StartGame(3);
size = 3;
}
});
zhongji_but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StartGame(6);
size = 6;
}
});
kunnan_but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
StartGame(9);
size = 9;
}
});
yanzheng_but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
gameover = 0;
for (int i = 0; i < data.size() - 1; i++) {
if (data.get(i + 1).index - data.get(i).index != 1) {
Toast.makeText(PintuActivity.this, "验证失败", Toast.LENGTH_SHORT).show();
Message message = handler.obtainMessage();
message.obj = 1;
handler.sendMessage(message);
return;
} else {
gameover++;
if (gameover == data.size() - 1) {
Toast.makeText(PintuActivity.this, "恭喜你", Toast.LENGTH_SHORT).show();
}
}
}
}
});
}
/**
* 游戏结束标识
*/
int gameover = 0;
ActMainGvAdapter actMainGvAdapter;
private void StartGame(int size) {
if (bitmap == null) {
Toast.makeText(PintuActivity.this, "请选择图片", Toast.LENGTH_SHORT).show();
return;
}
Message message = handler.obtainMessage();
message.obj = 2;
handler.sendMessage(message);
gameover = 0;
data = createRandomList(imagesplittter.split(bitmap, size, size, size), size * size);
// data = imagesplittter.split(bitmap, size, size,size);
// 给GridView 设置宽高
LayoutParams params = gv.getLayoutParams();
params.height = bitmap.getWidth() * 2;
params.width = (int) (bitmap.getHeight() * 2);
gv.setLayoutParams(params);
// 给GridView 设置adapter
actMainGvAdapter = new ActMainGvAdapter(PintuActivity.this, data);
// 给GridView设置列数
gv.setNumColumns(size);
// gv.setBackgroundColor(Color.RED);
gv.setBackgroundColor(Color.parseColor("#ff0000"));
gv.setAdapter(actMainGvAdapter);
CountDownTimer count = new CountDownTimer(size * 3 * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
// TODO Auto-generated method stub
// getNum.setText(millisUntilFinished/1000+"秒");
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
// getNum.setClickable(true);
// getNum.setText("获取验证码");
imgv.setVisibility(View.INVISIBLE);
}
};
count.start();
MovePic(size);
}
/**
* 是否是第一次点击 true 是第一次,false 不是第一次点击
*/
boolean firstclick = true;
/**
* 第一次点击的item id
*/
int startid;
/**
* 第二次点击的item id
*/
int endid;
/**
* 移动图片
*
* @param size
* 现在是 那个游戏模式 3、6、9
*/
public void MovePic(final int size) {
gv.setOnItemClickListener(new OnItemClickListener() {
@SuppressLint("NewApi")
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
// TODO Auto-generated method stub
view.setAnimation(AnimationManger.MakeLearAnimation());
if (firstclick) {
startid = position;
firstclick = false;
} else {
firstclick = true;
endid = position;
// 如果是前后点击
if (startid - endid == 1 || endid - startid == 1) {
// 将list中 指定的元素对换位置
Collections.swap(data, startid, endid);
actMainGvAdapter.notifyDataSetChanged();
}
// 如果是上下点击
if (startid - endid == size || endid - startid == size) {
// 将list中 指定的元素对换位置
Collections.swap(data, startid, endid);
actMainGvAdapter.notifyDataSetChanged();
}
}
}
});
}
/**
* 将list集合内容打散
*
* @param list
* 要打散的list
* @param n
* 要打散前多少个
* @return 返回打散后的list
*/
private List
Map
List
while (map.size() < n) {
int random = (int) (Math.random() * list.size());
if (!map.containsKey(random)) {
map.put(random, "");
listNew.add(list.get(random));
}
}
return listNew;
}
/*
* 剪切图片
*/
private void crop(Uri uri) {
// 看到有人问裁剪的路径
// 获取系统时间 然后将裁剪后的图片保存至指定的文件夹
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
String address = sDateFormat.format(new java.util.Date());
Uri imageUri = Uri.parse("aaaaa/" + address + ".JPEG");
// 裁剪图片意图
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪后输出图片的尺寸大小
intent.putExtra("outputX", 486);
intent.putExtra("outputY", 486);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);// 输出路径
intent.putExtra("outputFormat", "JPEG");// 图片格式
intent.putExtra("noFaceDetection", false);// 取消人脸识别 true 取消, false 不取消
intent.putExtra("return-data", true);
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHOTO_REQUEST_GALLERY) {
// 从相册返回的数据
if (data != null) {
// 得到图片的全路径
Uri uri = data.getData();
crop(uri);
}
} else if (requestCode == PHOTO_REQUEST_CUT) {
// 从剪切图片返回的数据
if (data != null) {
bitmap = data.getParcelableExtra("data");
imgv.setImageBitmap(bitmap);
// imgv.setImageBitmap(bitmap);
StartGame(3);
size = 3;
}
}
// super.onActivityResult(requestCode, resultCode, data);
}
}
package com.example.pintu;
import java.util.List;
import com.example.myzhipai.R;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ActMainGvAdapter extends BaseAdapter{
LayoutInflater inflater;
Activity activity;
List
public ActMainGvAdapter(Activity act,List
this.inflater = LayoutInflater.from(act);
activity=act;
this.gvImgs=gvImgs;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return gvImgs.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return gvImgs.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
ImageView iv;
v = inflater.inflate(R.layout.act_gv_item, null);
iv = (ImageView) v.findViewById(R.id.act_gv_item_imag);
//
LayoutParams params = iv.getLayoutParams();
params.height=gvImgs.get(position).bitmap.getWidth()*2;
params.width =gvImgs.get(position).bitmap.getHeight()*2;
iv.setLayoutParams(params);
iv.setImageBitmap(gvImgs.get(position).bitmap);//setBackgroundResource(gvImgs.get(position));
return v;
}
}
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_height="83.3dp"
android:src="@drawable/ic_launcher"
android:scaleType="fitXY"
android:id="@+id/act_gv_item_imag"
/>
>