rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » Android

ViewFlipper 实例

功能]
1. ViewFlipper 可以包含多个View 且View之间的切换有Animation  比如:渐变效果 [代码]
1. 创建包含ViewFlipper 的main.xml 还包含2个Button 用于各个View切换

Java代码
  • <?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>  
  • <?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代码
  • 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));  
  • 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代码
  • 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;   
  •     }  
  • 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代码
  • flipper.showNext();  
  • flipper.showNext();

    * 上一个View

    Java代码
  • flipper.showPrevious();  
  • 顶一下
    (0)
    踩一下
    (0)