public void transformPage(View view, float position) { int pageWidth = view.getWidth(); if (position < -1) { // [-Infinity,-1) // This page is way off-screen to the left. ViewHelper.setAlpha(view, 0); } else if (position <= 0) { // [-1,0] // Use the default slide transition when moving to the left page ViewHelper.setAlpha(view, 1); ViewHelper.setTranslationX(view, 0); ViewHelper.setScaleX(view, 1); ViewHelper.setScaleY(view, 1); } else if (position <= 1) { // (0,1] // Fade the page out. ViewHelper.setAlpha(view, 1 - position); // Counteract the default slide transition ViewHelper.setTranslationX(view, pageWidth * -position); // Scale the page down (between MIN_SCALE and 1) float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position)); ViewHelper.setScaleX(view, scaleFactor); ViewHelper.setScaleY(view, scaleFactor); } else { // (1,+Infinity] // This page is way off-screen to the right. ViewHelper.setAlpha(view, 0); } }
protected void animateFade(View left, View right, float positionOffset) { if (left != null) { ViewHelper.setAlpha(left, 1 - positionOffset); } if (right != null) { ViewHelper.setAlpha(right, positionOffset); } }
private void animateTitles() { final View prevTextView = getChildAt(0); final View nextTextView = getChildAt(getChildCount() - 1); if (prevTextView == null || nextTextView == null) { return; } // Set up initial values for the views that will be animated. ViewHelper.setTranslationX(prevTextView, -INIT_OFFSET); ViewHelper.setAlpha(prevTextView, 0); ViewHelper.setTranslationX(nextTextView, INIT_OFFSET); ViewHelper.setAlpha(nextTextView, 0); // Alpha animations. final ValueAnimator alpha1 = ObjectAnimator.ofFloat(prevTextView, "alpha", 1); final ValueAnimator alpha2 = ObjectAnimator.ofFloat(nextTextView, "alpha", 1); final AnimatorSet alphaAnimatorSet = new AnimatorSet(); alphaAnimatorSet.playTogether(alpha1, alpha2); alphaAnimatorSet.setDuration(ALPHA_MS); alphaAnimatorSet.setStartDelay(ANIMATION_DELAY_MS); // Bounce animation. final float bounceDistance = getWidth() / 100f; // Hack: TextFields still have 0 width here. final BounceAnimatorBuilder prevBounceAnimatorBuilder = new BounceAnimatorBuilder(prevTextView, "translationX"); prevBounceAnimatorBuilder.queue(new Attributes(bounceDistance, BOUNCE1_MS)); prevBounceAnimatorBuilder.queue(new Attributes(-bounceDistance / 4, BOUNCE2_MS)); prevBounceAnimatorBuilder.queue(new Attributes(0, BOUNCE3_MS)); final BounceAnimatorBuilder nextBounceAnimatorBuilder = new BounceAnimatorBuilder(nextTextView, "translationX"); nextBounceAnimatorBuilder.queue(new Attributes(-bounceDistance, BOUNCE1_MS)); nextBounceAnimatorBuilder.queue(new Attributes(bounceDistance / 4, BOUNCE2_MS)); nextBounceAnimatorBuilder.queue(new Attributes(0, BOUNCE3_MS)); final AnimatorSet bounceAnimatorSet = new AnimatorSet(); bounceAnimatorSet.playTogether( prevBounceAnimatorBuilder.build(), nextBounceAnimatorBuilder.build()); TransitionsTracker.track(nextBounceAnimatorBuilder); final AnimatorSet titlesAnimatorSet = new AnimatorSet(); titlesAnimatorSet.playTogether(alphaAnimatorSet, bounceAnimatorSet); titlesAnimatorSet.setStartDelay(ANIMATION_DELAY_MS); // Start animations. titlesAnimatorSet.start(); }
/** 滚动发生时调用 */ @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); /** * 距离qq差别 :1.qq内容区域实现了一个缩放的效果 2.菜单的偏移量一直在变化 3.菜单也存在缩放和透明度的变化 实现1.需要将 0.7 ~ 1.0 (0.7 + 0.3 * * scale) 实现2.修改菜单的其实隐藏跨度 实现3.菜单透明度变化0.6 ~ 1.0 ( 0.6 + 0.4 *(1-scale))+缩放:0.7~1.0(1.0 - * scale*0.3) */ // l是当前SrocllX值 float scale = l * 1.0f / mMenuWidth; Log.i("SlidingMenu:onScrollChanged", "" + scale); // 调用属性动画 设置TranslationX 修改菜单的其实隐藏跨度,其实隐藏60% ViewHelper.setTranslationX(mMenu, mMenuWidth * scale * 0.6f); // 实现内容区域缩小 float contextScale = 0.7f + 0.3f * scale; // 设置缩放的中心点 设置内容区域缩放值 ViewHelper.setPivotX(mContent, 0); ViewHelper.setPivotY(mContent, mContent.getHeight() / 2); ViewHelper.setScaleX(mContent, contextScale); ViewHelper.setScaleY(mContent, contextScale); // 实现菜单区域缩放 float menuScale = 1.0f - scale * 0.3f; // 实现菜单透明度变化 float menuAlpha = 0.6f + 0.4f * (1 - scale); // 调用属性动画实现 ViewHelper.setScaleX(mMenu, menuScale); ViewHelper.setScaleY(mMenu, menuScale); ViewHelper.setAlpha(mMenu, menuAlpha); }
private void removePendingDismisses(int originalHeight) { // No active animations, process all pending dismisses. // Sort by descending position Collections.sort(pendingDismisses); int[] dismissPositions = new int[pendingDismisses.size()]; for (int i = pendingDismisses.size() - 1; i >= 0; i--) { dismissPositions[i] = pendingDismisses.get(i).position; } swipeListView.onDismiss(dismissPositions); ViewGroup.LayoutParams lp; for (PendingDismissData pendingDismiss : pendingDismisses) { // Reset view presentation if (pendingDismiss.view != null) { setAlpha(pendingDismiss.view, 1f); setTranslationX(pendingDismiss.view, 0); lp = pendingDismiss.view.getLayoutParams(); lp.height = originalHeight; pendingDismiss.view.setLayoutParams(lp); } } resetPendingDismisses(); }
public LoadToast(Context context) { mView = new LoadToastView(context); mParentView = (ViewGroup) ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content); mParentView.addView( mView, new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); ViewHelper.setAlpha(mView, 0); mParentView.postDelayed( new Runnable() { @Override public void run() { ViewHelper.setTranslationX(mView, (mParentView.getWidth() - mView.getWidth()) / 2); ViewHelper.setTranslationY(mView, -mView.getHeight() + mTranslationY); mInflated = true; if (!mToastCanceled && mShowCalled) show(); } }, 1); mParentView .getViewTreeObserver() .addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { checkZPosition(); } }); }
/** * Modify dragged view alpha based on the horizontal position while the view is being horizontally * dragged. */ void changeDragViewViewAlpha() { if (enableHorizontalAlphaEffect) { float alpha = 1 - getHorizontalDragOffset(); if (alpha == 0) { alpha = 1; } ViewHelper.setAlpha(dragView, alpha); } }
@Override protected void onTransform(View view, float position) { final float scale = position < 0 ? position + 1f : Math.abs(1f - position); ViewHelper.setScaleX(view, scale); ViewHelper.setScaleY(view, scale); ViewHelper.setPivotX(view, view.getWidth() * 0.5f); ViewHelper.setPivotY(view, view.getHeight() * 0.5f); ViewHelper.setAlpha(view, position < -1f || position > 1f ? 0f : 1f - (scale - 1f)); }
@Override public void initView(View item, int position, int scrollDirection) { ViewHelper.setPivotX(item, item.getWidth() / 2); ViewHelper.setPivotY(item, item.getHeight() / 2); ViewHelper.setScaleX(item, INITIAL_SCALE_FACTOR); ViewHelper.setScaleY(item, INITIAL_SCALE_FACTOR); ViewHelper.setTranslationY(item, item.getHeight() / 2 * scrollDirection); ViewHelper.setAlpha(item, JazzyHelper.OPAQUE / 2); }
/** * Set either minutes or hours as showing. * * @param animate True to animate the transition, false to show with no animation. */ public void setCurrentItemShowing(int index, boolean animate) { if (index != HOUR_INDEX && index != MINUTE_INDEX) { Log.e(TAG, "TimePicker does not support view at index " + index); return; } int lastIndex = getCurrentItemShowing(); mCurrentItemShowing = index; if (animate && (index != lastIndex)) { List<ObjectAnimator> anims = new ArrayList<ObjectAnimator>(); if (index == MINUTE_INDEX) { anims.addAll(mHourRadialTextsView.getDisappearAnimator()); anims.addAll(mHourRadialSelectorView.getDisappearAnimator()); anims.addAll(mMinuteRadialTextsView.getReappearAnimator()); anims.addAll(mMinuteRadialSelectorView.getReappearAnimator()); } else if (index == HOUR_INDEX) { anims.addAll(mHourRadialTextsView.getReappearAnimator()); anims.addAll(mHourRadialSelectorView.getReappearAnimator()); anims.addAll(mMinuteRadialTextsView.getDisappearAnimator()); anims.addAll(mMinuteRadialSelectorView.getDisappearAnimator()); } if (mTransition != null && mTransition.isRunning()) { mTransition.end(); } mTransition = new AnimatorSet(); mTransition.playTogether(anims.toArray(new ObjectAnimator[anims.size()])); mTransition.start(); } else { int hourAlpha = (index == HOUR_INDEX) ? 255 : 0; int minuteAlpha = (index == MINUTE_INDEX) ? 255 : 0; Log.i( TAG, "set current item showing, no animate, hour alpha:" + hourAlpha + ", min alpha:" + minuteAlpha); ViewHelper.setAlpha(mHourRadialTextsView, hourAlpha); ViewHelper.setAlpha(mHourRadialSelectorView, hourAlpha); ViewHelper.setAlpha(mMinuteRadialTextsView, minuteAlpha); ViewHelper.setAlpha(mMinuteRadialSelectorView, minuteAlpha); } }
public static void reset(View view) { ViewHelper.setAlpha(view, 1); ViewHelper.setScaleX(view, 1); ViewHelper.setScaleY(view, 1); ViewHelper.setTranslationX(view, 0); ViewHelper.setTranslationY(view, 0); ViewHelper.setRotation(view, 0); ViewHelper.setRotationY(view, 0); ViewHelper.setRotationX(view, 0); }
@Override public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) { // Translate overlay and image float flexibleRange = mFlexibleSpaceImageHeight - mActionBarSize; int minOverlayTransitionY = mActionBarSize - mOverlayView.getHeight(); ViewHelper.setTranslationY( mOverlayView, ScrollUtils.getFloat(-scrollY, minOverlayTransitionY, 0)); ViewHelper.setTranslationY( mImageView, ScrollUtils.getFloat(-scrollY / 2, minOverlayTransitionY, 0)); // Change alpha of overlay ViewHelper.setAlpha(mOverlayView, ScrollUtils.getFloat((float) scrollY / flexibleRange, 0, 1)); // Scale title text float scale = 1 + ScrollUtils.getFloat( (flexibleRange - scrollY) / flexibleRange, 0, MAX_TEXT_SCALE_DELTA); ViewHelper.setPivotX(mTitleView, 0); ViewHelper.setPivotY(mTitleView, 0); ViewHelper.setScaleX(mTitleView, scale); ViewHelper.setScaleY(mTitleView, scale); // Translate title text int maxTitleTranslationY = (int) (mFlexibleSpaceImageHeight - mTitleView.getHeight() * scale); int titleTranslationY = maxTitleTranslationY - scrollY; ViewHelper.setTranslationY(mTitleView, titleTranslationY); // Translate FAB int maxFabTranslationY = mFlexibleSpaceImageHeight - mFab.getHeight() / 2; float fabTranslationY = ScrollUtils.getFloat( -scrollY + mFlexibleSpaceImageHeight - mFab.getHeight() / 2, mActionBarSize - mFab.getHeight() / 2, maxFabTranslationY); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { // On pre-honeycomb, ViewHelper.setTranslationX/Y does not set margin, // which causes FAB's OnClickListener not working. FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mFab.getLayoutParams(); lp.leftMargin = mOverlayView.getWidth() - mFabMargin - mFab.getWidth(); lp.topMargin = (int) fabTranslationY; mFab.requestLayout(); } else { ViewHelper.setTranslationX(mFab, mOverlayView.getWidth() - mFabMargin - mFab.getWidth()); ViewHelper.setTranslationY(mFab, fabTranslationY); } // Show/hide FAB if (fabTranslationY < mFlexibleSpaceShowFabOffset) { hideFab(); } else { showFab(); } }
/** * reset the view to default status * * @param target */ public void reset(View target) { ViewHelper.setAlpha(target, 1); ViewHelper.setScaleX(target, 1); ViewHelper.setScaleY(target, 1); ViewHelper.setTranslationX(target, 0); ViewHelper.setTranslationY(target, 0); ViewHelper.setRotation(target, 0); ViewHelper.setRotationY(target, 0); ViewHelper.setRotationX(target, 0); ViewHelper.setPivotX(target, target.getMeasuredWidth() / 2.0f); ViewHelper.setPivotY(target, target.getMeasuredHeight() / 2.0f); }
@Override protected void onTransform(View view, float position) { final float scale = 1f + Math.abs(position); ViewHelper.setScaleX(view, scale); ViewHelper.setScaleY(view, scale); ViewHelper.setPivotX(view, view.getWidth() * 0.5f); ViewHelper.setPivotY(view, view.getWidth() * 0.5f); ViewHelper.setAlpha(view, position < -1f || position > 1f ? 0f : 1f - (scale - 1f)); if (position < -0.9) { // -0.9 to prevent a small bug ViewHelper.setTranslationX(view, view.getWidth() * position); } }
/** 显示模糊背景 */ protected void showShowdown() { ViewHelper.setTranslationY(mRootView, 0); mEffect.effect(mParentVG, mBg); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); mParentVG.addView(mBg, lp); ViewHelper.setAlpha(mBg, 0); ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mBg, "alpha", 0, 1); objectAnimator.setDuration(400); objectAnimator.start(); }
private void translateTab(int scrollY, boolean animated) { int flexibleSpaceImageHeight = getResources().getDimensionPixelSize(R.dimen.flexible_space_image_height); int tabHeight = getResources().getDimensionPixelSize(R.dimen.tab_height); View imageView = findViewById(R.id.image); View overlayView = findViewById(R.id.overlay); TextView titleView = (TextView) findViewById(R.id.title); // Translate overlay and image float flexibleRange = flexibleSpaceImageHeight - getActionBarSize(); int minOverlayTransitionY = tabHeight - overlayView.getHeight(); ViewHelper.setTranslationY( overlayView, ScrollUtils.getFloat(-scrollY, minOverlayTransitionY, 0)); ViewHelper.setTranslationY( imageView, ScrollUtils.getFloat(-scrollY / 2, minOverlayTransitionY, 0)); // Change alpha of overlay ViewHelper.setAlpha(overlayView, ScrollUtils.getFloat((float) scrollY / flexibleRange, 0, 1)); // Scale title text float scale = 1 + ScrollUtils.getFloat( (flexibleRange - scrollY - tabHeight) / flexibleRange, 0, MAX_TEXT_SCALE_DELTA); setPivotXToTitle(titleView); ViewHelper.setPivotY(titleView, 0); ViewHelper.setScaleX(titleView, scale); ViewHelper.setScaleY(titleView, scale); // Translate title text int maxTitleTranslationY = flexibleSpaceImageHeight - tabHeight - getActionBarSize(); int titleTranslationY = maxTitleTranslationY - scrollY; ViewHelper.setTranslationY(titleView, titleTranslationY); // If tabs are moving, cancel it to start a new animation. ViewPropertyAnimator.animate(mSlidingTabLayout).cancel(); // Tabs will move between the top of the screen to the bottom of the image. float translationY = ScrollUtils.getFloat( -scrollY + mFlexibleSpaceHeight - mTabHeight, 0, mFlexibleSpaceHeight - mTabHeight); if (animated) { // Animation will be invoked only when the current tab is changed. ViewPropertyAnimator.animate(mSlidingTabLayout) .translationY(translationY) .setDuration(200) .start(); } else { // When Fragments' scroll, translate tabs immediately (without animation). ViewHelper.setTranslationY(mSlidingTabLayout, translationY); } }
@Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); float scale = l * 1.0f / mMenuWidth; float leftScale = 1 - 0.3f * scale; // float rightScale = 0.8f + scale * 0.2f; // ViewHelper.setScaleX(mMenu, leftScale); // ViewHelper.setScaleY(mMenu, leftScale); ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale)); ViewHelper.setTranslationX(mMenu, mMenuWidth * scale * 0.7f); /*ViewHelper.setPivotX(mContent, 0); ViewHelper.setPivotY(mContent, mContent.getHeight() / 2); ViewHelper.setScaleX(mContent, rightScale); ViewHelper.setScaleY(mContent, rightScale);*/ }
/** * Moves the view * * @param deltaX delta */ public void move(float deltaX) { swipeRecyclerView.onMove(downPosition, deltaX); float posX = ViewHelper.getX(frontView); if (opened.get(downPosition)) { posX += openedRight.get(downPosition) ? -viewWidth + rightOffset : viewWidth - leftOffset; } if (posX > 0 && !swipingRight) { if (SwipeRecyclerView.DEBUG) { Log.d(SwipeRecyclerView.TAG, "change to right"); } swipingRight = !swipingRight; swipeCurrentAction = swipeActionRight; if (swipeCurrentAction == SwipeRecyclerView.SWIPE_ACTION_CHOICE) { backView.setVisibility(View.GONE); } else { backView.setVisibility(View.VISIBLE); } } if (posX < 0 && swipingRight) { if (SwipeRecyclerView.DEBUG) { Log.d(SwipeRecyclerView.TAG, "change to left"); } swipingRight = !swipingRight; swipeCurrentAction = swipeActionLeft; if (swipeCurrentAction == SwipeRecyclerView.SWIPE_ACTION_CHOICE) { backView.setVisibility(View.GONE); } else { backView.setVisibility(View.VISIBLE); } } if (swipeCurrentAction == SwipeRecyclerView.SWIPE_ACTION_DISMISS) { setTranslationX(parentView, deltaX); setAlpha(parentView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / viewWidth))); } else if (swipeCurrentAction == SwipeRecyclerView.SWIPE_ACTION_CHOICE) { if ((swipingRight && deltaX > 0 && posX < DISPLACE_CHOICE) || (!swipingRight && deltaX < 0 && posX > -DISPLACE_CHOICE) || (swipingRight && deltaX < DISPLACE_CHOICE) || (!swipingRight && deltaX > -DISPLACE_CHOICE)) { setTranslationX(frontView, deltaX); } } else { setTranslationX(frontView, deltaX); } }
public LoadToast show() { if (!mInflated) { mShowCalled = true; return this; } mView.show(); ViewHelper.setTranslationX(mView, (mParentView.getWidth() - mView.getWidth()) / 2); ViewHelper.setAlpha(mView, 0f); ViewHelper.setTranslationY(mView, -mView.getHeight() + mTranslationY); // mView.setVisibility(View.VISIBLE); ViewPropertyAnimator.animate(mView) .alpha(1f) .translationY(25 + mTranslationY) .setInterpolator(new DecelerateInterpolator()) .setDuration(300) .setStartDelay(0) .start(); return this; }
public void show(final View view) { pushedView = view; if (this.getParent() != null) return; synchronized (Snackbar.class) { if (!next.contains(this)) next.add(this); if (next.indexOf(this) == 0) { View decor = ((Activity) getContext()).getWindow().getDecorView(); ((ViewGroup) decor.findViewById(android.R.id.content)) .addView( this, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); ViewHelper.setAlpha(content, 0); AnimUtils.flyIn(content, null); if (view != null) { ValueAnimator animator = ValueAnimator.ofFloat(0, -1); animator.setDuration(200); animator.setInterpolator(new DecelerateInterpolator()); animator.addUpdateListener( new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { MarginLayoutParams lp = (MarginLayoutParams) content.getLayoutParams(); Log.e( "snackbar " + Snackbar.this.hashCode(), "" + ((content.getHeight() + lp.bottomMargin) * (Float) valueAnimator.getAnimatedValue())); ViewHelper.setTranslationY( view, (content.getHeight() + lp.bottomMargin) * (Float) valueAnimator.getAnimatedValue()); if (pushedView.getParent() != null) ((View) pushedView.getParent()).postInvalidate(); } }); animator.start(); } if (duration != INFINITE) handler.postDelayed(hideRunnable, duration); } } }
/** * start any animation through percent * * @param percent the percent start any animation */ private void setAnimation(float percent) { // the left panel's animation // the left panel's scale of x is 0.5-1 ViewHelper.setScaleX(leftGroup, evalute(percent, 0.5f, 1.0f)); // the left panel's scale of y is 0.5-1 ViewHelper.setScaleY(leftGroup, evalute(percent, 0.5f, 1.0f)); // the left panel's tranlation range is -rangewidth-0 ViewHelper.setTranslationX(leftGroup, evalute(percent, -mRangeWidth, 0)); // the left panel's alpha range is 0.5-1 ViewHelper.setAlpha(leftGroup, evalute(percent, 0.5f, 1.0f)); // the main panel's animation // the main panel's scale of x is 1.0-0.8 ViewHelper.setScaleX(mainGroup, evalute(percent, 1.0f, 0.8f)); // the main panel's scale of y is 1.0-0.8 ViewHelper.setScaleY(mainGroup, evalute(percent, 1.0f, 0.8f)); // the background's animation getBackground() .setColorFilter( (Integer) evaluateColor(percent, Color.BLACK, Color.TRANSPARENT), Mode.SRC_OVER); }
@Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { // 首次使用获取banner初始高度和宽度 if (originHeight == -1 || originWidth == -1) { originHeight = mTopView.getHeight(); originWidth = mTopView.getWidth(); } // t为ScrollView向上滑动的量, 两者相减即为当前banner所需的高度 int height = originHeight - t; // 计算缩放比例 float newRatio = (float) height / (float) originHeight; // 修改banner的属性 if (height >= 0) { ViewHelper.setScaleX(mTopView, newRatio); ViewHelper.setScaleY(mTopView, newRatio); ViewHelper.setY(mTopView, t / 2); ViewHelper.setAlpha(mTopView, newRatio); } super.onScrollChanged(l, t, oldl, oldt); }
@Override public void setValue(View object, float value) { ViewHelper.setAlpha(object, value); }
@Override public boolean onTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mListView.getWidth(); } switch (motionEvent.getActionMasked()) { case MotionEvent.ACTION_DOWN: { if (mPaused) { return false; } // TODO: ensure this is a finger, and set a flag // Find the child view that was touched (perform a hit test) Rect rect = new Rect(); int childCount = mListView.getChildCount(); int[] listViewCoords = new int[2]; mListView.getLocationOnScreen(listViewCoords); int x = (int) motionEvent.getRawX() - listViewCoords[0]; int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = mListView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { mDownView = child; break; } } if (mDownView != null) { mDownX = motionEvent.getRawX(); mDownPosition = mListView.getPositionForView(mDownView); if (((ListItem) mListView.getAdapter().getItem(mDownPosition)).getId() < 0) { return true; } mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } view.onTouchEvent(motionEvent); return true; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } float deltaX = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = Math.abs(mVelocityTracker.getXVelocity()); float velocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX) { dismiss = true; dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; animate(mDownView) .translationX(dismissRight ? mViewWidth : -mViewWidth) .alpha(0) .setDuration(mAnimationTime) .setListener( new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { performDismiss(downView, downPosition); } }); } else { // cancel animate(mDownView) .translationX(0) .alpha(1) .setDuration(mAnimationTime) .setListener(null); } mVelocityTracker = null; mDownX = 0; mDownView = null; mDownPosition = ListView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; if (Math.abs(deltaX) > mSlop) { mSwiping = true; mListView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction( MotionEvent.ACTION_CANCEL | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); mListView.onTouchEvent(cancelEvent); } if (mSwiping) { setTranslationX(mDownView, deltaX); setAlpha( mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } break; } } return false; }
@Override public boolean onTouch(final View view, final MotionEvent motionEvent) { if (mViewWidth < 2) { mViewWidth = mListView.getWidth(); } switch (motionEvent.getActionMasked()) { case MotionEvent.ACTION_DOWN: { if (mPaused) { return false; } // Find the child view that was touched (perform a hit test) final Rect rect = new Rect(); final int childCount = mListView.getChildCount(); final int[] listViewCoords = new int[2]; mListView.getLocationOnScreen(listViewCoords); final int x = (int) motionEvent.getRawX() - listViewCoords[0]; final int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = mListView.getChildAt(i); child.getHitRect(rect); if (rect.contains(x, y)) { mDownView = child; break; } } if (mDownView != null) { mDownX = motionEvent.getRawX(); mDownPosition = mListView.getPositionForView(mDownView); mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } view.onTouchEvent(motionEvent); return true; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } final float deltaX = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); final float velocityX = Math.abs(mVelocityTracker.getXVelocity()); final float velocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX && mSwiping && isDirectionValid(mVelocityTracker.getXVelocity()) && deltaX >= mViewWidth * 0.2f) { dismiss = true; dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss) { // dismiss final View downView = mDownView; // mDownView gets null'd before animation ends final int downPosition = mDownPosition; ++mDismissAnimationRefCount; animate(mDownView) .translationX(dismissRight ? mViewWidth : -mViewWidth) .alpha(0) .setDuration(mAnimationTime) .setListener( new AnimatorListenerAdapter() { @Override public void onAnimationEnd(final Animator animation) { performDismiss(downView, downPosition); } }); } else { // cancel animate(mDownView) .translationX(0) .alpha(1) .setDuration(mAnimationTime) .setListener(null); } mVelocityTracker = null; mDownX = 0; mDownView = null; mDownPosition = AdapterView.INVALID_POSITION; mSwiping = false; break; } case MotionEvent.ACTION_MOVE: { if (mUndoPopup.isShowing()) { // Send a delayed message to hide popup mHandler.sendMessageDelayed(mHandler.obtainMessage(mDelayedMsgId), mAutoHideDelay); } if (mVelocityTracker == null || mPaused) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; // Only start swipe in correct direction if (isDirectionValid(deltaX)) { if (Math.abs(deltaX) > mSlop) { mSwiping = true; mListView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) final MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction( MotionEvent.ACTION_CANCEL | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); mListView.onTouchEvent(cancelEvent); } } else { // If we swiped into wrong direction, act like this was the new // touch down point mDownX = motionEvent.getRawX(); deltaX = 0; } if (mSwiping) { setTranslationX(mDownView, deltaX); setAlpha( mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } break; } } return false; }
/** Modify the second view alpha based on dragged view vertical position. */ void changeSecondViewAlpha() { ViewHelper.setAlpha(secondView, 1 - getVerticalDragOffset()); }
/** Restore view alpha to 1 */ void restoreAlpha() { if (enableHorizontalAlphaEffect && ViewHelper.getAlpha(dragView) < 1) { ViewHelper.setAlpha(dragView, 1); } }
@Override public boolean dispatchTouchEvent(MotionEvent ev) { float currentActivityScaleX = ViewHelper.getScaleX(viewActivity); if (currentActivityScaleX == 1.0f) setScaleDirectionByRawX(ev.getRawX()); switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: lastActionDownX = ev.getX(); lastActionDownY = ev.getY(); isInIgnoredView = isInIgnoredView(ev) && !isOpened(); pressedState = PRESSED_DOWN; break; case MotionEvent.ACTION_MOVE: if (isInIgnoredView || isInDisableDirection(scaleDirection)) break; if (pressedState != PRESSED_DOWN && pressedState != PRESSED_MOVE_HORIZONTAL) break; int xOffset = (int) (ev.getX() - lastActionDownX); int yOffset = (int) (ev.getY() - lastActionDownY); if (pressedState == PRESSED_DOWN) { if (yOffset > 25 || yOffset < -25) { pressedState = PRESSED_MOVE_VERTICAL; break; } if (xOffset < -50 || xOffset > 50) { pressedState = PRESSED_MOVE_HORIZONTAL; ev.setAction(MotionEvent.ACTION_CANCEL); } } else if (pressedState == PRESSED_MOVE_HORIZONTAL) { if (currentActivityScaleX < 0.95) showScrollViewMenu(scrollViewMenu); float targetScale = getTargetScale(ev.getRawX()); if (mUse3D) { int angle = scaleDirection == DIRECTION_LEFT ? -ROTATE_Y_ANGLE : ROTATE_Y_ANGLE; angle *= (1 - targetScale) * 2; ViewHelper.setRotationY(viewActivity, angle); ViewHelper.setScaleX(imageViewShadow, targetScale - shadowAdjustScaleX); ViewHelper.setScaleY(imageViewShadow, targetScale - shadowAdjustScaleY); } else { ViewHelper.setScaleX(imageViewShadow, targetScale + shadowAdjustScaleX); ViewHelper.setScaleY(imageViewShadow, targetScale + shadowAdjustScaleY); } ViewHelper.setScaleX(viewActivity, targetScale); ViewHelper.setScaleY(viewActivity, targetScale); ViewHelper.setAlpha(scrollViewMenu, (1 - targetScale) * 2.0f); lastRawX = ev.getRawX(); return true; } break; case MotionEvent.ACTION_UP: if (isInIgnoredView) break; if (pressedState != PRESSED_MOVE_HORIZONTAL) break; pressedState = PRESSED_DONE; if (isOpened()) { if (currentActivityScaleX > 0.56f) closeMenu(); else openMenu(scaleDirection); } else { if (currentActivityScaleX < 0.94f) { openMenu(scaleDirection); } else { closeMenu(); } } break; } lastRawX = ev.getRawX(); return super.dispatchTouchEvent(ev); }
private void restoreViewPosition(View view) { setAlpha(view, 1f); setTranslationX(view, 0); }
public void setAlpha(float x) { ViewHelper.setAlpha(this, x); }