第一步:布局xml
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@drawable/redball_ground">
<GridView
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:padding="10dip" android:verticalSpacing="10dip"
android:horizontalSpacing="10dip" android:numColumns="auto_fit"
android:columnWidth="60dip" android:stretchMode="columnWidth"
android:gravity="center" android:id="@+id/grid"/>
</LinearLayout>
第二步:定义图片的个数以及其他
// --点击次数 集合
private ArrayList<String> redBallList = new ArrayList<String>();
private GridView gv_red_ball;
private String FirstGV = "REDBALL";
// --红色球
int RED_BALL_COUNT = 33;// 球的个数
int RED_LINE_COUNT = 0;
private Drawable tv_gray_draw;
private Drawable tv_red_draw;
private Bitmap orgimg;
private int width;// 获取原始图片宽度
第三步:创建Activity
①:设置获取屏幕宽度
// 屏幕宽度
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenwidth = dm.widthPixels;
②:加载原始图片,获取尺寸
// 加载原始图片
orgimg = BitmapFactory.decodeResource(getResources(),
R.drawable.red_ball);// 将图片解析成二进制,getResources()是加载资源文件
width = orgimg.getWidth();
③:设置每行图片个数
RED_LINE_COUNT = screenwidth / width;
④:设置列数
gv_red_ball.setNumColumns(RED_LINE_COUNT);
gv_red_ball.setOnItemClickListener(new GridItemClickListener(FirstGV,
RED_BALL_COUNT));
gv_red_ball.setAdapter(new BallAdapter(this, tv_gray_draw));
第四:创建所需类OnItemClickListener
class GridItemClickListener implements OnItemClickListener {
String ballColor;
int ballCount;
public GridItemClickListener(String ballColor, int ballCount) {
super();
this.ballColor = ballColor;
this.ballCount = ballCount;
}
public void onItemClick(AdapterView<?> arg0, View view, int position,
long arg3) {
TextView tv_ball = (TextView) view;
Drawable current_draw = tv_ball.getBackground();
String ball_num = tv_ball.getText().toString();
if (ballColor.equals(FirstGV)) {
ballCount = RED_BALL_COUNT;
if (current_draw == tv_gray_draw) {
tv_ball.setBackgroundDrawable(tv_red_draw);
tv_ball.setTextColor(Color.WHITE);
} else {
tv_ball.setBackgroundDrawable(tv_gray_draw);
tv_ball.setTextColor(Color.BLACK);
}
if (redBallList.contains(ball_num)) {
redBallList.remove(ball_num);
} else {
redBallList.add(ball_num);
}
}
}
}
第五步:创建构造函数,来继承Baseadapter
public int getRED_BALL_COUNT() {
return RED_BALL_COUNT;
}
public void setRED_BALL_COUNT(int rED_BALL_COUNT) {
RED_BALL_COUNT = rED_BALL_COUNT;
}
// 自定义适配器
class BallAdapter extends BaseAdapter {
// 上下文环境
Context context;
Drawable drawable;
// 构造方法
public BallAdapter(Context context, Drawable drawable) {
super();
this.context = context;
this.drawable = drawable;
}
// 获得数量
public int getCount() {
// TODO Auto-generated method stub
if (drawable == tv_gray_draw) {
return getRED_BALL_COUNT();
} else {
return 0;
}
}
// 获得当前选项
public Object getItem(int item) {
return item;
}
// 获得当前选项ID
public long getItemId(int id) {
return id;
}
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv_ball;
if (convertView == null) {
// 实例化ImageView对象
tv_ball = new TextView(context);
tv_ball.setLayoutParams(new GridView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
} else {
tv_ball = (TextView) convertView;
}
tv_ball.setGravity(Gravity.CENTER);
tv_ball.setTextColor(Color.BLACK);
TextPaint tp = tv_ball.getPaint();
tp.setFakeBoldText(true);
tv_ball.setBackgroundDrawable(drawable);
tv_ball.setText((position + 1) + "");
return tv_ball;
}
}