ImageSwitcher TextSwitcher Animation 使用

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值