释放双眼,带上耳机,听听看~!
先上图
当滑动时,选中的图片会变大,两侧的图片会变小。接下来上代码
MainActivity.Java
public class MainActivity extends AppCompatActivity {
private Gallery gallery;
private ImageAdapter imageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery) findViewById(R.id.gallery);
imageAdapter = new ImageAdapter(this);
gallery.setSpacing(10);//图与图之间的横向距离
gallery.setAdapter(imageAdapter);
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView> adapterView, View view, int position, long id) {
// 选中Gallery中某个图像时,放大显示该图像
imageAdapter.setSelectItem(position);
imageAdapter.notifyDataSetChanged();//当滑动时,事件响应,通知适配器更新数据
}
@Override
public void onNothingSelected(AdapterView> adapterView) {
//没选中
}
});
}
}
————-是不是很简单呢?——————–
activity_main:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abc.gallerydemo.MainActivity">
android:id="@+id/gallery"
android:layout_marginTop="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
—–接下来是ImageAdapter——–
public class ImageAdapter extends BaseAdapter{
private Context mContext;
private int mGalleryItemBackground;
private int[] myImageIds = {R.drawable.icon_bmfw, R.drawable.icon_bszn, R.drawable.icon_sqgg, R.drawable.icon_sqxw, R.drawable.icon_zcbx,};
private int selectItem;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = mContext.obtainStyledAttributes(R.styleable.Gallery); /* 使用在res/values/attrs.xml中的定义 的Gallery属性. */
// mGalleryItemBackground = a.getResourceId(R.styleable.Gallery_android_galleryItemBackground, 0); ///*取得Gallery属性的Index
a.recycle();/* 让对象的styleable属性能够反复使用 */
}
@Override
public int getCount() {
return Integer.MAX_VALUE;//最大值能使图片无限滑动
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public void setSelectItem(int selectItem) {
this.selectItem = selectItem;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(myImageIds[position%myImageIds.length]);//实现循环滑动
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
if(selectItem==position){
//选中时的动画
// Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.my_scale_action); //实现动画效果
// imageView.startAnimation(animation); //选中时,这时设置的比较大
imageView.setLayoutParams(new Gallery.LayoutParams(320,240));
}
else{
//未选中时的动画
// Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.my_scale_action); //实现动画效果
// imageView.startAnimation(animation);
imageView.setLayoutParams(new Gallery.LayoutParams(160,120));//未选中
}
return imageView;
}
}
代码里都有注释,很简单而且一读就懂。我也是一名刚入门的新手,希望与大家共同进步。
源码:http://download.csdn.net/detail/qq_24531461/9649849