龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 移动开发 > Android开发 >

Android中使用ViewFlipper进行手势切换实例(2)

时间:2014-10-15 11:28来源:网络整理 作者:网络 点击:
分享到:
复制代码 代码如下: private ViewFlipper viewFlipper; private Animation slideLeftIn; private Animation slideLeftOut; private Animation slideRightIn; private Animation slideRightOut; private Gest

复制代码 代码如下:
private ViewFlipper viewFlipper; 
 
private Animation slideLeftIn; 
private Animation slideLeftOut; 
private Animation slideRightIn; 
private Animation slideRightOut; 
 
private GestureDetector gestureDetector; 
@Override 
public void onCreate(Bundle savedInstanceState) 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
     
    viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper); 
       slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in); 
       slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out); 
       slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in); 
       slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out); 
        
       gestureDetector = new GestureDetector(new MyGestureDetector()); 
        
       new View.OnTouchListener() 
       { 
           public boolean onTouch(View v, MotionEvent event) 
           { 
               if (gestureDetector.onTouchEvent(event)) 
               { 
                   return true; 
               } 
               return false; 
           } 
       }; 

 
@Override 
public boolean onTouchEvent(MotionEvent event) 

    if (gestureDetector.onTouchEvent(event)) 
    { 
        return true; 
    } 
    else 
    { 
        return false; 
    } 
}

最后,在res文件夹下新建一个anim文件夹,用来存放view之间切换的动画文件:

1.slide_left_in.xml

精彩图集

赞助商链接