Android控件Gallery 3D效果


本人最近在结合网上资料整理出了Android Gallery 3D效果,因为我们后面项目中需要这个效果,所以提前研究下,在这里和大家分享下,希望互相交流,共同进步。

Android控件Gallery 3D效果如下:

Android控件Gallery 3D效果

本文源码下载在:

在帮客之家的1号FTP服务器里,下载地址:

FTP地址:ftp://www.bkjia.com

用户名:www.bkjia.com

密码:www.muu.cc

在 2011年LinuxIDC.com\8月\Android控件Gallery 3D效果源码

下载方法见这里 http://www.bkjia.net/thread-1187-1-1.html

1.扩展Gallery:

  1. public class GalleryFlow extends Gallery {   
  2.     private Camera mCamera = new Camera();//相机类   
  3.     private int mMaxRotationAngle = 60;//最大转动角度   
  4.     private int mMaxZoom = -300;////最大缩放值   
  5.     private int mCoveflowCenter;//半径值   
  6.     public GalleryFlow(Context context) {   
  7.         super(context);   
  8.         //支持转换 ,执行getChildStaticTransformation方法   
  9.         this.setStaticTransformationsEnabled(true);   
  10.     }   
  11.     public GalleryFlow(Context context, AttributeSet attrs) {   
  12.         super(context, attrs);   
  13.         this.setStaticTransformationsEnabled(true);   
  14.     }   
  15.     public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {   
  16.         super(context, attrs, defStyle);   
  17.         this.setStaticTransformationsEnabled(true);   
  18.     }   
  19.     public int getMaxRotationAngle() {   
  20.         return mMaxRotationAngle;   
  21.     }   
  22.     public void setMaxRotationAngle(int maxRotationAngle) {   
  23.         mMaxRotationAngle = maxRotationAngle;   
  24.     }   
  25.     public int getMaxZoom() {   
  26.         return mMaxZoom;   
  27.     }   
  28.     public void setMaxZoom(int maxZoom) {   
  29.         mMaxZoom = maxZoom;   
  30.     }   
  31.     private int getCenterOfCoverflow() {   
  32.         return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2  
  33.                         + getPaddingLeft();   
  34.     }   
  35.     private static int getCenterOfView(View view) {   
  36.         System.out.println("view left :"+view.getLeft());   
  37.         System.out.println("view width :"+view.getWidth());   
  38.         return view.getLeft() + view.getWidth() / 2;   
  39.     }   
  40.        
  41.        
  42.    //控制gallery中每个图片的旋转(重写的gallery中方法)   
  43.     protected boolean getChildStaticTransformation(View child, Transformation t) {     
  44.         //取得当前子view的半径值   
  45.         final int childCenter = getCenterOfView(child);   
  46.         System.out.println("childCenter:"+childCenter);   
  47.         final int childWidth = child.getWidth();   
  48.         //旋转角度   
  49.         int rotationAngle = 0;   
  50.         //重置转换状态   
  51.         t.clear();   
  52.         //设置转换类型   
  53.         t.setTransformationType(Transformation.TYPE_MATRIX);   
  54.         //如果图片位于中心位置不需要进行旋转   
  55.         if (childCenter == mCoveflowCenter) {   
  56.             transformImageBitmap((ImageView) child, t, 0);   
  57.         } else {   
  58.             //根据图片在gallery中的位置来计算图片的旋转角度   
  59.             rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);   
  60.             System.out.println("rotationAngle:" +rotationAngle);   
  61.             //如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)   
  62.             if (Math.abs(rotationAngle) > mMaxRotationAngle) {   
  63.                 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;   
  64.             }   
  65.             transformImageBitmap((ImageView) child, t, rotationAngle);   
  66.         }   
  67.         return true;   
  68.     }   
  69.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {   
  70.         mCoveflowCenter = getCenterOfCoverflow();   
  71.         super.onSizeChanged(w, h, oldw, oldh);   
  72.     }   
  73.     private void transformImageBitmap(ImageView child, Transformation t,   
  74.                     int rotationAngle) {   
  75.         //对效果进行保存   
  76.         mCamera.save();   
  77.         final Matrix imageMatrix = t.getMatrix();   
  78.         //图片高度   
  79.         final int imageHeight = child.getLayoutParams().height;   
  80.         //图片宽度   
  81.         final int imageWidth = child.getLayoutParams().width;   
  82.            
  83.         //返回旋转角度的绝对值   
  84.         final int rotation = Math.abs(rotationAngle);   
  85.            
  86.         // 在Z轴上正向移动camera的视角,实际效果为放大图片。   
  87.         // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。   
  88.         mCamera.translate(0.0f, 0.0f, 100.0f);   
  89.         // As the angle of the view gets less, zoom in   
  90.         if (rotation < mMaxRotationAngle) {   
  91.             float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));   
  92.             mCamera.translate(0.0f, 0.0f, zoomAmount);   
  93.         }   
  94.         // 在Y轴上旋转,对应图片竖向向里翻转。   
  95.         // 如果在X轴上旋转,则对应图片横向向里翻转。   
  96.         mCamera.rotateY(rotationAngle);   
  97.         mCamera.getMatrix(imageMatrix);   
  98.         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));   
  99.         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));   
  100.         mCamera.restore();   
  101.     }   
  102. }  
  • 1
  • 2
  • 3
  • 下一页

相关内容