免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3205 | 回复: 1
打印 上一主题 下一主题

自定义Gallery 滑动中图片自动突出显示 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-03-19 20:56 |只看该作者 |倒序浏览

自定义Gallery 滑动中图片自动突出显示











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

论坛徽章:
0
2 [报告]
发表于 2012-03-19 20:57 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP