android 选中GridView 中 的item 时进展图片放大的操作
android 选中GridView 中 的item 时进行图片放大的操作
2、anim.xml 文件
1、在GridView 中我们会遇到当选中某个Item 时想让它处于高亮或者是放大的操作,我们只需要在setOnItemClickListener() 方法中把相应的position 给传过去。
package com.example.sgridview; import java.util.ArrayList; import java.util.List; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.ImageView.ScaleType; public class MainActivity extends Activity { private GridView gridView; private MyAdapter adapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lists.add(R.drawable.ic_launcher); lists.add(R.drawable.ic_launcher); lists.add(R.drawable.ic_launcher); this.gridView = (GridView) this.findViewById(R.id.gridView); adapter = new MyAdapter(); this.gridView.setAdapter(adapter); this.gridView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int id, long arg3) { // TODO Auto-generated method stub MyAdapter ad = (MyAdapter) arg0.getAdapter(); ad.setNotifyDataChange(id); } }); } private List<Integer> lists = new ArrayList<Integer>(); private class MyAdapter extends BaseAdapter { private int selectPic = -1; @Override public int getCount() { // TODO Auto-generated method stub return lists.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return lists.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } public void setNotifyDataChange(int id) { selectPic = id; super.notifyDataSetChanged(); } @SuppressLint("NewApi") @Override public View getView(int position, View view, ViewGroup arg2) { ImageView img = new ImageView(MainActivity.this); img.setBackgroundResource(lists.get(position)); if (selectPic == position) { Animation testAnim = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim); img.startAnimation(testAnim); } else { // the rest img.setScaleType(ScaleType.CENTER_INSIDE); } return img; } } }
2、anim.xml 文件
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true"> <scale android:interpolator="@android:anim/accelerate_interpolator" android:fromXScale="1.0" android:toXScale="1.4" android:fromYScale="1.0" android:toYScale="1.4" android:pivotX="50%" android:pivotY="50%" android:duration="250" /> </set>