当前位置:操作系统 > 安卓/Android >>

Android左右滑动实现Activity切换类 (整合代码实例)

 先上图吧,下图是左右拖动的过程:
 \
具体代码如下:Fling_Gallery类
[java]
 package com.xu81.testflip; 
import android.content.Context; 
import android.view.GestureDetector; 
import android.view.KeyEvent; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.view.animation.Interpolator; 
import android.view.animation.Transformation; 
import android.widget.Adapter; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 
 
// TODO: 
 
// 1. In order to improve performance Cache screen bitmap and use for animation 
// 2. Establish superfluous memory allocations and delay or replace with reused objects 
//    Probably need to make sure we are not allocating objects (strings, etc.) in loops 
 
public class FlingGallery extends FrameLayout 

    // Constants 
     
    private final int swipe_min_distance = 120; 
    private final int swipe_max_off_path = 250; 
    private final int swipe_threshold_veloicty = 400; 
 
    // Properties 
     
    private int mViewPaddingWidth = 0; 
    private int mAnimationDuration = 250; 
    private float mSnapBorderRatio = 0.5f; 
    private boolean mIsGalleryCircular = true; 
 
    // Members 
 
    private int mGalleryWidth = 0; 
    private boolean mIsTouched = false; 
    private boolean mIsDragging = false; 
    private float mCurrentOffset = 0.0f; 
    private long mScrollTimestamp = 0; 
    private int mFlingDirection = 0; 
    private int mCurrentPosition = 0; 
    private int mCurrentViewNumber = 0; 
 
    private Context mContext; 
    private Adapter mAdapter; 
    private FlingGalleryView[] mViews; 
    private FlingGalleryAnimation mAnimation; 
    private GestureDetector mGestureDetector; 
    private Interpolator mDecelerateInterpolater; 
 
    public FlingGallery(Context context) 
    { 
        super(context); 
 
        mContext = context; 
        mAdapter = null; 
         
        mViews = new FlingGalleryView[3]; 
        mViews[0] = new FlingGalleryView(0, this); 
        mViews[1] = new FlingGalleryView(1, this); 
        mViews[2] = new FlingGalleryView(2, this); 
 
        mAnimation = new FlingGalleryAnimation(); 
        mGestureDetector = new GestureDetector(new FlingGestureDetector()); 
        mDecelerateInterpolater = AnimationUtils.loadInterpolator(mContext, android.R.anim.decelerate_interpolator); 
    } 
 
    public void setPaddingWidth(int viewPaddingWidth) 
    { 
        mViewPaddingWidth = viewPaddingWidth; 
    } 
 
    public void setAnimationDuration(int animationDuration) 
    { 
        mAnimationDuration = animationDuration; 
    } 
     
    public void setSnapBorderRatio(float snapBorderRatio) 
    { 
        mSnapBorderRatio = snapBorderRatio; 
    } 
 
    public void setIsGalleryCircular(boolean isGalleryCircular)  
    { 
        if (mIsGalleryCircular != isGalleryCircular) 
        { 
            mIsGalleryCircular = isGalleryCircular; 
     
            if (mCurrentPosition == getFirstPosition()) 
            { 
                // We need to reload the view immediately to the left to change it to circular view or blank 
                mViews[getPrevViewNumber(mCurrentViewNumber)].recycleView(getPrevPosition(mCurrentPosition));            
            } 
     
            if (mCurrentPosition == getLastPosition()) 
            { 
                // We need to reload the view immediately to the right to change it to circular view or blank 
                mViews[getNextViewNumber(mCurrentViewNumber)].recycleView(getNextPosition(mCurrentPosition));            
            } 
        } 
    } 
 
    public int getGalleryCount() 
    { 
        return (mAdapter == null) ? 0 : mAdapter.getCount(); 
    } 
 
    public int getFirstPosition() 
    { 
        return 0; 
    } 
 
    public int getLastPosition() 
    { 
        return (getGalleryCount() == 0) ? 0 : getGalleryCount() - 1; 
    } 
 
    private int getPrevPosition(int relativePosition) 
    { 
    &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,