第一步:布局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;
}
}
- 大小: 31.1 KB
分享到:
相关推荐
一个很简单的gridview示例,对初学asp.net有用,希望可以帮助到你
GridView是按照行列的方式来显示内容,一般是用来显示图片
gridview示例,基于 android studio工程,包含按钮,图标等
WPF_Silverlight使用Grid+ScrollViewer自定义GridView示例
C# GridView自动列宽 C# GridView自动列宽 C# GridView自动列宽
GridView的多个使用示例.详细分解.
C# 写的一个简单的 ASP.NET 2.0 GridView 的示例,在示例1的基础上又添加了隐藏列和给ButtonField添加提示窗体的示范。
asp.net中GridView高效分页示例,非使用存储过程,简单易用,效率高。
GridView自动序号 GridView自动序号
GridView 示例(文章的修改,删除等操作并结合了文章的类型[DropDownList控件])
GridView实现自动编号
GridView实用示例源码附加导出Excel功能,GridView的高级应用。
GridView实用示例源码附加导出ExcelGridView实用示例源码附加导出ExcelGridView实用示例源码附加导出ExcelGridView实用示例源码附加导出ExcelGridView实用示例源码附加导出Excel
android 拖拽 gridview拖动+自动移位效果 zaker
Android ScrollView+GridView左右滑动 创建时自动定位滑动到某一项以及点击按钮准确定位到某一项,附带源码,非常适合初学者。
Gridview各种高级用法汇总(含代码示例), 供C#2.0高级用户参考使用
GridView控件示例(微软官方Win8 Metro开发示例)
自制GridView控件示例源码 项目描述 GenericGridView是GridView控件的引申。 在内部GridView控件的ViewState被禁用, 因为作者喜欢管理自己的GridView状态 。 如果你喜欢效率和自由,请不要使用ViewState。 后面的...
三层Gridview分页示例源码 功能介绍: 引用简单的三层架构,并在Gridview控件上显示, 利用AspNetPager控件实现分页功能。
GridView控件中实现自动编号