Android中ImageSwitcher详解(顺便附上TextSwitcher的Demo)
先看看继承关系,ImageSwitcher和TextSwitcher的继承关系是一样的。两个重要的父类:ViewSwitcher和ViewAnimator继承于ViewSwitcher,说明具备了切换功能
继承于ViewAnimator,说明具备了动画功能
ImageSwitcher原理
ImageSwitcher的内容在Gallery中已经有所讲解,这边系统的详解一下
ImageSwitcher粗略的理解就是ImageView的选择器
ImageSwitcher的原理:ImageSwitcher有两个子View:ImageView,当左右滑动的时候,就在这两个ImageView之间来回切换来显示图片
下面我们来看看Android自带的source,以便更深的理解这个原理:
既然有两个子ImageView,那么我们要创建两个ImageView给ImageSwitcher。创建ImageSwitcher是通过工厂来实现的,看下面代码
[java] imageSwicher.setFactory(this);
imageSwicher.setFactory(this);为imageSwitcher设置ViewFactory
[java] @Override
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setImageResource(arrayPictures[pictureIndex]);
return imageView;
}
@Override
public View makeView() {
ImageView imageView = new ImageView(this);
imageView.setImageResource(arrayPictures[pictureIndex]);
return imageView;
}实现ViewFactory的makeView()方法,makeView()方法就是负责给ImageSwitcher创建两个字ImageView
下面再来看看setFactory()方法的具体代码
[java] public void setFactory(ViewFactory factory) {
mFactory = factory;
obtainView();
obtainView();
}
public void setFactory(ViewFactory factory) {
mFactory = factory;
obtainView();
obtainView();
}可以看到在setFactory的同时,调用了两遍obtainView()方法,obtainView()方法就是给ImageSwitcher添加子ImageView的,调用两遍就是添加了两个子ImageView
再来看看obtainView()方法的具体代码
[java] private View obtainView() {
View child = mFactory.makeView();
LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp == null) {
lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
addView(child, lp);
return child;
}
private View obtainView() {
View child = mFactory.makeView();
LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (lp == null) {
lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
}
addView(child, lp);
return child;
}可以看到obtainView()方法的的职责就是:通过makeView()方法创建View,然后把创建出来的View添加到ImageSwitcher上
再来看看下面的方法
[java] public void setImageResource(int resid)
{
ImageView image = (ImageView)this.getNextView();
image.setImageResource(resid);
showNext();
}
public void setImageResource(int resid)
{
ImageView image = (ImageView)this.getNextView();
image.setImageResource(resid);
showNext();
}此方法就是用来显示下一张图片的,我们可以看到这个方法里面调用了getNextView()方法和showNext()方法,那么我们来看看这两个方法的具体代码
[java] public View getNextView() {
int which = mWhichChild == 0 ? 1 : 0;
return getChildAt(which);
}
public View getNextView() {
int which = mWhichChild == 0 ? 1 : 0;
return getChildAt(which);
}[java] public void showNext() {
setDisplayedChild(mWhichChild + 1);
}
public void showNext() {
setDisplayedChild(mWhichChild + 1);
}getNextView()方法是在两个子ImageView之间切换,showNext()方法是负责显示这两个子View中的哪一个
也就是说,现用getNextView()方法得到下一个View,然后重新设置这个View的imageResource,最后通过showNext()方法将下一个View显示出来
好了,ImageSwitcher的原理讲完了。下面附上一个Demo
ImageSwitcher实例
main.xml
[html] <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageSwitcher
android:id="@+id/imageSwicher"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@android:color/white"
android:gravity="center" >
</ImageSwitcher>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageSwitcher
android:id="@+id/imageSwicher"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="@android:color/white"
android:gravity="center" >
</ImageSwitcher>
</LinearLayout>
ImageSwicherDemoActivity.java
[java] package com.tianjf;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
/**
* 一个左右滑动浏览图片的Demo
*
* @author tianjf
*
*/
public class ImageSwicherDemoActivity extends Activity implements ViewFactory,
OnTouchListener {
private ImageSwitcher imageSwicher;
&
补充:移动开发 , Android ,