Android实现GridView中ImageView动态变换的方法
程序员文章站
2023-11-12 09:34:22
本文实例讲述了android实现gridview中imageview动态变换的方法。分享给大家供大家参考。具体如下:
使用yy影音的时候,发现点击gridview的某一个...
本文实例讲述了android实现gridview中imageview动态变换的方法。分享给大家供大家参考。具体如下:
使用yy影音的时候,发现点击gridview的某一个item,item里面的图标会在按下的时候发生变换,变成另外一个图片。
自己写了一个类似的demo,具体步骤如下:
1、创建一个包含grid的acitity
2、创建item.xml 里面包含一个imageview和一个textview
3、自定义一个adapter,从baseadapter继承
4、在getview中为每个imageview添加setontouchlistener
代码入下:
mainactivity.java package com.mygriddemo; import java.util.arraylist; import android.app.activity; import android.content.context; import android.content.intent; import android.content.res.resources; import android.graphics.drawable.drawable; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.motionevent; import android.view.view; import android.view.view.ontouchlistener; import android.view.viewgroup; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.baseadapter; import android.widget.gridview; import android.widget.imagebutton; import android.widget.imageview; import android.widget.textview; import android.widget.toast; public class mainactivity extends activity { /** called when the activity is first created. */ private gridview gv; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); gv = (gridview) findviewbyid(r.id.mygrid); addadapter addadapter = new addadapter(this); gv.setadapter(addadapter); } public class addadapter extends baseadapter { private final layoutinflater minflater; private final arraylist<listitem> mitems = new arraylist<listitem>(); // public static final int item_shortcut = 0; // public static final int item_appwidget = 1; // public static final int item_live_folder = 2; // public static final int item_wallpaper = 3; /** * specific item in our list. */ public class listitem { public final charsequence text; public final drawable image; //public final int actiontag; public final drawable touchimage; //, int actiontag public listitem(resources res, int textresourceid, int imageresourceid,int touchimageresourceid) { text = res.getstring(textresourceid); if (imageresourceid != -1) { image = res.getdrawable(imageresourceid); } else { image = null; } if (touchimageresourceid != -1) { touchimage = res.getdrawable(touchimageresourceid); } else { touchimage = null; } } } public addadapter(mainactivity launcher) { super(); minflater = (layoutinflater) launcher.getsystemservice(context.layout_inflater_service); // create default actions resources res = launcher.getresources(); mitems.add(new listitem(res, r.string.item1, r.drawable.item1, r.drawable.item1back)); mitems.add(new listitem(res, r.string.item2, r.drawable.item2, r.drawable.item2back)); mitems.add(new listitem(res, r.string.item3, r.drawable.item3, r.drawable.item3back)); mitems.add(new listitem(res, r.string.item4, r.drawable.item4, r.drawable.item4back)); } public view getview(int position, view convertview, viewgroup parent) { final listitem item = (listitem) getitem(position); if (convertview == null) { convertview = minflater.inflate(r.layout.grid_item, parent, false); } final textview textview = (textview) convertview;//.findviewbyid(r.id.mygrid); textview.settag(item); textview.settext(item.text); textview.setcompounddrawableswithintrinsicbounds(null, item.image, null, null); textview.setontouchlistener(new ontouchlistener(){ public boolean ontouch(view arg0, motionevent arg1) { // todo auto-generated method stub if (arg1.getaction() == motionevent.action_down){ log.d("weatherwidget", motionevent.action_down+""); textview.setcompounddrawableswithintrinsicbounds(null, item.touchimage, null, null); } else if(arg1.getaction() == motionevent.action_up) { log.d("weatherwidget", arg1.getaction()+""); textview.setcompounddrawableswithintrinsicbounds(null, item.image, null, null); intent i = new intent(mainactivity.this,page1activity.class); startactivity(i); } return true; } }); return convertview; } public int getcount() { return mitems.size(); } public object getitem(int position) { return mitems.get(position); } public long getitemid(int position) { return position; } } }
main.xml代码:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <gridview android:id="@+id/mygrid" android:layout_width="fill_parent" android:layout_height="fill_parent" android:numcolumns="2" android:verticalspacing="10dip" android:horizontalspacing="10dip" android:stretchmode="columnwidth" android:gravity="center" > </gridview> </linearlayout>
griditem.xml代码:
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:minheight="?android:attr/listpreferreditemheight" android:layout_centerhorizontal="true" android:drawablepadding="14dip" android:paddingleft="15dip" android:paddingright="15dip" android:gravity="center_horizontal" />
希望本文所述对大家的android程序设计有所帮助。