在活动中使用外部库中的变量

时间:2017-07-14 09:14:30

标签: java android xml

我正在尝试使用Activity中外部库中的变量“mCurrentPage”。此库使用FrameLayout中的视图,并将其显示为可以滑动的书页。我想要做的是执行一些事情,如果FrameLayout的X视图在前面。 (请不要带ToFront()) 我尝试过getElevation(),但它需要API> 21。

实际上该代码中的变量“mCurrentPage”完成了这项工作。但我也需要在其他活动中使用它(或模仿它),我只是无法弄清楚这个变量是如何工作的,它从哪里得到它们的值,以便告诉前面的当前视图

    public class PageTurnLayout extends FrameLayout {

    private Point mLastTouchPoint;
    private Rect mTopViewRect;
    private Rect mBottomViewRect;

    private Paint mPaint;
    private int mCurrentPage;

    private int mPageTouchSlop;
    private boolean mIsTurning;

    private PageTurnDirection mDirection;
    private float mFirstX;

    private Handler mHandler = new Handler();

    public PageTurnLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();

    }

    public PageTurnLayout(Context context, AttributeSet attrs, int defStyle)                   {
    super(context, attrs, defStyle);
    init();
}

    public PageTurnLayout(Context context) {
    super(context);
    init();

}

    private void init() {
    setWillNotDraw(false);
    mPaint = new Paint();

    mBottomViewRect = new Rect();
    mTopViewRect = new Rect();

    mPageTouchSlop = (int)             getResources().getDimension(R.dimen.touch_start_padding);
}

    protected boolean isTouchAPageTurnStart(MotionEvent ev) {
    if (ev.getAction() != MotionEvent.ACTION_DOWN)
        return false;

    return isTouchNearEdge(ev);

}

    protected boolean isTouchNearEdge(MotionEvent ev) {
    if (Math.abs(ev.getX() - getMeasuredWidth()) < mPageTouchSlop)
        return true;
    else if (ev.getX() < mPageTouchSlop)
        return true;

    return false;
}

protected PageTurnDirection getPageTurnDirection(MotionEvent ev) {
    if(mFirstX - ev.getX() == 0.0f)
        return null;

    PageTurnDirection direction = mFirstX - ev.getX() > 0 ? PageTurnDirection.LEFT : PageTurnDirection.RIGHT;
    return direction;
}

protected boolean shouldTurn() {
    if(mDirection == null)
        return false;

    if(mDirection == PageTurnDirection.LEFT && mCurrentPage == getChildCount() - 1)
        return false;
    else if(mDirection == PageTurnDirection.RIGHT && mCurrentPage == 0)
        return false;

    return true;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (event.getAction() == MotionEvent.ACTION_DOWN && !mIsTurning) {

        mIsTurning = isTouchAPageTurnStart(event);

        if (!mIsTurning) {
            return false;
        } else {

            invalidate();
            mLastTouchPoint = new Point((int) event.getX(), (int) event.getY());
            mFirstX = event.getX();
            return true;
        }

    } else if (event.getAction() == MotionEvent.ACTION_MOVE && mIsTurning) {
        if(mDirection == null) {
            //get the page turn direction
            mDirection = getPageTurnDirection(event);

            //if we shouldn't turn then abort everything and reset it
            if(!shouldTurn()) {
                mDirection = null;
                mIsTurning = false;
                return false;
            }
        }

        mLastTouchPoint = new Point((int) event.getX(), (int) event.getY());
        invalidate();

    } else if (event.getAction() == MotionEvent.ACTION_UP && mIsTurning) {

        int halfWidth = getMeasuredWidth() / 2;

        if (mLastTouchPoint.x > halfWidth) {
            final Runnable animationRunnable = new Runnable() {
                public void run() {
                    mLastTouchPoint.x += 20;
                    invalidate();

                    if (mLastTouchPoint.x < getMeasuredWidth())
                        mHandler.post(this);
                    else {
                        mIsTurning = false;

                        if (mDirection == PageTurnDirection.RIGHT)
                            mCurrentPage--;
                        mDirection = null;
                    }
                }
            };

            mHandler.post(animationRunnable);
        } else {

            final Runnable animationRunnable = new Runnable() {
                public void run() {
                    mLastTouchPoint.x -= 20;
                    invalidate();

                    if (mLastTouchPoint.x > -(getMeasuredWidth() / 2)) {
                        mHandler.post(this);
                    } else {
                        mIsTurning = false;

                        if (mDirection == PageTurnDirection.LEFT)
                            mCurrentPage++;
                        mDirection = null;
                    }
                }
            };

            mHandler.post(animationRunnable);

        }
    }

    return true;
}

0 个答案:

没有答案