Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android實現GridView中ImageView動態變換的方法

Android實現GridView中ImageView動態變換的方法

編輯:關於Android編程

本文實例講述了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程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved