Android PullToZoomListView实现放大回弹效果

时间:2021-08-16 21:39:56

另外一个相同项目的地址https://github.com/Frank-Zhu/PullZoomView

转自http://blog.csdn.net/wangjinyu501/article/details/38367669

之前看过一篇文章,链接是:可以下拉缩放HeaderView的ListView:PullToZoomInListView。说的就是PullToZoomListView,不过这篇文章有个地方需要勘误,就是PullToZoomListView这个控件虽然是github上一个开源项目。不过最美应用并不是使用这个开源项目,而是这个开源项目反编译了最美应用的代码。
  但是,它这个代码是有问题的,当手指在屏幕上滑动的时候会引发onItemClick事件。看了一下它的代码,发现是因为在onTouchEvent()方法中的MotionEvent.ACTION_MOVE中return true了,这样就会消费这个事件,所以导致了onItemClick事件。之后我也反编译了一下最美应用,看了一下这个控件的实现,发现还有其他问题,比如少了一些变量,没有重写onInterceptTouchEvent()方法等,因此我又自己动手把这个控件丰富了一下,使其能够最大限度的接近原始代码,而且可正常使用。修改后代码如下:
  1. package com.kince.android.pulltozoomlistview;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.SystemClock;
  5. import android.util.AttributeSet;
  6. import android.util.DisplayMetrics;
  7. import android.util.Log;
  8. import android.view.MotionEvent;
  9. import android.view.ViewGroup;
  10. import android.view.animation.Interpolator;
  11. import android.widget.AbsListView;
  12. import android.widget.FrameLayout;
  13. import android.widget.ImageView;
  14. import android.widget.ListView;
  15. public class PullToZoomListView extends ListView implements
  16. AbsListView.OnScrollListener {
  17. private static final int INVALID_VALUE = -1;
  18. private static final String TAG = "PullToZoomListView";
  19. private static final Interpolator sInterpolator = new Interpolator() {
  20. public float getInterpolation(float paramAnonymousFloat) {
  21. float f = paramAnonymousFloat - 1.0F;
  22. return 1.0F + f * (f * (f * (f * f)));
  23. }
  24. };
  25. int mActivePointerId = -1;
  26. private FrameLayout mHeaderContainer;
  27. private int mHeaderHeight;
  28. private ImageView mHeaderImage;
  29. float mLastMotionY = -1.0F;
  30. float mLastScale = -1.0F;
  31. float mMaxScale = -1.0F;
  32. private AbsListView.OnScrollListener mOnScrollListener;
  33. private ScalingRunnalable mScalingRunnalable;
  34. private int mScreenHeight;
  35. private ImageView mShadow;
  36. private boolean mScrollable = true;
  37. private boolean mShowHeaderImage = true;
  38. private boolean mZoomable = true;
  39. public PullToZoomListView(Context paramContext) {
  40. super(paramContext);
  41. init(paramContext);
  42. }
  43. public PullToZoomListView(Context paramContext,
  44. AttributeSet paramAttributeSet) {
  45. super(paramContext, paramAttributeSet);
  46. init(paramContext);
  47. }
  48. public PullToZoomListView(Context paramContext,
  49. AttributeSet paramAttributeSet, int paramInt) {
  50. super(paramContext, paramAttributeSet, paramInt);
  51. init(paramContext);
  52. }
  53. private void endScraling() {
  54. if (this.mHeaderContainer.getBottom() >= this.mHeaderHeight)
  55. Log.d("mmm", "endScraling");
  56. this.mScalingRunnalable.startAnimation(200L);
  57. }
  58. private void init(Context paramContext) {
  59. DisplayMetrics localDisplayMetrics = new DisplayMetrics();
  60. ((Activity) paramContext).getWindowManager().getDefaultDisplay()
  61. .getMetrics(localDisplayMetrics);
  62. this.mScreenHeight = localDisplayMetrics.heightPixels;
  63. this.mHeaderContainer = new FrameLayout(paramContext);
  64. this.mHeaderImage = new ImageView(paramContext);
  65. int i = localDisplayMetrics.widthPixels;
  66. setHeaderViewSize(i, (int) (9.0F * (i / 16.0F)));
  67. this.mShadow = new ImageView(paramContext);
  68. FrameLayout.LayoutParams localLayoutParams = new FrameLayout.LayoutParams(
  69. -1, -2);
  70. localLayoutParams.gravity = 80;
  71. this.mShadow.setLayoutParams(localLayoutParams);
  72. this.mHeaderContainer.addView(this.mHeaderImage);
  73. this.mHeaderContainer.addView(this.mShadow);
  74. addHeaderView(this.mHeaderContainer);
  75. this.mScalingRunnalable = new ScalingRunnalable();
  76. super.setOnScrollListener(this);
  77. }
  78. private void onSecondaryPointerUp(MotionEvent paramMotionEvent) {
  79. int i = (paramMotionEvent.getAction()) >> 8;
  80. Log.d("onSecondaryPointerUp", i + "");
  81. if (paramMotionEvent.getPointerId(i) == this.mActivePointerId)
  82. if (i != 0) {
  83. this.mLastMotionY = paramMotionEvent.getY(1);
  84. this.mActivePointerId = paramMotionEvent.getPointerId(0);
  85. return;
  86. }
  87. }
  88. private void reset() {
  89. this.mActivePointerId = -1;
  90. this.mLastMotionY = -1.0F;
  91. this.mMaxScale = -1.0F;
  92. this.mLastScale = -1.0F;
  93. }
  94. public ImageView getHeaderView() {
  95. return this.mHeaderImage;
  96. }
  97. public void hideHeaderImage() {
  98. this.mShowHeaderImage = false;
  99. this.mZoomable = false;
  100. this.mScrollable = false;
  101. removeHeaderView(this.mHeaderContainer);
  102. }
  103. public boolean isScrollable() {
  104. return this.mScrollable;
  105. }
  106. public boolean isZoomable() {
  107. return this.mZoomable;
  108. }
  109. public boolean onInterceptTouchEvent(MotionEvent paramMotionEvent) {
  110. if (!this.mZoomable) {
  111. return super.onInterceptTouchEvent(paramMotionEvent);
  112. }
  113. switch (paramMotionEvent.getAction() & MotionEvent.ACTION_MASK) {
  114. case MotionEvent.ACTION_DOWN:
  115. this.mActivePointerId = paramMotionEvent.getPointerId(0);
  116. this.mMaxScale = (this.mScreenHeight / this.mHeaderHeight);
  117. break;
  118. case MotionEvent.ACTION_UP:
  119. reset();
  120. break;
  121. case MotionEvent.ACTION_POINTER_DOWN:
  122. this.mActivePointerId = paramMotionEvent
  123. .getPointerId(paramMotionEvent.getActionIndex());
  124. break;
  125. case MotionEvent.ACTION_POINTER_UP:
  126. onSecondaryPointerUp(paramMotionEvent);
  127. break;
  128. }
  129. return super.onInterceptTouchEvent(paramMotionEvent);
  130. }
  131. protected void onLayout(boolean paramBoolean, int paramInt1, int paramInt2,
  132. int paramInt3, int paramInt4) {
  133. super.onLayout(paramBoolean, paramInt1, paramInt2, paramInt3, paramInt4);
  134. if (this.mHeaderHeight == 0)
  135. this.mHeaderHeight = this.mHeaderContainer.getHeight();
  136. }
  137. @Override
  138. public void onScroll(AbsListView paramAbsListView, int paramInt1,
  139. int paramInt2, int paramInt3) {
  140. if (this.mScrollable) {
  141. Log.d(TAG, "onScroll");
  142. float f = this.mHeaderHeight - this.mHeaderContainer.getBottom();
  143. Log.d("onScroll", "f|" + f);
  144. if ((f > 0.0F) && (f < this.mHeaderHeight)) {
  145. Log.d("onScroll", "1");
  146. int i = (int) (0.65D * f);
  147. this.mHeaderImage.scrollTo(0, -i);
  148. } else if (this.mHeaderImage.getScrollY() != 0) {
  149. Log.d("onScroll", "2");
  150. this.mHeaderImage.scrollTo(0, 0);
  151. }
  152. }
  153. if (this.mOnScrollListener != null) {
  154. this.mOnScrollListener.onScroll(paramAbsListView, paramInt1,
  155. paramInt2, paramInt3);
  156. }
  157. }
  158. public void onScrollStateChanged(AbsListView paramAbsListView, int paramInt) {
  159. if (this.mOnScrollListener != null)
  160. this.mOnScrollListener.onScrollStateChanged(paramAbsListView,
  161. paramInt);
  162. }
  163. public boolean onTouchEvent(MotionEvent ev) {
  164. Log.d(TAG, "" + (0xFF & ev.getAction()));
  165. if (!this.mZoomable) {
  166. Log.i("zoom", "zoom");
  167. return super.onTouchEvent(ev);
  168. }
  169. switch (ev.getAction() & MotionEvent.ACTION_MASK) {
  170. case MotionEvent.ACTION_OUTSIDE:
  171. case MotionEvent.ACTION_DOWN:
  172. if (!this.mScalingRunnalable.mIsFinished) {
  173. this.mScalingRunnalable.abortAnimation();
  174. }
  175. this.mLastMotionY = ev.getY();
  176. this.mActivePointerId = ev.getPointerId(0);
  177. this.mMaxScale = (this.mScreenHeight / this.mHeaderHeight);
  178. this.mLastScale = (this.mHeaderContainer.getBottom() / this.mHeaderHeight);
  179. break;
  180. case MotionEvent.ACTION_MOVE:
  181. Log.d("onTouchEvent", "mActivePointerId" + mActivePointerId);
  182. int j = ev.findPointerIndex(this.mActivePointerId);
  183. if (j == -1) {
  184. Log.e("PullToZoomListView", "Invalid pointerId="
  185. + this.mActivePointerId + " in onTouchEvent");
  186. } else {
  187. if (this.mLastMotionY == -1.0F)
  188. this.mLastMotionY = ev.getY(j);
  189. if (this.mHeaderContainer.getBottom() >= this.mHeaderHeight) {
  190. ViewGroup.LayoutParams localLayoutParams = this.mHeaderContainer
  191. .getLayoutParams();
  192. float f = ((ev.getY(j) - this.mLastMotionY + this.mHeaderContainer
  193. .getBottom()) / this.mHeaderHeight - this.mLastScale)
  194. / 2.0F + this.mLastScale;
  195. if ((this.mLastScale <= 1.0D) && (f < this.mLastScale)) {
  196. localLayoutParams.height = this.mHeaderHeight;
  197. this.mHeaderContainer
  198. .setLayoutParams(localLayoutParams);
  199. }
  200. this.mLastScale = Math.min(Math.max(f, 1.0F),
  201. this.mMaxScale);
  202. localLayoutParams.height = ((int) (this.mHeaderHeight * this.mLastScale));
  203. if (localLayoutParams.height < this.mScreenHeight)
  204. this.mHeaderContainer
  205. .setLayoutParams(localLayoutParams);
  206. this.mLastMotionY = ev.getY(j);
  207. }
  208. this.mLastMotionY = ev.getY(j);
  209. }
  210. break;
  211. case MotionEvent.ACTION_UP:
  212. reset();
  213. endScraling();
  214. break;
  215. case MotionEvent.ACTION_CANCEL:
  216. break;
  217. case MotionEvent.ACTION_POINTER_DOWN:
  218. int i = ev.getActionIndex();
  219. this.mLastMotionY = ev.getY(i);
  220. this.mActivePointerId = ev.getPointerId(i);
  221. break;
  222. case MotionEvent.ACTION_POINTER_UP:
  223. onSecondaryPointerUp(ev);
  224. this.mLastMotionY = ev.getY(ev
  225. .findPointerIndex(this.mActivePointerId));
  226. break;
  227. }
  228. return super.onTouchEvent(ev);
  229. }
  230. public void setHeaderViewSize(int paramInt1, int paramInt2) {
  231. if (!this.mShowHeaderImage) {
  232. return;
  233. }
  234. Object localObject = this.mHeaderContainer.getLayoutParams();
  235. if (localObject == null)
  236. localObject = new AbsListView.LayoutParams(paramInt1, paramInt2);
  237. ((ViewGroup.LayoutParams) localObject).width = paramInt1;
  238. ((ViewGroup.LayoutParams) localObject).height = paramInt2;
  239. this.mHeaderContainer
  240. .setLayoutParams((ViewGroup.LayoutParams) localObject);
  241. this.mHeaderHeight = paramInt2;
  242. }
  243. public void setOnScrollListener(
  244. AbsListView.OnScrollListener paramOnScrollListener) {
  245. this.mOnScrollListener = paramOnScrollListener;
  246. }
  247. public void setScrollable(boolean paramBoolean) {
  248. if (!this.mShowHeaderImage) {
  249. return;
  250. }
  251. this.mScrollable = paramBoolean;
  252. }
  253. public void setShadow(int paramInt) {
  254. if (!this.mShowHeaderImage) {
  255. return;
  256. }
  257. this.mShadow.setBackgroundResource(paramInt);
  258. }
  259. public void setZoomable(boolean paramBoolean) {
  260. if (!this.mShowHeaderImage) {
  261. return;
  262. }
  263. this.mZoomable = paramBoolean;
  264. }
  265. class ScalingRunnalable implements Runnable {
  266. long mDuration;
  267. boolean mIsFinished = true;
  268. float mScale;
  269. long mStartTime;
  270. ScalingRunnalable() {
  271. }
  272. public void abortAnimation() {
  273. this.mIsFinished = true;
  274. }
  275. public boolean isFinished() {
  276. return this.mIsFinished;
  277. }
  278. public void run() {
  279. float f2;
  280. ViewGroup.LayoutParams localLayoutParams;
  281. if ((!this.mIsFinished) && (this.mScale > 1.0D)) {
  282. float f1 = ((float) SystemClock.currentThreadTimeMillis() - (float) this.mStartTime)
  283. / (float) this.mDuration;
  284. f2 = this.mScale - (this.mScale - 1.0F)
  285. * PullToZoomListView.sInterpolator.getInterpolation(f1);
  286. localLayoutParams = PullToZoomListView.this.mHeaderContainer
  287. .getLayoutParams();
  288. if (f2 > 1.0F) {
  289. Log.d("mmm", "f2>1.0");
  290. localLayoutParams.height = PullToZoomListView.this.mHeaderHeight;
  291. localLayoutParams.height = ((int) (f2 * PullToZoomListView.this.mHeaderHeight));
  292. PullToZoomListView.this.mHeaderContainer
  293. .setLayoutParams(localLayoutParams);
  294. PullToZoomListView.this.post(this);
  295. return;
  296. }
  297. this.mIsFinished = true;
  298. }
  299. }
  300. public void startAnimation(long paramLong) {
  301. this.mStartTime = SystemClock.currentThreadTimeMillis();
  302. this.mDuration = paramLong;
  303. this.mScale = ((float) (PullToZoomListView.this.mHeaderContainer
  304. .getBottom()) / PullToZoomListView.this.mHeaderHeight);
  305. this.mIsFinished = false;
  306. PullToZoomListView.this.post(this);
  307. }
  308. }
  309. }

关于这个控件的实现原理可以自行参考上面链接的文章,里面已经有较为细致的讲解,此处不再赘述。实现的效果就是下面这样:

Android PullToZoomListView实现放大回弹效果
  github链接:https://github.com/wangjinyu501/PullToZoomListView/,如果您发现了任何问题,可以在文章下面留言,我会第一时间处理,欢迎交流。