Android GridView multiple-selection effect of instance code _android

Source: Internet
Author: User
Tags static class

The specific code is as follows:

Main.xml

Copy Code code as follows:

<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

Copy Code code as follows:

<?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
Copy Code code as follows:

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

Copy Code code as follows:

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;

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;
}

The main thing is the following code.
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); The spacing of the first few pictures from each side
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 to LA; Returns the superimposed graph
}
public void chicestate (int post)
{
Ischice[post]=ischice[post]==true?false:true;
This.notifydatasetchanged ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.