android导航页效果

1.创建一个滑动监听接口

public interface OnViewChangeListener {

public void OnViewChange(int view);

}

2.自定义一个滑动的页面

public class MyScrollLayout extends ViewGroup{


    private static final String TAG = "ScrollLayout";   
    
    private VelocityTracker mVelocityTracker;  
    
    private static final int SNAP_VELOCITY = 600;    
    
    private Scroller  mScroller;

    private int mCurScreen;    
    
private int mDefaultScreen = 0;    
 
    private float mLastMotionX;    


    
    private OnViewChangeListener mOnViewChangeListener;
 
public MyScrollLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}

public MyScrollLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init(context);
}

public MyScrollLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub

init(context);
}

private void init(Context context)
{
mCurScreen = mDefaultScreen;    


   mScroller = new Scroller(context); 
   
}


@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {

if (changed) {    
           int childLeft = 0;    
           final int childCount = getChildCount();    
               
           for (int i=0; i<childCount; i++) {    
               final View childView = getChildAt(i);    
               if (childView.getVisibility() != View.GONE) {    
                   final int childWidth = childView.getMeasuredWidth();    
                   childView.layout(childLeft, 0,     
                           childLeft+childWidth, childView.getMeasuredHeight());    
                   childLeft += childWidth;    
               }    
           }    
       }    
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);



final int width = MeasureSpec.getSize(widthMeasureSpec);       


final int count = getChildCount();       
        for (int i = 0; i < count; i++) {       
            getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);       
        }         
        
        scrollTo(mCurScreen * width, 0);

}




public void snapToDestination() {    
       final int screenWidth = getWidth();    


       final int destScreen = (getScrollX()+ screenWidth/2)/screenWidth;    
       snapToScreen(destScreen);    
}  

public void snapToScreen(int whichScreen) {    

       // get the valid layout page    
       whichScreen = Math.max(0, Math.min(whichScreen, getChildCount()-1));    
       if (getScrollX() != (whichScreen*getWidth())) {    
               
           final int delta = whichScreen*getWidth()-getScrollX();    
       
           mScroller.startScroll(getScrollX(), 0,     
                   delta, 0, Math.abs(delta)*2);
 
           
           mCurScreen = whichScreen;    
           invalidate();       // Redraw the layout    
           
           if (mOnViewChangeListener != null)
           {
            mOnViewChangeListener.OnViewChange(mCurScreen);
           }
       }    
   }    




@Override
public void computeScroll() {
// TODO Auto-generated method stub
if (mScroller.computeScrollOffset()) {    
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());  
            postInvalidate();    
        }   
}


@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub           
           
       final int action = event.getAction();    
       final float x = event.getX();    
           
       switch (action) {    
       case MotionEvent.ACTION_DOWN: 
       
         Log.i("", "onTouchEvent  ACTION_DOWN");
         
        if (mVelocityTracker == null) {    
           mVelocityTracker = VelocityTracker.obtain();    
           mVelocityTracker.addMovement(event); 
   }
         
           if (!mScroller.isFinished()){    
               mScroller.abortAnimation();    
           }    
           
           mLastMotionX = x;           
           break;    
               
       case MotionEvent.ACTION_MOVE:  
          int deltaX = (int)(mLastMotionX - x);
          
          if (IsCanMove(deltaX))
          {
        if (mVelocityTracker != null)
          {
              mVelocityTracker.addMovement(event); 
          }   


             mLastMotionX = x;    
 
             scrollBy(deltaX, 0);
          }
         
          break;    
               
       case MotionEvent.ACTION_UP:       
       
        int velocityX = 0;
           if (mVelocityTracker != null)
           {
            mVelocityTracker.addMovement(event); 
            mVelocityTracker.computeCurrentVelocity(1000);  
            velocityX = (int) mVelocityTracker.getXVelocity();
           }
                   
               
           if (velocityX > SNAP_VELOCITY && mCurScreen > 0) {       
               // Fling enough to move left       
               Log.e(TAG, "snap left");    
               snapToScreen(mCurScreen - 1);       
           } else if (velocityX < -SNAP_VELOCITY       
                   && mCurScreen < getChildCount() - 1) {       
               // Fling enough to move right       
               Log.e(TAG, "snap right");    
               snapToScreen(mCurScreen + 1);       
           } else {       
               snapToDestination();       
           }      
           
          
           
           if (mVelocityTracker != null) {       
               mVelocityTracker.recycle();       
               mVelocityTracker = null;       
           }       


           break;      
       }    
           
       return true;    
}



private boolean IsCanMove(int deltaX)
{

if (getScrollX() <= 0 && deltaX < 0 )
{
return false;
}

if  (getScrollX() >=  (getChildCount() - 1) * getWidth() && deltaX > 0)
{
return false;
}


return true;
}

public void SetOnViewChangeListener(OnViewChangeListener listener)
{
mOnViewChangeListener = listener;
}


}

3.布局中使用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mainRLayout"
    android:background="#000000"
    >

<!-- 与自己写的MyScrollLayout的包名一致-->
    <com.test.MyScrollLayout
       xmlns:android="http://schemas.android.com/apk/res/android"    
 android:id="@+id/ScrollLayout"    
 android:layout_width="fill_parent"    
 android:layout_height="fill_parent"
 android:visibility="visible"
 >

<!--图片 -->
      <RelativeLayout  
          android:background="@drawable/start_ad1"    
 android:layout_width="fill_parent"    
 android:layout_height="fill_parent"
 />
      <RelativeLayout  
          android:background="@drawable/start_ad2"    
 android:layout_width="fill_parent"    
 android:layout_height="fill_parent"
 />
      
      <RelativeLayout  
          android:id="@+id/start_ad3"
          android:background="@drawable/start_ad3"    
 android:layout_width="fill_parent"    
 android:layout_height="fill_parent"/>


    </com.test.MyScrollLayout>

</RelativeLayout>

4.进行应用

public class NewActivity extends Activity implements OnViewChangeListener{

private MyScrollLayout mScrollLayout;
private int count;
private int currentItem;
private RelativeLayout layout;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mScrollLayout  = (MyScrollLayout) findViewById(R.id.ScrollLayout);
count = mScrollLayout.getChildCount();
mScrollLayout.SetOnViewChangeListener(this);
layout=(RelativeLayout) findViewById(R.id.start_ad3);

//最后一个导航页面应该是点击按钮跳转到首页,由于按钮被做在了背景图上,所以我就给第三个背景图设置了点击事件,否则可以在第三张背景图里加按钮,给按钮添加点击事件
layout.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
//做跳转处理
}
});
    }
    

@Override
public void OnViewChange(int position) {
setcurrentPoint(position);
}


private void setcurrentPoint(int position) {
if(position < 0 || position > count -1 || currentItem == position) {
return;
}
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值