Android之ImageSwitcher 图片查看
Android之ImageSwitcher 图片查看
布局文件如下:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ImageSwitcher android:id="@+id/switcher"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true">
- </ImageSwitcher>
- <Gallery android:id="@+id/gallery"
- android:layout_width="fill_parent"
- android:layout_alignParentLeft="true"
- android:layout_height="60dp"
- android:spacing="15dp"
- android:layout_alignParentBottom="true"
- android:gravity="center_vertical"
- android:background="#aaaaaa">
- </Gallery>
- </RelativeLayout>
- package com.cloay.imageswitcher;
- import android.app.Activity;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.animation.AnimationUtils;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.Gallery.LayoutParams;
- import android.widget.BaseAdapter;
- import android.widget.Gallery;
- import android.widget.ImageSwitcher;
- import android.widget.ImageView;
- import android.widget.ViewSwitcher.ViewFactory;
- /**
- * ImageSwitcherActivity.java
- * @author cloay
- * 2011-7-16
- */
- public class ImageSwitcherActivity extends Activity implements OnItemSelectedListener, ViewFactory{
- private ImageSwitcher imageSwitcher;
- private Gallery gallery;
- private Integer [] imagesId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,
- R.drawable.f, R.drawable.g};
- private Integer [] selectId = new Integer[]{R.drawable.b, R.drawable.c, R.drawable.d,
- R.drawable.f, R.drawable.g};
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- imageSwitcher = (ImageSwitcher)findViewById(R.id.switcher);
- imageSwitcher.setFactory(this);
- //设置图片切换时的动画效果
- imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
- imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
- gallery = (Gallery)findViewById(R.id.gallery);
- //自定义ImageAdapter继承于BaseAdapter,是一个内部类
- gallery.setAdapter(new ImageAdapter(this));
- gallery.setOnItemSelectedListener(this);
- }
- @Override
- public View makeView() {
- ImageView image = new ImageView(this);
- image.setScaleType(ImageView.ScaleType.FIT_CENTER);
- image.setLayoutParams(new ImageSwitcher.LayoutParams(
- LayoutParams.FILL_PARENT, LayoutParams.MATCH_PARENT));
- return image;
- }
- @Override
- public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- imageSwitcher.setImageResource(imagesId[arg2]);
- }
- @Override
- public void onNothingSelected(AdapterView<?> arg0) {
- }
- public class ImageAdapter extends BaseAdapter{
- private Context context;
- int galleryItemBackground;
- public ImageAdapter (Context c){
- context = c;
- TypedArray typeArray = obtainStyledAttributes(R.styleable.Gallery1);
- galleryItemBackground = typeArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
- typeArray.recycle();
- }
- @Override
- public int getCount() {
- //返回selectId[]的长度
- return selectId.length;
- }
- @Override
- public Object getItem(int position) {
- return 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(selectId[position]);
- imageView.setAdjustViewBounds(true); //允许调整边框
- //设定底部画廊,自适应大小
- imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- //设置画廊背景
- imageView.setBackgroundResource(galleryItemBackground);
- return imageView;
- }
- }
- }
评论暂时关闭