Java tutorial
/* * Copyright (c) NoticeDog 2017. * GNU LESSER GENERAL PUBLIC LICENSE * Version 3, 29 June 2007 * * Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> * Everyone is permitted to copy and distribute verbatim copies * of this license document, but changing it is not allowed. * * * This version of the GNU Lesser General Public License incorporates * the terms and conditions of version 3 of the GNU General Public * License, supplemented by the additional permissions listed below. * * 0. Additional Definitions. * * As used herein, "this License" refers to version 3 of the GNU Lesser * General Public License, and the "GNU GPL" refers to version 3 of the GNU * General Public License. * * "The Library" refers to a covered work governed by this License, * other than an Application or a Combined Work as defined below. * * An "Application" is any work that makes use of an interface provided * by the Library, but which is not otherwise based on the Library. * Defining a subclass of a class defined by the Library is deemed a mode * of using an interface provided by the Library. * * A "Combined Work" is a work produced by combining or linking an * Application with the Library. The particular version of the Library * with which the Combined Work was made is also called the "Linked * Version". * * The "Minimal Corresponding Source" for a Combined Work means the * Corresponding Source for the Combined Work, excluding any source code * for portions of the Combined Work that, considered in isolation, are * based on the Application, and not on the Linked Version. * * The "Corresponding Application Code" for a Combined Work means the * object code and/or source code for the Application, including any data * and utility programs needed for reproducing the Combined Work from the * Application, but excluding the System Libraries of the Combined Work. * * 1. Exception to Section 3 of the GNU GPL. * * You may convey a covered work under sections 3 and 4 of this License * without being bound by section 3 of the GNU GPL. * * 2. Conveying Modified Versions. * * If you modify a copy of the Library, and, in your modifications, a * facility refers to a function or data to be supplied by an Application * that uses the facility (other than as an argument passed when the * facility is invoked), then you may convey a copy of the modified * version: * * a) under this License, provided that you make a good faith effort to * ensure that, in the event an Application does not supply the * function or data, the facility still operates, and performs * whatever part of its purpose remains meaningful, or * * b) under the GNU GPL, with none of the additional permissions of * this License applicable to that copy. * * 3. Object Code Incorporating Material from Library Header Files. * * The object code form of an Application may incorporate material from * a header file that is part of the Library. You may convey such object * code under terms of your choice, provided that, if the incorporated * material is not limited to numerical parameters, data structure * layouts and accessors, or small macros, inline functions and templates * (ten or fewer lines in length), you do both of the following: * * a) Give prominent notice with each copy of the object code that the * Library is used in it and that the Library and its use are * covered by this License. * * b) Accompany the object code with a copy of the GNU GPL and this license * document. * * 4. Combined Works. * * You may convey a Combined Work under terms of your choice that, * taken together, effectively do not restrict modification of the * portions of the Library contained in the Combined Work and reverse * engineering for debugging such modifications, if you also do each of * the following: * * a) Give prominent notice with each copy of the Combined Work that * the Library is used in it and that the Library and its use are * covered by this License. * * b) Accompany the Combined Work with a copy of the GNU GPL and this license * document. * * c) For a Combined Work that displays copyright notices during * execution, include the copyright notice for the Library among * these notices, as well as a reference directing the user to the * copies of the GNU GPL and this license document. * * d) Do one of the following: * * 0) Convey the Minimal Corresponding Source under the terms of this * License, and the Corresponding Application Code in a form * suitable for, and under terms that permit, the user to * recombine or relink the Application with a modified version of * the Linked Version to produce a modified Combined Work, in the * manner specified by section 6 of the GNU GPL for conveying * Corresponding Source. * * 1) Use a suitable shared library mechanism for linking with the * Library. A suitable mechanism is one that (a) uses at run time * a copy of the Library already present on the user's computer * system, and (b) will operate properly with a modified version * of the Library that is interface-compatible with the Linked * Version. * * e) Provide Installation Information, but only if you would otherwise * be required to provide such information under section 6 of the * GNU GPL, and only to the extent that such information is * necessary to install and execute a modified version of the * Combined Work produced by recombining or relinking the * Application with a modified version of the Linked Version. (If * you use option 4d0, the Installation Information must accompany * the Minimal Corresponding Source and Corresponding Application * Code. If you use option 4d1, you must provide the Installation * Information in the manner specified by section 6 of the GNU GPL * for conveying Corresponding Source.) * * 5. Combined Libraries. * * You may place library facilities that are a work based on the * Library side by side in a single library together with other library * facilities that are not Applications and are not covered by this * License, and convey such a combined library under terms of your * choice, if you do both of the following: * * a) Accompany the combined library with a copy of the same work based * on the Library, uncombined with any other library facilities, * conveyed under the terms of this License. * * b) Give prominent notice with the combined library that part of it * is a work based on the Library, and explaining where to find the * accompanying uncombined form of the same work. * * 6. Revised Versions of the GNU Lesser General Public License. * * The Free Software Foundation may publish revised and/or new versions * of the GNU Lesser General Public License from time to time. Such new * versions will be similar in spirit to the present version, but may * differ in detail to address new problems or concerns. * * Each version is given a distinguishing version number. If the * Library as you received it specifies that a certain numbered version * of the GNU Lesser General Public License "or any later version" * applies to it, you have the option of following the terms and * conditions either of that published version or of any later version * published by the Free Software Foundation. If the Library as you * received it does not specify a version number of the GNU Lesser * General Public License, you may choose any version of the GNU Lesser * General Public License ever published by the Free Software Foundation. * * If the Library as you received it specifies that a proxy can decide * whether future versions of the GNU Lesser General Public License shall * apply, that proxy's public statement of acceptance of any version is * permanent authorization for you to choose that version for the * Library. */ package io.bunnyblue.noticedog.app.ui.pageview; import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.Drawable; import android.os.Parcel; import android.os.Parcelable; import android.support.v4.view.MotionEventCompat; import android.support.v4.view.ViewConfigurationCompat; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewConfiguration; import io.bunnyblue.noticedog.app.R; public class LinePageIndicator extends View implements PageIndicator { private static final int INVALID_POINTER = -1; private final Paint mPaintSelected; private final Paint mPaintUnselected; private int mActivePointerId; private boolean mCentered; private int mCurrentPage; private float mGapWidth; private boolean mIsDragging; private float mLastMotionX; private float mLineWidth; private OnPageChangeListener mListener; private int mTouchSlop; private ViewPager mViewPager; public LinePageIndicator(Context context) { this(context, null); } public LinePageIndicator(Context context, AttributeSet attrs) { this(context, attrs, R.attr.vpiLinePageIndicatorStyle); } public LinePageIndicator(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.mPaintUnselected = new Paint(1); this.mPaintSelected = new Paint(1); this.mLastMotionX = -1.0f; this.mActivePointerId = -1; if (!isInEditMode()) { Resources res = getResources(); int defaultSelectedColor = res.getColor(R.color.default_line_indicator_selected_color); int defaultUnselectedColor = res.getColor(R.color.default_line_indicator_unselected_color); float defaultLineWidth = res.getDimension(R.dimen.default_line_indicator_line_width); float defaultGapWidth = res.getDimension(R.dimen.default_line_indicator_gap_width); float defaultStrokeWidth = res.getDimension(R.dimen.default_line_indicator_stroke_width); boolean defaultCentered = res.getBoolean(R.bool.default_line_indicator_centered); TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LinePageIndicator, defStyle, 0); this.mCentered = a.getBoolean(1, defaultCentered); this.mLineWidth = a.getDimension(5, defaultLineWidth); this.mGapWidth = a.getDimension(6, defaultGapWidth); setStrokeWidth(a.getDimension(3, defaultStrokeWidth)); this.mPaintUnselected.setColor(a.getColor(4, defaultUnselectedColor)); this.mPaintSelected.setColor(a.getColor(2, defaultSelectedColor)); Drawable background = a.getDrawable(0); if (background != null) { setBackgroundDrawable(background); } a.recycle(); this.mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(ViewConfiguration.get(context)); } } public boolean isCentered() { return this.mCentered; } public void setCentered(boolean centered) { this.mCentered = centered; invalidate(); } public int getUnselectedColor() { return this.mPaintUnselected.getColor(); } public void setUnselectedColor(int unselectedColor) { this.mPaintUnselected.setColor(unselectedColor); invalidate(); } public int getSelectedColor() { return this.mPaintSelected.getColor(); } public void setSelectedColor(int selectedColor) { this.mPaintSelected.setColor(selectedColor); invalidate(); } public float getLineWidth() { return this.mLineWidth; } public void setLineWidth(float lineWidth) { this.mLineWidth = lineWidth; invalidate(); } public float getStrokeWidth() { return this.mPaintSelected.getStrokeWidth(); } public void setStrokeWidth(float lineHeight) { this.mPaintSelected.setStrokeWidth(lineHeight); this.mPaintUnselected.setStrokeWidth(lineHeight); invalidate(); } public float getGapWidth() { return this.mGapWidth; } public void setGapWidth(float gapWidth) { this.mGapWidth = gapWidth; invalidate(); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (this.mViewPager != null) { int count = this.mViewPager.getAdapter().getCount(); if (count == 0) { return; } if (this.mCurrentPage >= count) { setCurrentItem(count - 1); return; } float lineWidthAndGap = this.mLineWidth + this.mGapWidth; float indicatorWidth = (((float) count) * lineWidthAndGap) - this.mGapWidth; float paddingTop = (float) getPaddingTop(); float paddingLeft = (float) getPaddingLeft(); float paddingRight = (float) getPaddingRight(); float verticalOffset = paddingTop + (((((float) getHeight()) - paddingTop) - ((float) getPaddingBottom())) / 2.0f); float horizontalOffset = paddingLeft; if (this.mCentered) { horizontalOffset += (((((float) getWidth()) - paddingLeft) - paddingRight) / 2.0f) - (indicatorWidth / 2.0f); } int i = 0; while (i < count) { float dx1 = horizontalOffset + (((float) i) * lineWidthAndGap); canvas.drawLine(dx1, verticalOffset, dx1 + this.mLineWidth, verticalOffset, i == this.mCurrentPage ? this.mPaintSelected : this.mPaintUnselected); i++; } } } public boolean onTouchEvent(MotionEvent ev) { if (super.onTouchEvent(ev)) { return true; } if (this.mViewPager == null || this.mViewPager.getAdapter().getCount() == 0) { return false; } int action = ev.getAction() & MotionEventCompat.ACTION_MASK; switch (action) { case 0: this.mActivePointerId = MotionEventCompat.getPointerId(ev, 0); this.mLastMotionX = ev.getX(); break; case 1: case 3: if (!this.mIsDragging) { int count = this.mViewPager.getAdapter().getCount(); int width = getWidth(); float halfWidth = ((float) width) / 2.0f; float sixthWidth = ((float) width) / 6.0f; if (this.mCurrentPage > 0 && ev.getX() < halfWidth - sixthWidth) { if (action != 3) { this.mViewPager.setCurrentItem(this.mCurrentPage - 1); } return true; } else if (this.mCurrentPage < count - 1 && ev.getX() > halfWidth + sixthWidth) { if (action != 3) { this.mViewPager.setCurrentItem(this.mCurrentPage + 1); } return true; } } this.mIsDragging = false; this.mActivePointerId = -1; if (this.mViewPager.isFakeDragging()) { this.mViewPager.endFakeDrag(); break; } break; case 2: float x = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, this.mActivePointerId)); float deltaX = x - this.mLastMotionX; if (!this.mIsDragging && Math.abs(deltaX) > ((float) this.mTouchSlop)) { this.mIsDragging = true; } if (this.mIsDragging) { this.mLastMotionX = x; if (this.mViewPager.isFakeDragging() || this.mViewPager.beginFakeDrag()) { this.mViewPager.fakeDragBy(deltaX); break; } } break; case 5: int index = MotionEventCompat.getActionIndex(ev); this.mLastMotionX = MotionEventCompat.getX(ev, index); this.mActivePointerId = MotionEventCompat.getPointerId(ev, index); break; case 6: int pointerIndex = MotionEventCompat.getActionIndex(ev); if (MotionEventCompat.getPointerId(ev, pointerIndex) == this.mActivePointerId) { this.mActivePointerId = MotionEventCompat.getPointerId(ev, pointerIndex == 0 ? 1 : 0); } this.mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, this.mActivePointerId)); break; } return true; } public void setViewPager(ViewPager viewPager) { if (this.mViewPager != viewPager) { if (this.mViewPager != null) { this.mViewPager.setOnPageChangeListener(null); } if (viewPager.getAdapter() == null) { throw new IllegalStateException("ViewPager does not have adapter instance."); } this.mViewPager = viewPager; this.mViewPager.setOnPageChangeListener(this); invalidate(); } } public void setViewPager(ViewPager view, int initialPosition) { setViewPager(view); setCurrentItem(initialPosition); } public void setCurrentItem(int item) { if (this.mViewPager == null) { throw new IllegalStateException("ViewPager has not been bound."); } this.mViewPager.setCurrentItem(item); this.mCurrentPage = item; invalidate(); } public void notifyDataSetChanged() { invalidate(); } public void onPageScrollStateChanged(int state) { if (this.mListener != null) { this.mListener.onPageScrollStateChanged(state); } } public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { if (this.mListener != null) { this.mListener.onPageScrolled(position, positionOffset, positionOffsetPixels); } } public void onPageSelected(int position) { this.mCurrentPage = position; invalidate(); if (this.mListener != null) { this.mListener.onPageSelected(position); } } public void setOnPageChangeListener(OnPageChangeListener listener) { this.mListener = listener; } protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); } private int measureWidth(int measureSpec) { float result; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == 1073741824 || this.mViewPager == null) { result = (float) specSize; } else { int count = this.mViewPager.getAdapter().getCount(); result = (((float) (getPaddingLeft() + getPaddingRight())) + (((float) count) * this.mLineWidth)) + (((float) (count - 1)) * this.mGapWidth); if (specMode == Integer.MIN_VALUE) { result = Math.min(result, (float) specSize); } } return (int) Math.ceil(result); } private int measureHeight(int measureSpec) { float result; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == 1073741824) { result = (float) specSize; } else { result = (this.mPaintSelected.getStrokeWidth() + ((float) getPaddingTop())) + ((float) getPaddingBottom()); if (specMode == Integer.MIN_VALUE) { result = Math.min(result, (float) specSize); } } return (int) Math.ceil(result); } public void onRestoreInstanceState(Parcelable state) { SavedState savedState = (SavedState) state; super.onRestoreInstanceState(savedState.getSuperState()); this.mCurrentPage = savedState.currentPage; requestLayout(); } public Parcelable onSaveInstanceState() { SavedState savedState = new SavedState(super.onSaveInstanceState()); savedState.currentPage = this.mCurrentPage; return savedState; } static class SavedState extends BaseSavedState { public static final Creator<SavedState> CREATOR = new Creator<SavedState>() { public SavedState createFromParcel(Parcel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; int currentPage; public SavedState(Parcelable superState) { super(superState); } private SavedState(Parcel in) { super(in); this.currentPage = in.readInt(); } public void writeToParcel(Parcel dest, int flags) { super.writeToParcel(dest, flags); dest.writeInt(this.currentPage); } } }