android GridView多选效果的实例代码
具体代码如下:
main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".mainactivity" >
<gridview
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/grid"
android:verticalspacing="3dp"
android:horizontalspacing="3dp"
android:numcolumns="3"
></gridview>
</linearlayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<imageview
android:scaletype="fitxy"
android:padding="3dp"
android:layout_height="70dp"
android:layout_width="70dp"
android:id="@+id/image_item"
/>
</linearlayout>
mainactivity.java
package com.imageview;
import android.os.bundle;
import android.app.activity;
import android.view.view;
import android.widget.adapterview;
import android.widget.gridview;
import android.widget.adapterview.onitemclicklistener;
public class mainactivity extends activity {
private adpter adpter;
private gridview gridview;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
int [] image={r.drawable.dog1,r.drawable.m2,r.drawable.m3,r.drawable.m4,r.drawable.m5,r.drawable.dog2};
adpter=new adpter(image, this);
gridview=(gridview) findviewbyid(r.id.grid);
gridview.setadapter(adpter);
gridview.setonitemclicklistener(new onitemclicklistener() {
@override
public void onitemclick(adapterview<?> arg0, view arg1,
int position, long arg3) {
adpter.chicestate(position);
}
});
}
}
adpter.java
package com.imageview;
import android.content.context;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.graphics.drawable.bitmapdrawable;
import android.graphics.drawable.drawable;
import android.graphics.drawable.layerdrawable;
import android.util.log;
import android.view.layoutinflater;
import android.view.view;
import android.view.viewgroup;
import android.widget.baseadapter;
import android.widget.imageview;
public class adpter extends baseadapter {
private int[] image;
private boolean ischice[];
private context context;
public adpter(int[] im, context context) {
this.image = im;
log.i("hck", im.length+"lenght");
ischice=new boolean[im.length];
for (int i = 0; i < im.length; i++) {
ischice[i]=false;
}
this.context = context;
}
@override
public int getcount() {
return image.length;
}
@override
public object getitem(int arg0) {
return image[arg0];
}
@override
public long getitemid(int arg0) {
return arg0;
}
@override
public view getview(int arg0, view arg1, viewgroup arg2) {
view view = arg1;
getview getview=null;
if (view == null) {
view = layoutinflater.from(context).inflate(r.layout.item, null);
getview = new getview();
getview.imageview=(imageview) view.findviewbyid(r.id.image_item);
view.settag(getview);
} else {
getview = (getview) view.gettag();
}
getview.imageview.setimagedrawable(getview(arg0));
return view;
}
static class getview {
imageview imageview;
}
//主要就是下面的代码了
private layerdrawable getview(int post) {
bitmap bitmap = ((bitmapdrawable)context.getresources().getdrawable(image[post])).getbitmap();
bitmap bitmap2=null;
layerdrawable la=null;
if (ischice[post]== true){
bitmap2 = bitmapfactory.decoderesource(context.getresources(),
r.drawable.editable_mode_checked_tag);
}
if (bitmap2!=null) {
drawable[] array = new drawable[2];
array[0] = new bitmapdrawable(bitmap);
array[1] = new bitmapdrawable(bitmap2);
la= new layerdrawable(array);
la.setlayerinset(0, 0, 0, 0, 0); //第几张图离各边的间距
la.setlayerinset(1, 0, 65, 65, 0);
}
else {
drawable[] array = new drawable[1];
array[0] = new bitmapdrawable(bitmap);
la= new layerdrawable(array);
la.setlayerinset(0, 0, 0, 0, 0);
}
return la; // 返回叠加后的图
}
public void chicestate(int post)
{
ischice[post]=ischice[post]==true?false:true;
this.notifydatasetchanged();
}
}