下面列出了怎么用android.widget.Scroller的API类实例代码及写法,或者点击链接到github查看源代码。
public BaseReadView(Context context, String bookId, List<BookMixAToc.mixToc.Chapters> chaptersList,
OnReadStateChangeListener listener) {
super(context);
this.listener = listener;
this.bookId = bookId;
mScreenWidth = ScreenUtils.getScreenWidth();
mScreenHeight = ScreenUtils.getScreenHeight();
mCurPageBitmap = Bitmap.createBitmap(mScreenWidth, mScreenHeight, Bitmap.Config.ARGB_8888);
mNextPageBitmap = Bitmap.createBitmap(mScreenWidth, mScreenHeight, Bitmap.Config.ARGB_8888);
mCurrentPageCanvas = new Canvas(mCurPageBitmap);
mNextPageCanvas = new Canvas(mNextPageBitmap);
mScroller = new Scroller(getContext());
pagefactory = new PageFactory(getContext(), bookId, chaptersList);
pagefactory.setOnReadStateChangeListener(listener);
}
public PageWidget(Context context) {
super(context);
// TODO Auto-generated constructor stub
mPath0 = new Path();
mPath1 = new Path();
createDrawable();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);
ColorMatrix cm = new ColorMatrix();
float array[] = { 0.55f, 0, 0, 0, 80.0f, 0, 0.55f, 0, 0, 80.0f, 0, 0,
0.55f, 0, 80.0f, 0, 0, 0, 0.2f, 0 };
cm.set(array);
mColorMatrixFilter = new ColorMatrixColorFilter(cm);
mMatrix = new Matrix();
mScroller = new Scroller(getContext());
mTouch.x = 0.01f;
mTouch.y = 0.01f;
}
/**
* Attaches the {@link android.support.v7.widget.SnapHelper} to the provided RecyclerView, by calling
* {@link RecyclerView#setOnFlingListener(RecyclerView.OnFlingListener)}.
* You can call this method with {@code null} to detach it from the current RecyclerView.
*
* @param recyclerView The RecyclerView instance to which you want to add this helper or
* {@code null} if you want to remove SnapHelper from the current
* RecyclerView.
* @throws IllegalArgumentException if there is already a {@link RecyclerView.OnFlingListener}
* attached to the provided {@link RecyclerView}.
*/
public void attachToRecyclerView(@Nullable RecyclerView recyclerView)
throws IllegalStateException {
if (mRecyclerView == recyclerView) {
return; // nothing to do
}
if (mRecyclerView != null) {
destroyCallbacks();
}
mRecyclerView = recyclerView;
if (mRecyclerView != null) {
setupCallbacks();
mGravityScroller = new Scroller(mRecyclerView.getContext(),
new DecelerateInterpolator());
snapToTargetExistingView();
}
}
/**
* Apps should use ViewDragHelper.create() to get a new instance.
* This will allow VDH to use internal compatibility implementations for different
* platform versions.
*
* @param context Context to initialize config-dependent params from
* @param forParent Parent view to monitor
*/
private ViewDragHelper(Context context, ViewGroup forParent, Callback cb) {
if (forParent == null) {
throw new IllegalArgumentException("Parent view may not be null");
}
if (cb == null) {
throw new IllegalArgumentException("Callback may not be null");
}
mParentView = forParent;
mCallback = cb;
final ViewConfiguration vc = ViewConfiguration.get(context);
final float density = context.getResources().getDisplayMetrics().density;
mEdgeSize = (int) (EDGE_SIZE * density + 0.5f);
mTouchSlop = vc.getScaledTouchSlop();
mMaxVelocity = vc.getScaledMaximumFlingVelocity();
mMinVelocity = vc.getScaledMinimumFlingVelocity();
mScroller = new Scroller(context, sInterpolator);
}
private void init() {
final Context context = getContext();
final ViewConfiguration configuration = ViewConfiguration.get(context);
mScroller = new Scroller(context, flipInterpolator);
mTouchSlop = configuration.getScaledPagingTouchSlop();
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mShadowPaint.setColor(Color.BLACK);
mShadowPaint.setStyle(Style.FILL);
mShadePaint.setColor(Color.BLACK);
mShadePaint.setStyle(Style.FILL);
mShinePaint.setColor(Color.WHITE);
mShinePaint.setStyle(Style.FILL);
}
/**
* Move to the final position of a scroller. Ensures to force finish the scroller
* and if it is not at its final position a scroll of the selector wheel is
* performed to fast forward to the final position.
*
* @param scroller The scroller to whose final position to get.
* @return True of the a move was performed, i.e. the scroller was not in final position.
*/
private boolean moveToFinalScrollerPosition(Scroller scroller) {
scroller.forceFinished(true);
int amountToScroll = scroller.getFinalY() - scroller.getCurrY();
int futureScrollOffset = (mCurrentScrollOffset + amountToScroll) % mSelectorElementHeight;
int overshootAdjustment = mInitialScrollOffset - futureScrollOffset;
if (overshootAdjustment != 0) {
if (Math.abs(overshootAdjustment) > mSelectorElementHeight / 2) {
if (overshootAdjustment > 0) {
overshootAdjustment -= mSelectorElementHeight;
} else {
overshootAdjustment += mSelectorElementHeight;
}
}
amountToScroll += overshootAdjustment;
scrollBy(0, amountToScroll);
return true;
}
return false;
}
/**
* 通过scrollTo方法完成协调布局的滑动,其中主要使用了ViewCompat.postOnAnimation
*
* @param parent 协调布局parent
* @param child 协调布局协调滑动的child
* @param y 滑动目标位置y轴数值
* @param duration 滑动执行时间
* @return void
*/
public static void scrollTo(final CoordinatorLayout parent, final RecyclerView child, final int y, int duration) {
final Scroller scroller = new Scroller(parent.getContext());
scroller.startScroll(0, top, 0, y - top, duration); //设置scroller的滚动偏移量
ViewCompat.postOnAnimation(child, new Runnable() {
@Override
public void run() {
//返回值为boolean,true说明滚动尚未完成,false说明滚动已经完成。
// 这是一个很重要的方法,通常放在View.computeScroll()中,用来判断是否滚动是否结束。
if (scroller.computeScrollOffset()) {
int delta = scroller.getCurrY() - child.getTop();
child.offsetTopAndBottom(delta);
saveTop(child.getTop());
parent.dispatchDependentViewsChanged(child);
ViewCompat.postOnAnimation(child, this);
}
}
});
}
void initViewPager() {
setWillNotDraw(false);
setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
setFocusable(true);
final Context context = getContext();
mScroller = new Scroller(context, sInterpolator);
final ViewConfiguration configuration = ViewConfiguration.get(context);
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mLeftEdge = new EdgeEffectCompat(context);
mRightEdge = new EdgeEffectCompat(context);
final float density = context.getResources().getDisplayMetrics().density;
mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
mCloseEnough = (int) (CLOSE_ENOUGH * density);
mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);
ViewCompat.setAccessibilityDelegate(this, new MyAccessibilityDelegate());
if (ViewCompat.getImportantForAccessibility(this)
== ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
ViewCompat.setImportantForAccessibility(this,
ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
}
}
/**
* Attaches the {@link SnapHelper} to the provided RecyclerView, by calling
* {@link RecyclerView#setOnFlingListener(RecyclerView.OnFlingListener)}.
* You can call this method with {@code null} to detach it from the current RecyclerView.
*
* @param recyclerView The RecyclerView instance to which you want to add this helper or
* {@code null} if you want to remove SnapHelper from the current
* RecyclerView.
*
* @throws IllegalArgumentException if there is already a {@link RecyclerView.OnFlingListener}
* attached to the provided {@link RecyclerView}.
*
*/
public void attachToRecyclerView(@Nullable RecyclerView recyclerView)
throws IllegalStateException {
if (mRecyclerView == recyclerView) {
return; // nothing to do
}
if (mRecyclerView != null) {
destroyCallbacks();
}
mRecyclerView = recyclerView;
if (mRecyclerView != null) {
setupCallbacks();
mGravityScroller = new Scroller(mRecyclerView.getContext(),
new DecelerateInterpolator());
snapToTargetExistingView();
}
}
private void init() {
final Context context = getContext();
final ViewConfiguration configuration = ViewConfiguration.get(context);
mScroller = new Scroller(context, flipInterpolator);
mTouchSlop = configuration.getScaledPagingTouchSlop();
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mShadowPaint.setColor(Color.BLACK);
mShadowPaint.setStyle(Style.FILL);
mShadePaint.setColor(Color.BLACK);
mShadePaint.setStyle(Style.FILL);
mShinePaint.setColor(Color.WHITE);
mShinePaint.setStyle(Style.FILL);
}
void a()
{
setWillNotDraw(false);
setDescendantFocusability(0x40000);
setFocusable(true);
Context context = getContext();
u = new Scroller(context, l);
ViewConfiguration viewconfiguration = ViewConfiguration.get(context);
float f1 = context.getResources().getDisplayMetrics().density;
N = ViewConfigurationCompat.getScaledPagingTouchSlop(viewconfiguration);
V = (int)(400F * f1);
W = viewconfiguration.getScaledMaximumFlingVelocity();
ae = new EdgeEffectCompat(context);
af = new EdgeEffectCompat(context);
Z = (int)(25F * f1);
aa = (int)(2.0F * f1);
L = (int)(16F * f1);
ViewCompat.setAccessibilityDelegate(this, new aD(this));
if (ViewCompat.getImportantForAccessibility(this) == 0)
{
ViewCompat.setImportantForAccessibility(this, 1);
}
}
private void init() {
scroller = new Scroller(getContext(), new DecelerateInterpolator());
measureView(getHeaderView());
headHeight = getHeaderView().getMeasuredHeight();
getHeaderView().setPadding(0, -1 * headHeight, 0, 0);
getHeaderView().invalidate();
// this.setBackgroundResource(R.color.stand_default_bg_color);
//添加头部视图
super.addHeaderView(getHeaderView(), null, false);
setOnScrollListener(this);
super.setOnItemClickListener(this);
super.setOnItemLongClickListener(this);
super.setOnItemSelectedListener(this);
}
private void init() {
mJelloPaint = new Paint();
mJelloPaint.setAntiAlias(true);
mCheckedDrawable = getResources().getDrawable(R.drawable.checked);
mOnCheckDrawable = getResources().getDrawable(R.drawable.check_on);
mUnCheckedDrawable = getResources().getDrawable(R.drawable.uncheck);
setJelloState();
mJelloRect = new Rect();
mScroller = new Scroller(getContext());
mJelloPath = new Path();
mInterpolator = new EaseOutElasticInterpolator();
mDuration = DEFAULT_DURATION;
}
@Override
public void run() {
final Scroller scroller = mScroller;
boolean animationNotFinished = scroller.computeScrollOffset();
final int x = scroller.getCurrX();
int delta = x - mLastFlingX;
trackMotion(delta);
if (animationNotFinished) {
mLastFlingX = x;
post(this);
} else {
endFling();
}
}
void initViewPager() {
setWillNotDraw(false);
mScroller = new Scroller(getContext());
final ViewConfiguration configuration = ViewConfiguration.get(getContext());
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
private void init(Context context) {
WindowManager wm = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(dm);
mScreenHeight = dm.heightPixels;
mScroller = new Scroller(context);
}
void initCustomViewAbove() {
setWillNotDraw(false);
setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
setFocusable(true);
final Context context = getContext();
mScroller = new Scroller(context, sInterpolator);
final ViewConfiguration configuration = ViewConfiguration.get(context);
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
setInternalPageChangeListener(new SimpleOnPageChangeListener() {
public void onPageSelected(int position) {
if (mViewBehind != null) {
switch (position) {
case 0:
case 2:
mViewBehind.setChildrenEnabled(true);
break;
case 1:
mViewBehind.setChildrenEnabled(false);
break;
}
}
}
});
final float density = context.getResources().getDisplayMetrics().density;
mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
}
public HorScrollSelectedView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
setWillNotDraw(false);
// setClickable(true);
initAttrs(attrs);
initPaint();
mScroller = new Scroller(mContext);
}
/**
* Constructor
* @param context the current context
* @param listener the scrolling listener
*/
public WheelScroller(Context context, ScrollingListener listener) {
gestureDetector = new GestureDetector(context, gestureListener);
gestureDetector.setIsLongpressEnabled(false);
scroller = new Scroller(context);
this.listener = listener;
this.context = context;
}
public ScrollerView(Context context, AttributeSet attrs) {
super(context, attrs);
setClickable(true);
setLongClickable(true);
mScroller = new Scroller(context);
mGestureDetector = new GestureDetector(context, new CustomGestureListener());
}
private synchronized void initView() {
mLeftViewIndex = -1;
mRightViewIndex = 0;
mDisplayOffset = 0;
mCurrentX = 0;
mNextX = 0;
mMaxX = Integer.MAX_VALUE;
mScroller = new Scroller(getContext());
mGesture = new GestureDetector(getContext(), mOnGesture);
}
private void initBounce() {
// init the bouncy scroller, and make sure the layout is being drawn
// after the top padding
mScroller = new Scroller(getContext(), new OvershootInterpolator(OVERSHOOT_TENSION));
overScrollerSpringbackTask = new Runnable() {
@Override
public void run() {
// scroll till after the padding
mScroller.computeScrollOffset();
scrollTo(0, mScroller.getCurrY());
if (!mScroller.isFinished()) {
post(this);
}
}
};
prevScrollY = getPaddingTop();
try {
mScrollXField = View.class.getDeclaredField("mScrollX");
mScrollYField = View.class.getDeclaredField("mScrollY");
} catch (Exception e) {
hasFailedObtainingScrollFields = true;
}
}
private void initWithContext(Context context) {
mScroller = new Scroller(context, new DecelerateInterpolator());
// XListView need the scroll event, and it will dispatch the event to
// user's listener (as a proxy).
super.setOnScrollListener(this);
// init header view
mHeaderView = new MsgHeader(context);
mHeaderViewContent = (RelativeLayout) mHeaderView
.findViewById(R.id.msg_header_content);
// mHeaderTimeView = (TextView) mHeaderView
// .findViewById(R.id.xlistview_header_time);
addHeaderView(mHeaderView);
// init footer view
mFooterView = new XListViewFooter(context);
// init header height
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mHeaderViewHeight = mHeaderViewContent.getHeight();
getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
});
}
void initViewPager() {
setWillNotDraw(false);
setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
setFocusable(true);
final Context context = getContext();
mScroller = new Scroller(context, sInterpolator);
final ViewConfiguration configuration = ViewConfiguration.get(context);
final float density = context.getResources().getDisplayMetrics().density;
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
mMinimumVelocity = (int) (MIN_FLING_VELOCITY * density);
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mLeftEdge = new EdgeEffectCompat(context);
mRightEdge = new EdgeEffectCompat(context);
mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
mCloseEnough = (int) (CLOSE_ENOUGH * density);
mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);
ViewCompat.setAccessibilityDelegate(this, new MyAccessibilityDelegate());
if (ViewCompat.getImportantForAccessibility(this)
== ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
ViewCompat.setImportantForAccessibility(this,
ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
}
}
public GroupedPhotosListView(Context context, int paddingTop) {
super(context);
gestureDetector = new GestureDetector(context, this);
scroll = new Scroller(context);
itemWidth = AndroidUtilities.dp(42);
itemHeight = AndroidUtilities.dp(56);
itemSpacing = AndroidUtilities.dp(1);
itemY = paddingTop;
backgroundPaint.setColor(0x7f000000);
}
private Scroller makeOrGetScroller(Interpolator interpolator) {
if (interpolator == SPRING_INTERPOLATOR) {
return mCachedScroller[0];
} else if (interpolator == SPRING_BACK_INTERPOLATOR) {
return mCachedScroller[1];
} else if (interpolator == FLING_INTERPOLATOR) {
return mCachedScroller[2];
} else {
return new Scroller(getContext(), interpolator);
}
}
void initVerticalViewPager() {
setWillNotDraw(false);
setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
setFocusable(true);
final Context context = getContext();
mScroller = new Scroller(context, sInterpolator);
final ViewConfiguration configuration = ViewConfiguration.get(context);
final float density = context.getResources().getDisplayMetrics().density;
mTouchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);
mMinimumVelocity = (int) (MIN_FLING_VELOCITY * density);
mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
mTopEdge = new EdgeEffectCompat(context);
mBottomEdge = new EdgeEffectCompat(context);
mFlingDistance = (int) (MIN_DISTANCE_FOR_FLING * density);
mCloseEnough = (int) (CLOSE_ENOUGH * density);
mDefaultGutterSize = (int) (DEFAULT_GUTTER_SIZE * density);
ViewCompat.setAccessibilityDelegate(this, new MyAccessibilityDelegate());
if (ViewCompat.getImportantForAccessibility(this)
== ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
ViewCompat.setImportantForAccessibility(this,
ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
}
}
public MyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mWallpaperManager = WallpaperManager.getInstance(context);
mScroller = new Scroller(context);
mCurScreen = mDefaultScreen;
mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
}
public CompatScroller(Context context) {
if (VERSION.SDK_INT < VERSION_CODES.GINGERBREAD) {
isPreGingerbread = true;
scroller = new Scroller(context);
} else {
isPreGingerbread = false;
overScroller = new OverScroller(context);
}
}
public CompatScroller(Context context) {
if (VERSION.SDK_INT < VERSION_CODES.GINGERBREAD) {
isPreGingerbread = true;
scroller = new Scroller(context);
} else {
isPreGingerbread = false;
overScroller = new OverScroller(context);
}
}