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

作者:白羽雕弓 时间:2022-06-21 06:16:55 

本文实例讲述了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程序设计有所帮助。

标签:Android,GridView,ImageView
0
投稿

猜你喜欢

  • Android基础之Fragment与Activity交互详解

    2021-12-25 16:02:11
  • Android权限操作之uses-permission详解

    2023-07-20 17:07:02
  • Java String类简单用法实战示例【字符串输出、比较】

    2021-09-22 11:59:51
  • 使用递归实现数组求和示例分享

    2023-04-24 02:17:40
  • SpringBoot实现本地存储文件上传及提供HTTP访问服务的方法

    2022-09-14 19:09:12
  • Activiti开发环境的配置

    2021-07-31 21:57:51
  • Android中使用Gson解析JSON数据的两种方法

    2021-10-13 23:25:39
  • C#实现窗口之间的传值

    2022-05-26 08:28:14
  • 消息中间件详解以及比较选择

    2022-08-17 03:01:01
  • Java 遍历list和map的方法

    2023-02-06 22:23:40
  • Java通过JavaMail发送邮件功能

    2022-09-07 16:47:20
  • springboot整合Shiro

    2022-11-13 11:07:52
  • redis redisson 集合的使用案例(RList、Rset、RMap)

    2023-11-13 13:50:21
  • spring-boot-maven-plugin:打包时排除provided依赖问题

    2023-07-18 02:12:11
  • Kotlin Flow数据流的3种使用场景详解

    2021-06-03 09:06:18
  • IDEA JetBrains Mono字体介绍和安装教程(详解)

    2022-01-23 17:57:51
  • Java中List Set和Map之间的区别_动力节点Java学院整理

    2022-12-11 04:57:38
  • C语言中传值与传指针的介绍与区别

    2023-08-01 15:26:51
  • 掌握Android Handler消息机制核心代码

    2023-11-29 18:29:23
  • SpringBoot访问windows共享文件的方法

    2023-11-08 13:37:13
  • asp之家 软件编程 m.aspxhome.com