GridView示例2(自动增长)
16lz
2021-01-23
第一步:布局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 stubif (drawable == tv_gray_draw) {return getRED_BALL_COUNT();} else {return 0;}}// 获得当前选项public Object getItem(int item) {return item;}// 获得当前选项IDpublic 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;}}
更多相关文章
- Android实现图片缩放与旋转
- Android Studio点击按钮更换背景图片
- android图片放大 缩小 旋转
- Android 怎么给图片添加一个边框
- Android漏洞——将Android恶意代码隐藏在图片中
- Android imageView图片按比例缩放
- android中使用afinal一行代码显示网络图片
- Android——获取网络图片