在原有的基础上添加可选择手机任意一张图片。
1、在MainActivity.java代码中添加多了一个按钮可实现选择手机照片确认返回。在onActivityResult里面添加
gamePintuLayout.bitmap = bitmap;
gamePintuLayout.initBitmap();
gamePintuLayout.initItem();
否则,选择后将不会出现图片。
完整代码:
package wind.com.puzzle;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.display.DisplayManager;
import android.net.Uri;
import android.os.Handler;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private GamePintuLayout gamePintuLayout;
private TextView textView;
private Button button;
static Bitmap bitmap;
static Bitmap bitmap1;
static boolean flag2 = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gamePintuLayout = (GamePintuLayout) findViewById(R.id.id_gameview);
textView = (TextView)findViewById(R.id.text);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
/* 开启Pictures画面Type设定为image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT这个Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片后返回本画面 */
startActivityForResult(intent, 1);
}
});
//每1000毫秒更新一次步数
handler.removeCallbacks(runnable);
handler.postDelayed(runnable, 1000);
}
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
public void run () {
//ImageView image = (ImageView) findViewById(R.id.image);
/* 将Bitmap设定到ImageView */
//image.setImageBitmap(bitmap);
textView.setText("第"+ GamePintuLayout.numguan+"关,您用了 "+GamePintuLayout.number + " 步");
handler.postDelayed(this, 1000);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
// bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
bitmap = MediaStore.Images.Media.getBitmap(cr, uri);
gamePintuLayout.bitmap = bitmap;
gamePintuLayout.initBitmap();
gamePintuLayout.initItem();
/* 将Bitmap设定到ImageView */
} catch (Exception e) {
Log.e("Exception", e.getMessage(), e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
2、在GamePintuLayout.java 中的initBitmap中注释掉
// if (bitmap == null){
// bitmap = MainActivity.bitmap;
// bitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.ac);
// }
3、在onMeasure中将
if (!flag){
Log.d("nsc", "bitmap");
numguan = 1;
initBitmap();
initItem();
}
修改成:
if (!flag && bitmap!=null){
Log.d("nsc", "bitmap");
numguan = 1;
initBitmap();
initItem();
}
否则将会报空指针错误。
4、在权限文件中添加,否则会报oom内存溢出。
android:largeHeap="true"
其他跟之前的基本一样,详细可看上一篇(android 拼图游戏)。
代码下载地址:点击打开链接