Android实现GridView中ImageView动态变换的方法

Android实现GridView中ImageView动态变换的方法,第1张

概述本文实例讲述了Android实现GridView中ImageView动态变换的方法。分享给大家供大家参考。具体如下:

本文实例讲述了AndroID实现GrIDVIEw中ImageVIEw动态变换的方法。分享给大家供大家参考。具体如下:

使用YY影音的时候,发现点击GrIDVIEw的某一个Item,Item里面的图标会在按下的时候发生变换,变成另外一个图片。

自己写了一个类似的demo,具体步骤如下:

1、创建一个包含GrID的Acitity
2、创建item.xml 里面包含一个imagevIEw和一个textvIEw
3、自定义一个adapter,从baseadapter继承
4、在getVIEw中为每个imageVIEw添加setontouchListener

代码入下:

MainActivity.javapackage 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);     }     else if(arg1.getAction() == MotionEvent.ACTION_UP) {      Log.d("WeatherWidget",arg1.getAction()+"");      textVIEw.setCompoundDrawablesWithIntrinsicBounds(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程序设计有所帮助。

总结

以上是内存溢出为你收集整理的Android实现GridView中ImageView动态变换的方法全部内容,希望文章能够帮你解决Android实现GridView中ImageView动态变换的方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142247.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存