【Android】图片切换组件ImageSwitcher的运用

类的继承关系

java.lang.Object
android.view.View
                android.view.ViewGroup
                       android.widget.FrameLayout
                              android.widget.ViewAnimator
                                     android.widget.ViewSwitcher
                                            android.widget.ImageSwitcher

ImageSwitcher Android 中控制图片展示效果的一个控件,如:幻灯片效果


公共方法

public void setImageDrawable (Drawable drawable)

绘制图片

public void setImageResource (int resid)

设置图片资源库

public void setImageURI (Uri uri)

设置图片地址


Java类

package com.app.test01;

import com.app.adapter.ImageAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Gallery.LayoutParams;
import android.widget.ViewSwitcher.ViewFactory;

public class ImageSwitcherActivity extends Activity{
	ImageSwitcher imageSwitcher;
	Gallery gallery;
	ImageAdapter iAdapter = new ImageAdapter(this);
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_imageswitcher);
		imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
		gallery = (Gallery) findViewById(R.id.gallery1);
		//设置用来生成将在视图转换器中切换的两个视图的工厂
		imageSwitcher.setFactory(new ViewFactory() {
			@Override
			public View makeView() {
				// TODO Auto-generated method stub
		        ImageView i = new ImageView(ImageSwitcherActivity.this);
		        // 把图片按比例扩大/缩小到View的宽度,居中显示
		        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
		        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
		                LayoutParams.MATCH_PARENT));
		        return i;
			}
		});
		
		gallery.setAdapter(iAdapter);
		gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Integer[] iViews = iAdapter.getmImageIds();
				imageSwitcher.setImageResource(iViews[position%iViews.length]);
			}
		});
	}
}

适配器ImageAdapter

package com.app.adapter;

import com.app.test01.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;


public class ImageAdapter extends BaseAdapter {
    private Context mContext;
    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() { /* 重写方法getCount,传回图片数目总数 */
        return Integer.MAX_VALUE;
    }

    public Object getItem(int position) {/* 重写的getItem,传回position */
        return position;
    }

    public long getItemId(int position) {/*重写方法getItemId,传回position */ 
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView i = new ImageView(mContext);
        i.setImageResource(mImageIds[position%mImageIds.length]);/* 设定图片给imageView对象 */  
        i.setLayoutParams(new Gallery.LayoutParams(150,75));
        return i;
    }

    public Integer[] getmImageIds() {
		return mImageIds;
	}

	public void setmImageIds(Integer[] mImageIds) {
		this.mImageIds = mImageIds;
	}

	private Integer[] mImageIds = {
            R.drawable.image01,
            R.drawable.image02,
            R.drawable.image03,
            R.drawable.image04,
            R.drawable.image05,
            R.drawable.image06,
            R.drawable.image07,
            R.drawable.image08,
            R.drawable.image09,
    };
}

布局文件

<?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"
    android:orientation="vertical" >

    <ImageSwitcher
        android:id="@+id/imageSwitcher1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true">
    </ImageSwitcher>

    <Gallery
        android:id="@+id/gallery1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="center_vertical" 
        android:spacing="16dp"/>

</RelativeLayout>

效果图

【Android】图片切换组件ImageSwitcher的运用_第1张图片

附:ImageView的属性scaleType

ImageView的属性android:scaleType,即ImageView.setScaleType(ImageView.ScaleType)。
android:scaleType是控制图片如何resized/moved来匹对ImageView的size。

ImageView.ScaleType / android:scaleType值的意义区别: 
CENTER /center  按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示
CENTER_CROP / centerCrop  按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽)
CENTER_INSIDE / centerInside  将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长/宽等于或小于View的长/宽
FIT_CENTER / fitCenter  把图片按比例扩大/缩小到View的宽度,居中显示
FIT_END / fitEnd   把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置
FIT_START / fitStart  把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置
FIT_XY / fitXY  把图片不按比例扩大/缩小到View的大小显示
MATRIX / matrix 用矩阵来绘制


你可能感兴趣的:(android,gallery,ImageSwitcher,图片切换,ScaleType)