当前位置: 网学 > 编程文档 > Android > 正文

左右滑屏_Android

来源:Http://myeducs.cn 联系QQ:点击这里给我发消息 作者: myeducs.cn 发布时间: 13/03/17

【网学网提醒】:网学会员为广大网友收集整理了,左右滑屏_Android,希望对大家有所帮助!


    最近比较闲,想起墨迹左右滑屏的效果该如何实现,如是在网上搜索资料,都没看到理想的效果,于是参照别人的例子自己加以修改,最终效果还行。左右滑屏是通过ViewGroup结合手势划屏来实现,代码如下:
    ScrollLayoutTest.java
    packagecom.scroll.test;
    importcom.yao_guet.test.R;
    importandroid.app.Activity;
    importandroid.os.Bundle;
    importandroid.util.Log;
    importandroid.view.View;
    importandroid.view.View.OnClickListener;
    importandroid.view.View.OnFocusChangeListener;
    importandroid.widget.Button;
    publicclassScrollLayoutTestextendsActivity{
    privateScrollLayoutsl;
    privateButtonbtn1;
    privatePageControlViewpageControlView;
    @Override
    publicvoidonCreate(BundlesavedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    sl=(ScrollLayout)findViewById(R.id.ScrollLayoutTest);
    pageControlView=(PageControlView)findViewById(R.id.pageControl);
    btn1=(Button)findViewById(R.id.btn1);
    btn1.setOnClickListener(newOnClickListener(){
    @Override
    publicvoidonClick(Viewv){
    sl.snapToScreen(1);
    }
    });
    sl.setOnFocusChangeListener(newOnFocusChangeListener(){
    @Override
    publicvoidonFocusChange(Viewv,booleanhasFocus){
    inti=v.getVisibility();
    Log.v("=sl.i=",i+"=");
    }
    });
    Log.v("=sl.getCurScreen()=",sl.getCurScreen()+"=");
    pageControlView.bindScrollLayout(sl);
    }
    }
    ScrollLayout.java
    packagecom.scroll.test;
    importandroid.content.Context;
    importandroid.graphics.Canvas;
    importandroid.util.AttributeSet;
    importandroid.util.Log;
    importandroid.view.MotionEvent;
    importandroid.view.VelocityTracker;
    importandroid.view.View;
    importandroid.view.ViewConfiguration;
    importandroid.view.ViewGroup;
    importandroid.widget.Scroller;
    /**
    *仿Launcher中的WorkSapce,可以左右滑动切换屏幕的类
    *
    *
    *
    */
    publicclassScrollLayoutextendsViewGroup{
    privatestaticfinalStringTAG="ScrollLayout";
    privateScrollermScroller;
    privateVelocityTrackermVelocityTracker;
    
    privateintmCurScreen;
    privateintmDefaultScreen=0;
    
    privatestaticfinalintTOUCH_STATE_REST=0;
    privatestaticfinalintTOUCH_STATE_SCROLLING=1;
    
    privatestaticfinalintSNAP_VELOCITY=600;
    
    privateintmTouchState=TOUCH_STATE_REST;
    privateintmTouchSlop;
    privatefloatmLastMotionX;
    privatefloatmLastMotionY;
    publicScrollLayout(Contextcontext,AttributeSetattrs){
    this(context,attrs,0);
    //TODOAuto-generatedconstructorstub
    }
    publicScrollLayout(Contextcontext,AttributeSetattrs,intdefStyle){
    super(context,attrs,defStyle);
    //TODOAuto-generatedconstructorstub
    mScroller=newScroller(context);
    
    mCurScreen=mDefaultScreen;
    mTouchSlop=ViewConfiguration.get(getContext()).getScaledTouchSlop();
    }
    @Override
    protectedvoidonLayout(booleanchanged,intl,intt,intr,intb){
    //TODOAuto-generatedmethodstub
    if(changed){
    intchildLeft=0;
    finalintchildCount=getChildCount();
    
    for(inti=0;i    finalViewchildView=getChildAt(i);
    if(childView.getVisibility()!=View.GONE){
    finalintchildWidth=childView.getMeasuredWidth();
    childView.layout(childLeft,0,
    childLeft+childWidth,childView.getMeasuredHeight());
    childLeft+=childWidth;
    }
    }
    }
    }
    @Override
    protectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){
    Log.e(TAG,"onMeasure");
    super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    
    finalintwidth=MeasureSpec.getSize(widthMeasureSpec);
    finalintwidthMode=MeasureSpec.getMode(widthMeasureSpec);
    if(widthMode!=MeasureSpec.EXACTLY){
    thrownewIllegalStateException("ScrollLayoutonlycanmCurScreenrunatEXACTLYmode!");
    }
    
    finalintheightMode=MeasureSpec.getMode(heightMeasureSpec);
    if(heightMode!=MeasureSpec.EXACTLY){
    thrownewIllegalStateException("ScrollLayoutonlycanrunatEXACTLYmode!");
    }
    
    //ThechildrenaregiventhesamewidthandheightasthescrollLayout
    finalintcount=getChildCount();
    for(inti=0;i    getChildAt(i).measure(widthMeasureSpec,heightMeasureSpec);
    }
    //Log.e(TAG,"movingtoscreen"+mCurScreen);
    scrollTo(mCurScreen*width,0);
    }
    
    /**
    *Accordingtothepositionofcurrentlayout
    *scrolltothedestinationpage.
    */
    publicvoidsnapToDestination(){
    finalintscreenWidth=getWidth();
    finalintdestScreen=(getScrollX()+screenWidth/2)/screenWidth;
    snapToScreen(destScreen);
    }
    
    publicvoidsnapToScreen(intwhichScreen){
    //getthevalidlayoutpage
    whichScreen=Math.max(0,Math.min(whichScreen,getChildCount()-1));
    if(getScrollX()!=(whichScreen*getWidth())){
    
    finalintdelta=whichScreen*getWidth()-getScrollX();
    mScroller.startScroll(getScrollX(),0,
    delta,0,Math.abs(delta)*2);
    mCurScreen=whichScreen;
    onScreenChangeListener.onScreenChange(mCurScreen);
    invalidate();//Redrawthelayout
    }
    }
    
    publicvoidsetToScreen(intwhichScreen){
    whichScreen=Math.max(0,Math.min(whichScreen,getChildCount()-1));
    mCurScreen=whichScreen;
    scrollTo(whichScreen*getWidth(),0);
    }
    
    publicintgetCurScreen(){
    returnmCurScreen;
    }
    
    @Override
    publicvoidcomputeScroll(){
    //TODOAuto-generatedmethodstub
    if(mScrollerputeScrollOffset()){
    scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
    postInvalidate();
    }
    }
    @Override
    publicbooleanonTouchEvent(MotionEventevent){
    //TODOAuto-generatedmethodstub
    
    if(mVelocityTracker==null){
    mVelocityTracker=VelocityTracker.obtain();
    }
    mVelocityTracker.addMovement(event);
    
    finalintaction=event.getAction();
    finalfloatx=event.getX();
    finalfloaty=event.getY();
    
    switch(action){
    caseMotionEvent.ACTION_DOWN:
    Log.e(TAG,"eventdown!");
    if(!mScroller.isFinished()){
    mScroller.abortAnimation();
    }
    mLastMotionX=x;
    break;
    
    caseMotionEvent.ACTION_MOVE:
    intdeltaX=(int)(mLastMotionX-x);
    mLastMotionX=x;
    
    scrollBy(deltaX,0);
    break;
    
    caseMotionEvent.ACTION_UP:
    Log.e(TAG,"event:up");
    //if(mTouchState==TOUCH_STATE_SCROLLING){
    finalVelocityTrackervelocityTracker=mVelocityTracker;
    velocityTrackerputeCurrentVelocity(1000);
    intvelocityX=(int)velocityTracker.getXVelocity();
    Log.e(TAG,"velocityX:"+velocityX);
    
    if(velocityX>SNAP_VELOCITY&;&;mCurScreen>0){
    //Flingenoughtomoveleft
    //onScreenChangeListener.onScreenChange(mCurScreen-1);
    Log.e(TAG,"snapleft");
    snapToScreen(mCurScreen-1);
    }elseif(velocityX<-SNAP_VELOCITY
    &;&;mCurScreen    //Flingenoughtomoveright
    Log.e(TAG,"snapright");
    //onScreenChangeListener.onScreenChange(mCurScreen+1);
    snapToScreen(mCurScreen+1);
    }else{
    snapToDestination();
    }
    if(mVelocityTracker!=null){
    mVelocityTracker.recycle();
    mVelocityTracker=null;
    }
    //}
    mTouchState=TOUCH_STATE_REST;
    break;
    caseMotionEvent.ACTION_CANCEL:
    mTouchState=TOUCH_STATE_REST;
    break;
    }
    
    returntrue;
    }
    @Override
    publicbooleanonInterceptTouchEvent(MotionEventev){
    //TODOAuto-generatedmethodstub
    Log.e(TAG,"onInterceptTouchEvent-slop:"+mTouchSlop);
    
    finalintaction=ev.getAction();
    if((action==MotionEvent.ACTION_MOVE)&;&;
    (mTouchState!=TOUCH_STATE_REST)){
    returntrue;
    }
    
    finalfloatx=ev.getX();
    finalfloaty=ev.getY();
    
    switch(action){
    caseMotionEvent.ACTION_MOVE:
    finalintxDiff=(int)Math.abs(mLastMotionX-x);
    if(xDiff>mTouchSlop){
    mTouchState=TOUCH_STATE_SCROLLING;
    
    }
    break;
    
    caseMotionEvent.ACTION_DOWN:
    mLastMotionX=x;
    mLastMotionY=y;
    mTouchState=mScroller.isFinished()?TOUCH_STATE_REST:TOUCH_STATE_SCROLLING;
    break;
    
    caseMotionEvent.ACTION_CANCEL:
    caseMotionEvent.ACTION_UP:
    mTo
    uchState=TOUCH_STATE_REST;
    break;
    }
    
    returnmTouchState!=TOUCH_STATE_REST;
    }
    
    //监听页面变动
    publicinterfaceOnScreenChangeListener{
    voidonScreenChange(intcurrentIndex);
    }
    privateOnScreenChangeListeneronScreenChangeListener;
    publicvoidsetOnScreenChangeListener(
    OnScreenChangeListeneronScreenChangeListener){
    this.onScreenChangeListener=onScreenChangeListener;
    }
    
    
    }
    PageControlView.java
    packagecom.scroll.test;
    importcom.scroll.test.ScrollLayout.OnScreenChangeListener;
    importcom.yao_guet.test.R;
    importandroid.R.integer;
    importandroid.content.Context;
    importandroid.util.AttributeSet;
    importandroid.util.Log;
    importandroid.widget.FrameLayout;
    importandroid.widget.ImageView;
    importandroid.widget.LinearLayout;
    publicclassPageControlViewextendsLinearLayout{
    privateContextcontext;
    privateintcount;
    
    publicPageControlView(Contextcontext){
    super(context);
    this.context=context;
    }
    publicPageControlView(Contextcontext,AttributeSetattrs){
    super(context,attrs);
    this.context=context;
    }
    
    publicvoidbindScrollLayout(ScrollLayoutscrollLayout){
    this.count=scrollLayout.getChildCount();
    Log.v("=count==",count+"=");
    //初始化第一屏
    generatePageControl(0);
    scrollLayout.setOnScreenChangeListener(newOnScreenChangeListener(){
    
    @Override
    publicvoidonScreenChange(intcurrentIndex){
    
    generatePageControl(currentIndex);
    }
    });
    }
    
    privatevoidgeneratePageControl(intcurrentIndex){
    this.removeAllViews();
    for(inti=0;i    Log.v("=i==",i+"=");
    ImageViewimageView=newImageView(context);
    if(i==currentIndex){
    imageView.setImageResource(R.drawable.page_indicator_focused);
    }else{
    
    imageView.setImageResource(R.drawable.page_indicator);
    }
    this.addView(imageView);
    }
    }
    }
    main.xml
    
        android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        xmlns:android="schemas.android/apk/res/android"
    android:id="@+id/ScrollLayoutTest"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        android:background="#FF00"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
        android:background="#F0F0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
        android:background="#F00F"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    
        android:background="#FF00"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        android:layout_width="wrap_content"
    android:layout_height="
    wrap_content"
    android:text="Button1"
    android:id="@+id/btn1"/>
    
        android:layout_width="wrap_content"
    android:layout_height="wrap_content">
        android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button2"/>
    
    
        android:id="@+id/pageControl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true">
    
    
    
    如果想设置循环:
    
    设置循环滑屏:
    1.在ScrollLayout.java类中的snapToDestination()方法中添加:
    //设置使循环(5表示总共有多少屏)
    if(destScreen==0){
    setToScreen(5);
    }elseif(destScreen==5){
    setToScreen(0);
    }else{
    //设置不循环
    snapToScreen(destScreen);
    }
    2.在setToScreen(intwhichScreen)方法中添加:
    //设置使循环
    onScreenChangeListener.onScreenChange(mCurScreen);
    

网学推荐

免费论文

原创论文

浏览:
设为首页 | 加入收藏 | 论文首页 | 论文专题 | 设计下载 | 网学软件 | 论文模板 | 论文资源 | 程序设计 | 关于网学 | 站内搜索 | 网学留言 | 友情链接 | 资料中心
版权所有 QQ:3710167 邮箱:3710167@qq.com 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
Copyright 2008-2015 myeducs.Cn www.myeducs.Cn All Rights Reserved
湘ICP备09003080号