1. activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
tools:context=".MainActivity" >
<fragment
android:id="@+id/fragment1"
android:name="com.example.app.GridViewFragement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/res_but"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/fragment1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="ReStart" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/res_but"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rad_btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="3x3" />
<RadioButton
android:id="@+id/rad_btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4x4" />
<RadioButton
android:id="@+id/rad_btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5x5" />
<RadioButton
android:id="@+id/rad_btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6x6" />
</RadioGroup>
</RelativeLayout>
2. gridview_fragement.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView_fr"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</GridView>
3. ImageViewAdapter.java
import java.util.ArrayList;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
//import android.util.DisplayMetrics;
public class ImageViewAdapter extends BaseAdapter {
private Context context;
private ArrayList<ImageView> arraylist_imageview;
private int imageviewwidth, imageviewheight;
public ImageViewAdapter(Context context,
ArrayList<ImageView> arraylist_imageview) {
this.context = context;
this.arraylist_imageview = arraylist_imageview;
}
@Override
public int getCount() {
return arraylist_imageview.size();
}
@Override
public Object getItem(int position) {
return arraylist_imageview.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = arraylist_imageview.get(position);
convertView.setLayoutParams(new AbsListView.LayoutParams(
imageviewwidth, imageviewheight));
return convertView;
}
public void setImageViewWidth(int width) {
this.imageviewwidth = width;
}
public void setImageViewHeight(int height) {
this.imageviewheight = height;
}
}
4. GridViewFragement.java
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class GridViewFragement extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.gridview_fragement, container, false);
}
}
5. MainActivity.java
import java.util.ArrayList;
import java.util.Collections;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.GradientDrawable.Orientation;
public class MainActivity extends Activity {
private GridView gridview;
private ArrayList<ImageView> arraylist_imageview;
private ArrayList<Integer> shuffle_arraylist;
private ImageViewAdapter imageViewadapter;
private ImageView[] imageviewactivity;
private Button restart;
private RadioGroup radio_group_imageview;
private int[] imageViewinitid;
private int[] imageViewshuffleid;
private int gridViewbackgroundcolor = Color.WHITE;
private int imageview = R.drawable.koala;
private int check = R.id.rad_btn3;
private int imageViewsize3_3;
private int imageViewsize4_4;
private int imageViewsize5_5;
private int imageViewsize6_6;
private int row = 3;
private int col = 3;
private ImageView answerimageView;
private ImageView magicimageView;
private ImageView selectimageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageViewsize3_3 = (int) ((getResources().getDisplayMetrics().widthPixels - (20 * getResources()
.getDisplayMetrics().density)) / 3);
imageViewsize4_4 = (int) ((getResources().getDisplayMetrics().widthPixels - (20 * getResources()
.getDisplayMetrics().density)) / 4);
imageViewsize5_5 = (int) ((getResources().getDisplayMetrics().widthPixels - (20 * getResources()
.getDisplayMetrics().density)) / 5);
imageViewsize6_6 = (int) ((getResources().getDisplayMetrics().widthPixels - (20 * getResources()
.getDisplayMetrics().density)) / 6);
initGridViewUI(3, 9, imageViewsize3_3, gridViewbackgroundcolor);
setImageViewInitId();
setImageViewShuffleId();
drawActivityBackground();
restart = (Button) this.findViewById(R.id.res_but);
radio_group_imageview = (RadioGroup) findViewById(R.id.radioGroup);
radio_group_imageview.check(R.id.rad_btn3);
radio_group_imageview
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radiogroup,
int checkedId) {
check = radiogroup.getCheckedRadioButtonId();
switch (check) {
case R.id.rad_btn3:
row = 3;
col = 3;
initGridViewUI(3, 9, imageViewsize3_3,
gridViewbackgroundcolor);
setImageViewInitId();
setImageViewShuffleId();
break;
case R.id.rad_btn4:
row = 4;
col = 4;
initGridViewUI(4, 16, imageViewsize4_4,
gridViewbackgroundcolor);
setImageViewInitId();
setImageViewShuffleId();
break;
case R.id.rad_btn5:
row = 5;
col = 5;
initGridViewUI(5, 25, imageViewsize5_5,
gridViewbackgroundcolor);
setImageViewInitId();
setImageViewShuffleId();
break;
case R.id.rad_btn6:
row = 6;
col = 6;
initGridViewUI(6, 36, imageViewsize6_6,
gridViewbackgroundcolor);
setImageViewInitId();
setImageViewShuffleId();
break;
}
}
});
restart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
switch (check) {
case R.id.rad_btn3:
row = 3;
col = 3;
initGridViewUI(3, 9, imageViewsize3_3,
gridViewbackgroundcolor);
setImageViewInitId();
setImageViewShuffleId();
break;
case R.id.rad_btn4:
row = 4;
col = 4;
initGridViewUI(4, 16, imageViewsize4_4,
gridViewbackgroundcolor);
setImageViewInitId();
setImageViewShuffleId();
break;
case R.id.rad_btn5:
row = 5;
col = 5;
initGridViewUI(5, 25, imageViewsize5_5,
gridViewbackgroundcolor);
setImageViewInitId();
setImageViewShuffleId();
break;
case R.id.rad_btn6:
row = 6;
col = 6;
initGridViewUI(6, 36, imageViewsize6_6,
gridViewbackgroundcolor);
setImageViewInitId();
setImageViewShuffleId();
break;
}
}
});
}
public void initGridViewUI(int col, int count, int size,
int gridview_background) {
initImageViewUI(col, count, size);
gridview = (GridView) this.findViewById(R.id.fragment1);
gridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
gridview.setNumColumns(col);
gridview.setLayoutParams(new RelativeLayout.LayoutParams(col * size,
col * size));
gridview.setBackgroundColor(gridview_background);
imageViewadapter = new ImageViewAdapter(this, arraylist_imageview);
imageViewadapter.setImageViewWidth(size);
imageViewadapter.setImageViewHeight(size);
gridview.setAdapter(imageViewadapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view,
int position, long arg3) {
selectimageView = (ImageView) view; // 獲取被點擊的按鈕
boolean isclosed = false;
// 判斷點選圖形是否相鄰
isclosed = isClosed(selectimageView.getId(),
magicimageView.getId());
if (isclosed) {
int select = selectimageView.getId();
int magic = magicimageView.getId();
swap(selectimageView, magicimageView);
completePuzzle(select, magic);
}
}
});
}
public void initImageViewUI(int divisor, int number, int size) {
int count = -1;
int proportion = size * divisor;
arraylist_imageview = new ArrayList<ImageView>();
setShuffleArrayList(number);
Bitmap mBitmap = Bitmap.createScaledBitmap(
((BitmapDrawable) getResources().getDrawable(imageview))
.getBitmap(), proportion, proportion, true);
imageviewactivity = new ImageView[number];
for (int i = 0; i < imageviewactivity.length; i++) {
imageviewactivity[i] = new ImageView(this);
arraylist_imageview.add(imageviewactivity[i]);
imageviewactivity[i].setId(i);
}
for (int h = 0; h < proportion; h += proportion / divisor) {
for (int w = 0; w < proportion; w += proportion / divisor) {
imageviewactivity[shuffle_arraylist.get(++count)]
.setImageBitmap(Bitmap.createBitmap(mBitmap, w, h,
(proportion / divisor), (proportion / divisor),
null, true));
}
}
magicimageView = imageviewactivity[shuffle_arraylist.get(count)];
imageviewactivity[shuffle_arraylist.get(count)].setImageBitmap(null);
answerimageView = new ImageView(this);
answerimageView.setImageBitmap(mBitmap);
}
public void setImageViewInitId() {
imageViewinitid = new int[imageviewactivity.length];
for (int i = 0; i < imageViewinitid.length; i++) {
imageViewinitid[i] = i;
}
}
public void setImageViewShuffleId() {
imageViewshuffleid = new int[imageviewactivity.length];
for (int i = 0; i < imageViewinitid.length; i++) {
imageViewshuffleid[shuffle_arraylist.get(i)] = i;
}
}
public void setShuffleArrayList(int number) {
shuffle_arraylist = new ArrayList<Integer>();
for (int shuffle = 0; shuffle < number; shuffle++) {
shuffle_arraylist.add(shuffle);
}
Collections.shuffle(shuffle_arraylist);
}
private boolean isClosed(int location, int location2) {
if (Math.abs(location / col - location2 / col) == 1) {
if (location % col == location2 % col) {
return true;
}
} else if (location / row == location2 / row) {
if (Math.abs(location % row - location2 % row) == 1) {
return true;
}
}
return false;
}
private void swap(ImageView v1, ImageView v2) {
// 交換兩個陣列元素的圖片
v2.setImageDrawable(v1.getDrawable());
v1.setImageBitmap(null);
// 找出圖片空白那個陣列元素的編號
magicimageView = v1;
}
public void completePuzzle(int selectid, int magicbuttonid) {
int result = 0;
int select = imageViewshuffleid[selectid];
imageViewshuffleid[selectid] = imageViewshuffleid[magicbuttonid];
imageViewshuffleid[magicbuttonid] = select;
for (int i = 0; i < imageViewshuffleid.length; i++) {
if (imageViewinitid[i] == imageViewshuffleid[i]) {
result++;
}
}
if (result == imageViewinitid.length) {
gridview.setOnItemClickListener(null);
gridview.setBackgroundDrawable(answerimageView.getDrawable());
Toast.makeText(this, "finish", Toast.LENGTH_SHORT).show();
}
}
public void drawActivityBackground() {
GradientDrawable grad = new GradientDrawable(Orientation.TL_BR,
new int[] { Color.rgb(0, 0, 127), Color.rgb(0, 0, 255),
Color.rgb(127, 0, 255), Color.rgb(127, 127, 255),
Color.rgb(127, 255, 255), Color.rgb(255, 255, 255) });
this.getWindow().setBackgroundDrawable(grad);
}
}
6. 註備 : 圖片請自備
沒有留言:
張貼留言