Android ViewFlipper 用例

[功能]
1. ViewFlipper 可以包含多个View 且View之间的切换有Animation  比如:渐变效果


[代码]
1. 创建包含ViewFlipper 的main.xml 还包含2个Button 用于各个View切换

Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >   
  7.     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  8.     android:orientation="horizontal"  
  9.     android:layout_width="wrap_content"  
  10.     android:layout_height="wrap_content"  
  11.     >   
  12.     <Button   
  13.     android:id="@+id/previousButton"     
  14.     android:layout_width="wrap_content"    
  15.     android:layout_height="wrap_content"    
  16.     android:text="Previous"  
  17.     />   
  18.     <Button   
  19.     android:id="@+id/nextButton"     
  20.     android:layout_width="wrap_content"    
  21.     android:layout_height="wrap_content"    
  22.     android:text="Next"  
  23.     />   
  24.     </LinearLayout>   
  25. <ViewFlipper     
  26.     android:id="@+id/flipper"  
  27.     android:layout_width="fill_parent"    
  28.     android:layout_height="fill_parent"    
  29.     android:gravity="center"  
  30.     >   
  31. </ViewFlipper>   
  32. </LinearLayout>  
<?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>
<ViewFlipper  
	android:id="@+id/flipper"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center"
    >
</ViewFlipper>
</LinearLayout>



2. 设定 Animation 效果

Java代码 复制代码
  1. flipper = (ViewFlipper) findViewById(R.id.flipper);   
  2. flipper.setInAnimation(AnimationUtils.loadAnimation(this,   
  3.                 android.R.anim.fade_in));   
  4. flipper.setOutAnimation(AnimationUtils.loadAnimation(this,   
  5.                 android.R.anim.fade_out));  
flipper = (ViewFlipper) findViewById(R.id.flipper);
flipper.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));



3. 在 ViewFlipper  里面增加各种View

Java代码 复制代码
  1. flipper.addView(addTextByText("HelloAndroid"));   
  2.         flipper.addView(addImageById(R.drawable.beijing_003_mb5ucom));   
  3.         flipper.addView(addTextByText("eoe.Android"));   
  4.         flipper.addView(addImageById(R.drawable.beijing_004_mb5ucom));   
  5.         flipper.addView(addTextByText("Gryphone"));   
  6.   
  7. ublic View addTextByText(String text){   
  8.             TextView tv = new TextView(this);   
  9.             tv.setText(text);   
  10.             tv.setGravity(1);   
  11.             return tv;   
  12.     }   
  13.        
  14.     public View addImageById(int id){   
  15.         ImageView iv = new ImageView(this);   
  16.         iv.setImageResource(id);   
  17.            
  18.         return iv;   
  19.     }  
flipper.addView(addTextByText("HelloAndroid"));
        flipper.addView(addImageById(R.drawable.beijing_003_mb5ucom));
        flipper.addView(addTextByText("eoe.Android"));
        flipper.addView(addImageById(R.drawable.beijing_004_mb5ucom));
        flipper.addView(addTextByText("Gryphone"));

ublic View addTextByText(String text){
    		TextView tv = new TextView(this);
    		tv.setText(text);
    		tv.setGravity(1);
    		return tv;
    }
    
    public View addImageById(int id){
		ImageView iv = new ImageView(this);
		iv.setImageResource(id);
		
		return iv;
    }




4. View 切换
* 下一个View

Java代码 复制代码
  1. flipper.showNext();  
flipper.showNext();



* 上一个View

Java代码 复制代码
  1. flipper.showPrevious();  
flipper.showPrevious();




现释出所有代码!

over.

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值