论坛首页 移动开发技术论坛

ImageSwitcher TextSwitcher Animation 使用

浏览 7133 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-14   最后修改:2010-01-14
ImageSwitcher TextSwitcher


[功能]
* 之所以把ImageSwitcher TextSwitcher  放在一起 因为二者实在太像了 使用起来基本一样
* 严格意义来说 ImageSwitcher TextSwitcher 和 ViewFlipper 也基本一样 都能包含数个View 且View直接相互切换可以设置渐变动画 不同的就是 前二者里面要显示的View比较固定 为ImageView TextView 而 ViewFlipper 里面可以为任意View


[ImageSwitcher 使用]
1. 定义一个含有ImageSwitcher 的 main.xml 还可以有2个Button 用于切换控制用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
	<Button
	android:id="@+id/previousButton"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Previous"
    />
    <Button
	android:id="@+id/nextButton"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Next"
    />
    </LinearLayout>
<ImageSwitcher  
	android:id="@+id/switcher"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"
    >
</ImageSwitcher>
</LinearLayout>



2. 各个View 的初始化 并设置 ImageSwitcher 的渐变动画效果
previous = (Button) findViewById(R.id.previousButton);
    	next = (Button) findViewById(R.id.nextButton);
    	
    	switcher = (ImageSwitcher) findViewById(R.id.switcher);
    	switcher.setFactory(this);
    	
    	switcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
    	switcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));



3. 以上需要特别说明的是
switcher = (ImageSwitcher) findViewById(R.id.switcher);
    	switcher.setFactory(this);


因为有这一行的定义
switcher.setFactory(this);

这就要求该Activity 必须如下声明 而且要定义 public View makeView() 供ImageSwitcher 的第一张用 返回一个 ImageView
public class MyImageSwicherUsage extends Activity 
			implements ViewSwitcher.ViewFactory{
@Override  // the first View
	public View makeView() {
		// TODO Auto-generated method stub
		ImageView iv = new ImageView(this);
    	iv.setImageResource(resource[1]);
    	
    	return iv;
	}


4. 还有一点需要特别注意的是: ImageSwitcher 显示Image的方法
switcher.setImageResource(int)


5. 因为考虑到 循环切换 的问题,所以在显示知道Image id 的时候 做了一些改动 如下:
private int fitPrevious(int i){
    	int aint = i;
    	
    	if(aint <= 0){
    		aint = aint + resource.length;
    	}
    	aint = aint - 1;
    	return aint;
   
    }
    
    private int fitNext(int i){
    	int aint = i + 1;
    	
    	if(aint > resource.length - 1){
    		aint = aint - resource.length;
    	}
    	
    	return aint;
    }



6. 更详细或其他问题 可以见代码 或发贴说明一下


[TextSwitcher 使用]
1. 二者的使用基本相同 不过 还是有一些差别 如下:
× 其 main.xml 如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
	<Button
	android:id="@+id/previousButton"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Previous"
    />
    <Button
	android:id="@+id/nextButton"  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Next"
    />
    </LinearLayout>
<TextSwitcher  
	android:id="@+id/switcher"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"
    >
</TextSwitcher>
</LinearLayout>



× 显示指定内容 方法如下:
switcher.setText(String);




详情见代码! done.
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics