Bitmap切割,GridView显示,更新

GridView一些属性介绍

android:numColumns="auto_fit" --------每行多少个,这个是自动
android:columnWidth="20dp",----------每列的宽度,也就是Item的宽度
android:stretchMode="columnWidth"------缩放与列宽大小同步
android:verticalSpacing="10dp"----------垂直边距
android:horizontalSpacing="10dp"-------水平边距

思路:

1,在xml文件中定义了一个Button,和一个GridView

2,在MainActivity中的Button点击事件里,点击启动线程,通过Bitmap进行分割。

3,分割完成后通过GridView添加到适配器BaseAdapter里

4,通过消息机制Handler更新UI

5,实现GridView监听方法setOnItemClickListener.实现删除

代码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"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="sendBitmap"
        android:text="点击切割图片" />

    <GridView 
        android:id="@+id/main_gview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
         android:verticalSpacing="3dp"
         android:horizontalSpacing="3dp"
         android:numColumns="4"
        />

   </RelativeLayout> <span style="color:#ff0000;font-weight: bold; ">
    
</span>
MainActivity中代码

package com.example.bitmap;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends Activity implements OnItemClickListener {
	private GridView gv;
	// 定义集合装填图片,数据源
	private List<Bitmap> list;
	// 适配器
	private GridViewAdapter adapter;
	private Handler handle = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 1) {
				// 显示
				gv.setAdapter(adapter);
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// 初始话GritView
		gv = (GridView) findViewById(R.id.main_gview);
		// 调用适配器删除
		gv.setOnItemClickListener(this);
	}

	// 将图片分割并显示到页面上
	public void sendBitmap(View v) {
		new Thread() {
			public void run() {
//				声明集合装填图片,数据源
				list = new ArrayList<Bitmap>();
//				获取图片
				Bitmap bm = BitmapFactory.decodeResource(getResources(),
						R.drawable.mm);
				// 定义切割的宽和高
				int xwidth = 5;
				int yheight = 5;
				// 每份的长度 = 总长度/切割的长度
				int xbm = bm.getWidth() / xwidth;
				int ybm = bm.getHeight() / yheight;
				// 循环遍历
				for (int i = 0; i < xwidth; i++) {
					for (int j = 0; j < yheight; j++) {
						// 切割。异常原因,宽度必须小于图片的宽度
						// java.lang.IllegalArgumentException: x + width must be
						// <= bitmap.width()
						// 需要切割的图片资源、切割起始点的X坐标、切割起始点的Y坐标、切割多宽、切割多高。
						Bitmap newbm = Bitmap.createBitmap(bm, j * xbm,
								i * ybm, xbm, ybm);
						list.add(newbm);
						// 核心代码完毕,传参数传bm,切割高和宽
					}
				}
				adapter = new GridViewAdapter(list, MainActivity.this);

				handle.sendEmptyMessage(1);

			}

		}.start();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// 删除
		list.remove(list.get(position));
//		更新
		adapter.notifyDataSetChanged();
	}

}<span style="color:#ff0000;font-weight: bold; ">
</span>
BaseAdapter适配器里

package com.example.bitmap;

import java.util.List;

import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class GridViewAdapter extends BaseAdapter {

	private List<Bitmap> datas;
	private MainActivity mat;
	
	public GridViewAdapter(List<Bitmap> datas, MainActivity mat) {
		
		this.datas = datas;
		this.mat = mat;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		View v;
		ViewHolder vh;
		if (convertView==null) {
			v = mat.getLayoutInflater().inflate(R.layout.gview_main, null);
			vh = new ViewHolder();
			vh.iv = (ImageView) v.findViewById(R.id.gview_iv);
			v.setTag(vh);
		}else{
			v = convertView;
			vh = (ViewHolder) v.getTag();
		}
//		这里显示的是Bitmap
		vh.iv.setImageBitmap(datas.get(position));
		return v;
	}
	static class ViewHolder{
		ImageView iv;
	}

}
<span style="color:#ff0000;font-weight: bold; ">
</span>
子布局的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ImageView 
        android:id="@+id/gview_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        
        />
</RelativeLayout><strong>
</strong>

你可能感兴趣的:(Bitmap切割,GridView显示,更新)