Gallery组件被称之为画廊,是一种横向浏览图片的列表,在使用android API 19 Platform 时会发现Gallery被画上了横线,表明谷歌已经不推荐使用该组件了,
* @deprecated This widget is no longer supported. Other horizontally scrolling* widgets include {@link HorizontalScrollView} and {@link android.support.v4.view.ViewPager}* from the support library. 取而代之的是 HorizontalScrollView,也就是横向的ScrollView 和 ViewPager,这两种组件前面都有了解到。 Gallery和那些ListView 等都一样需要将资源加载进适配器,然后通过SetAdapter给控件设置适配器。 1,自定义一个适配器 继承自BaseAdapter
package com.example.lining.gallery; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Gallery; import android.widget.ImageView; import java.util.List; /** * Created by lining on 2015/11/27. */ public class ImageAdapter extends BaseAdapter { private int[]res; private Context context; public ImageAdapter(int[]res,Context context){ this.res = res; this.context = context; } @Override public int getCount() { return res.length; } @Override public Object getItem(int position) { return res[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView = new ImageView(context); imageView.setImageResource(res[position]); imageView.setLayoutParams(new Gallery.LayoutParams(300, 200));//设置ImageView的宽和高 imageView.setScaleType(ImageView.ScaleType.FIT_XY);//采用横向和纵向拉伸 return imageView; } }
2,设置适配器,并相应监听事件OnItemSelectListening
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener { private int []res={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a5,R.drawable.a6}; 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(res,this); gallery.setAdapter(imageAdapter); gallery.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id){ } @Override public void onNothingSelected(AdapterView<?> parent) { }}
这时我们可以在 onItemSelected响应函数中打印出我们选择的图片信息,onNothingSelected是只要其他的选项没有被选择就会被触发 现在我们让选择的图片用ImageSwitch显示出来
ImageSwitch 和ImageView的功能有点类似,它们都可以用于显示图片,区别是ImageSwitch的效果更炫,他可以 指定图片切换时的效果 ImageSwitch粗略的理解就是ImamgeView的选择器,他需要设置ViewFactory的方法,我们让ViewFactory的makeView方法返回 ImageView 3,在xml中加入ImageSwitch标签,并且设置淡入淡出的效果
imageSwitcher = (ImageSwitcher)findViewById(R.id.iamgeSwitch); imageSwitcher.setFactory(this); imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
同时需要实现ViewFactory的 ViewSwitcher.ViewFactory的监听事件
@Override public View makeView() { ImageView imageView = new ImageView(this); imageView.setScaleType(ImageView.ScaleType.CENTER);//等比例的缩放 return imageView; }
4,这时一切都准备好了,只需要在Gallery的选择事件中设置ImageSwitch的资源就可以了
imageSwitcher.setBackgroundResource(res[position]);
时间: 2024-10-09 02:59:32