带有动画效果的Gallery控件

来源:互联网 发布:海岛奇兵所有升级数据 编辑:程序博客网 时间:2024/05/01 14:01

带有动画效果的Gallery控件


由于项目想做得更加漂亮一些,想做一个滑动的标题栏,每个item对应各自的listview,经过几天终于写出来,先上界面效果


所有的设计思路会在下面几篇里讲述出来,这一篇先讲一下我搜到的一个朋友的说法
一次只想拨动一个item,系统的gallery轻轻一拨。。。嗖的一下跑到最后一项了,或者想做更炫的动画效果的时候就需要我们自定义一个gallery


  1. Android中Gallery 点击图片放大的思路:

  2. 第一、可以通过Matrix对象来变换图像,在选择的时候放大,在失去焦点的时候,缩小到原来的大小。

  3. 推荐阅读:Android gallery滑动惯性问题
  4. double  scale =  1.2;
  5. int width = bm.getWidth();
  6. int height = bm.getHeight();
  7. Log.i("size:", width+"");
  8. float scaleWidth = (float)(scale*width);
  9. float scaleHeight = (float)(scale*height);
  10. Log.i("size:", scaleWidth+"");
  11. Matrix matrix = new Matrix();
  12. matrix.postScale(scaleWidth, scaleHeight);
  13. bm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);

  14.   第二 、通过动画

  15. <?xml version="1.0" encoding="utf-8"?>

  16. <scale
  17. xmlns:android=
  18. android:interpolator="@android:anim/decelerate_interpolator"

  19. android:fromXScale="1"
  20. android:toXScale="1.1"
  21. android:fromYScale="1"
  22. android:toYScale="1.1"

  23. android:pivotX="50%"
  24. android:pivotY="50%"
  25. android:duration="500">

  26. </scale>

  27.   第三、通过setLayoutParams

  28. view.setLayoutParams(new Gallery.LayoutParams(150,150));
  29. int mCounts = g.getCount() - 1;
  30. if(position>0 && (position < mCounts))
  31. {
  32.   g.getChildAt(position - 1).setLayoutParams(new Gallery.LayoutParams(136, 88));
  33.   g.getChildAt(position + 1).setLayoutParams(new Gallery.LayoutParams(136, 88));
  34. }
  35. if(position == 0)
  36. {
  37.   g.getChildAt(position + 1).setLayoutParams(new Gallery.LayoutParams(136, 88));
  38. }
  39. if(position == mCounts)
  40. {
  41.   g.getChildAt(position - 1).setLayoutParams(new Gallery.LayoutParams(136, 88));
  42. }

  43. 注释:其中(136, 88)是gallery中图片的大小,是在ImageAdapter里面设置的。(150,150)是选中图片放大后的大小,可以随便设置,只要跟(136, 88)区别就行了,是为了观察变化,我设置的是150而已。

  44.   第四 、通过动画和LayoutParam结合

  45. gallery.setOnItemSelectedListener(new OnItemSelectedListener(){
  46. @Override
  47. public
  48. void onItemSelected(AdapterView<?> arg0, View arg1,
  49. int arg2, long arg3) { 
  50. ImageView v = (ImageView)arg1;
  51. if(tempView != null && v.hashCode() != tempView.hashCode()){
  52. tempView.setLayoutParams(new Gallery.LayoutParams(50,50));
  53. }
  54. v.startAnimation(toLarge);
  55. tempView = v;
  56. v.setLayoutParams(new Gallery.LayoutParams(60,60));
  57. //
  58. //v.setLayoutParams(new Gallery.LayoutParams(130,130));
  59. tvName.setText(tempList.get(arg2).getPicName());
  60. }
  61. @Override,
  62. public
  63. void onNothingSelected(AdapterView<?> arg0) {
  64. tvName.setText("Nothing selected .");
  65. }
  66. });

原创粉丝点击