自定义View:PullToZoomScrollView

傷城~ 2022-07-13 03:42 271阅读 0赞

PullToZoomScrollView

  1. public class PullToZoomScrollView extends NestedScrollView {
  2. private int DEFAULT_PULL_HEIGHT = 100;
  3. private float distance = 0.0f;
  4. private boolean isBig;
  5. private boolean isPreparePull;
  6. private boolean isTouchOne;
  7. private boolean isonce;
  8. private int mCurrentOffset = 0;
  9. private LinearLayout mParentView;
  10. private int mScreenHeight;
  11. private ViewGroup mTopView;
  12. private int mTopViewHeight;
  13. private ObjectAnimator mAnimator;
  14. private OnPullToZoomListener onPullToZoomListener;
  15. private float startY = 0.0f;
  16. private int touchSlop;
  17. public interface OnPullToZoomListener {
  18. void onCancelPull();
  19. void onPreparePull();
  20. void onPull();
  21. void onScrollChanged(int i);
  22. void onStartDrag();
  23. }
  24. public PullToZoomScrollView(Context context, AttributeSet attrs) {
  25. super(context, attrs);
  26. setOverScrollMode(View.OVER_SCROLL_NEVER);
  27. WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
  28. DisplayMetrics metrics = new DisplayMetrics();
  29. wm.getDefaultDisplay().getMetrics(metrics);
  30. this.mScreenHeight = metrics.heightPixels;
  31. this.mTopViewHeight = ResolutionUtils.getHeight(context, metrics.widthPixels, 3, 2);
  32. setVerticalScrollBarEnabled(false);
  33. this.touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
  34. }
  35. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  36. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  37. if (!this.isonce) {
  38. this.mParentView = (LinearLayout) getChildAt(0);
  39. this.mTopView = (ViewGroup) this.mParentView.getChildAt(0);
  40. this.mTopView.getLayoutParams().height = this.mTopViewHeight;
  41. this.isonce = true;
  42. }
  43. }
  44. public boolean onTouchEvent(MotionEvent ev) {
  45. switch (ev.getAction()) {
  46. case MotionEvent.ACTION_UP:
  47. if (this.isBig) {
  48. reset();
  49. this.isBig = false;
  50. }
  51. this.isTouchOne = false;
  52. break;
  53. case MotionEvent.ACTION_MOVE:
  54. if (this.mCurrentOffset <= 0) {
  55. if (!this.isTouchOne) {
  56. this.startY = ev.getY();
  57. this.isTouchOne = true;
  58. }
  59. this.distance = ev.getY() - this.startY;
  60. if (this.distance >= ((float) this.touchSlop) && this.distance > 0.0f) {
  61. this.isBig = true;
  62. setT(((int) (-this.distance)) / 4);
  63. break;
  64. }
  65. }
  66. break;
  67. }
  68. return super.onTouchEvent(ev);
  69. }
  70. public void setT(int t) {
  71. scrollTo(0, 0);
  72. if (t < 0) {
  73. this.mTopView.getLayoutParams().height = this.mTopViewHeight - t;
  74. this.mTopView.requestLayout();
  75. if ((-t) <= this.DEFAULT_PULL_HEIGHT || !this.isBig) {
  76. if (this.onPullToZoomListener != null) {
  77. this.onPullToZoomListener.onStartDrag();
  78. }
  79. } else if (this.onPullToZoomListener != null) {
  80. this.isPreparePull = true;
  81. this.onPullToZoomListener.onPreparePull();
  82. }
  83. }
  84. }
  85. private void reset() {
  86. if (this.mAnimator == null || !this.mAnimator.isRunning()) {
  87. this.mAnimator = ObjectAnimator.ofInt(this, "t", new int[]{((int) (-this.distance)) / 4, 0});
  88. this.mAnimator.setDuration(150);
  89. this.mAnimator.addListener(new AnimatorListenerAdapter() {
  90. public void onAnimationEnd(Animator animation) {
  91. super.onAnimationEnd(animation);
  92. PullToZoomScrollView.this.isPreparePull = false;
  93. if (PullToZoomScrollView.this.distance / aj.hA > ((float) PullToZoomScrollView.this.DEFAULT_PULL_HEIGHT)) {
  94. if (PullToZoomScrollView.this.onPullToZoomListener != null) {
  95. PullToZoomScrollView.this.onPullToZoomListener.onPull();
  96. }
  97. } else if (PullToZoomScrollView.this.onPullToZoomListener != null) {
  98. PullToZoomScrollView.this.onPullToZoomListener.onCancelPull();
  99. }
  100. }
  101. });
  102. this.mAnimator.start();
  103. }
  104. }
  105. protected void onScrollChanged(int l, int t, int oldl, int oldt) {
  106. super.onScrollChanged(l, t, oldl, oldt);
  107. this.mCurrentOffset = t;
  108. if (this.onPullToZoomListener != null) {
  109. this.onPullToZoomListener.onScrollChanged(t);
  110. }
  111. if (t <= this.mTopViewHeight && t >= 0 && !this.isBig) {
  112. this.mTopView.setTranslationY((float) (t / 2));
  113. }
  114. if (this.isBig) {
  115. scrollTo(0, 0);
  116. }
  117. }
  118. public void setOnPullToZoomListener(OnPullToZoomListener onPullToZoomListener) {
  119. this.onPullToZoomListener = onPullToZoomListener;
  120. }
  121. }

发表评论

表情:
评论列表 (有 0 条评论,271人围观)

还没有评论,来说两句吧...

相关阅读

    相关 定义函数

    1.1 使用函数 在Python中,函数必须先声明,然后才能调用它,使用函数时,只要按照函数定义的形式,向函数传递必需的参数,就可以调用函数完成相应的功能或者获得函数返回