标签:
1. 实现效果
2. 实现代码
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageSwitcher android:id="@+id/is_switcher" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" /> <Gallery android:id="@+id/gallery" android:layout_width="match_parent" android:layout_height="60dp" android:background="#55000000" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:gravity="center_vertical" android:spacing="16dp"/> </RelativeLayout>
public class MainActivity extends Activity implements OnItemSelectedListener, ViewFactory { /** * ImageSwitcher 控件 */ private ImageSwitcher is_switcher; /** * Gallery 控件 */ private Gallery galley; /** * 小图使用的数组 */ private Integer[] mThumbIds = { R.drawable.pic, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5 }; /** * 大图使用的数组 */ private Integer[] mImageIds = { R.drawable.ic_launcher, R.drawable.pic2, R.drawable.ic_launcher, R.drawable.pic2, R.drawable.ic_launcher }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * 使用无标题的样式 */ requestWindowFeature(Window.FEATURE_NO_TITLE); /** * 设置显示的布局文件 */ setContentView(R.layout.activity_main); /** * 实例化 ImageSwitcher 控件并与之关联 */ is_switcher = (ImageSwitcher) findViewById(R.id.is_switcher); /** * 指代下面的makeView 因为实现了ViewFactory 接口 */ is_switcher.setFactory(this); // 设置进入动画和出去动画 使用系统默认的样式 is_switcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in)); is_switcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out)); /** * 实例化一个Gallery控件并与之绑定 */ galley = (Gallery) findViewById(R.id.gallery); // 为Gallery 对象设置适配器 galley.setAdapter(new ImageAdapter(this)); /** * 设置当Gallery对象选项被选择时的监听事件 */ galley.setOnItemSelectedListener(this); } @Override public View makeView() { ImageView i = new ImageView(this); i.setBackgroundColor(0xFF000000); i.setScaleType(ImageView.ScaleType.FIT_CENTER); i.setLayoutParams(new ImageSwitcher.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); return i; } public class ImageAdapter extends BaseAdapter { public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mThumbIds.length; } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(mContext); i.setImageResource(mThumbIds[position]); i.setAdjustViewBounds(true); i.setLayoutParams(new Gallery.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); i.setBackgroundResource(R.drawable.ic_launcher); return i; } private Context mContext; } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { is_switcher.setImageResource(mImageIds[position]); } @Override public void onNothingSelected(AdapterView<?> parent) { } }
3. 图片
Android -- ImageSwitch和Gallery 混合使用
标签:
原文地址:http://www.cnblogs.com/SM-t/p/4183854.html