- 论坛徽章:
- 0
|
自定义Gallery 滑动中图片自动突出显示
Java代码- 1.package org.pskink;
- 2.
- 3.import android.content.Context;
- 4.import android.content.res.TypedArray;
- 5.import android.graphics.Matrix;
- 6.import android.os.Handler;
- 7.import android.os.Message;
- 8.import android.util.AttributeSet;
- 9.import android.util.Log;
- 10.import android.view.View;
- 11.import android.view.animation.Animation;
- 12.import android.view.animation.Transformation;
- 13.import android.widget.AdapterView;
- 14.import android.widget.Gallery;
- 15.import android.widget.ImageView;
- 16.import android.widget.AdapterView.OnItemSelectedListener;
- 17.
- 18.public class AnimatedSizingGallery extends Gallery implements OnItemSelectedListener {
- 19. public static final String TAG = "AnimatedSizingGallery";
- 20. private static final int MSG_ZOOM_IN = 1;
- 21. private static final long DELAY = 100;
- 22.
- 23. private View mPrev;
- 24. private float mAnimationScale;
- 25. private float mAnimationOffsetY;
- 26. private int mAnimationDuration;
- 27.
- 28. public AnimatedSizingGallery(Context context, AttributeSet attrs) {
- 29. super(context, attrs);
- 30. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AnimatedSizingGallery);
- 31. mAnimationScale = a.getFloat(R.styleable.AnimatedSizingGallery_animationScale, 2);
- 32. mAnimationOffsetY = a.getFloat(R.styleable.AnimatedSizingGallery_animationOffsetY, 0);
- 33. mAnimationDuration = a.getInteger(R.styleable.AnimatedSizingGallery_animationDuration, 500);
- 34. a.recycle();
- 35. setOnItemSelectedListener(this);
- 36. }
- 37.
- 38. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
- 39. if (mPrev != null) {
- 40. zoomOut();
- 41. }
- 42. mPrev = view;
- 43. mGalleryHandler.removeCallbacksAndMessages(view);
- 44. Message msg = Message.obtain(mGalleryHandler, MSG_ZOOM_IN, view);
- 45. mGalleryHandler.sendMessageDelayed(msg, DELAY);
- 46. }
- 47.
- 48. public void onNothingSelected(AdapterView<?> parent) {
- 49. Log.d(TAG, "onNothingSelected called !!!");
- 50. if (mPrev != null) {
- 51. zoomOut();
- 52. mPrev = null;
- 53. }
- 54. }
- 55.
- 56. private void zoomOut() {
- 57. if (mGalleryHandler.hasMessages(MSG_ZOOM_IN, mPrev)) {
- 58. mGalleryHandler.removeCallbacksAndMessages(mPrev);
- 59. } else {
- 60. ZoomAnimation a = (ZoomAnimation) mPrev.getAnimation();
- 61. a.resetForZoomOut();
- 62. mPrev.startAnimation(a);
- 63. }
- 64. }
- 65.
- 66. Handler mGalleryHandler = new Handler() {
- 67. @Override
- 68. public void dispatchMessage(Message msg) {
- 69. if (msg.what == MSG_ZOOM_IN) {
- 70. ImageView view = (ImageView) msg.obj;
- 71. Animation a = new ZoomAnimation(view, 1, mAnimationScale, mAnimationOffsetY, mAnimationDuration);
- 72. view.startAnimation(a);
- 73. }
- 74. }
- 75. };
- 76.
- 77. class ZoomAnimation extends Animation {
- 78. private float mFrom;
- 79. private float mTo;
- 80. private float mOffsetY;
- 81. private int mPivotX;
- 82. private int mPivotY;
- 83. private float mInterpolatedTime;
- 84.
- 85. public ZoomAnimation(View v, float from, float to, float offsetY, int duration) {
- 86. super();
- 87. mFrom = from;
- 88. mTo = to;
- 89. mOffsetY = offsetY * v.getHeight();
- 90. setDuration(duration);
- 91. setFillAfter(true);
- 92. mPivotX = v.getWidth() / 2;
- 93. mPivotY = v.getHeight() / 2;
- 94. }
- 95.
- 96. public void resetForZoomOut() {
- 97. reset();
- 98. mOffsetY = 0;
- 99. mFrom = mFrom + (mTo - mFrom) * mInterpolatedTime;
- 100. mTo = 1;
- 101. }
- 102.
- 103. @Override
- 104. protected void applyTransformation(float interpolatedTime, Transformation t) {
- 105. mInterpolatedTime = interpolatedTime;
- 106. float s = mFrom + (mTo - mFrom) * interpolatedTime;
- 107. Matrix matrix = t.getMatrix();
- 108. if (mOffsetY != 0) {
- 109. matrix.preTranslate(0, mOffsetY * interpolatedTime);
- 110. }
- 111. if (mTo == 1) {
- 112. matrix.preRotate(360 * interpolatedTime, mPivotX, mPivotY);
- 113. }
- 114. matrix.preScale(s, s, mPivotX, mPivotY);
- 115. }
- 116. }
- 117.}
复制代码 |
|