你好,游客 登录 注册 搜索
背景:
阅读新闻

Android自定义带倒影和偏转的超炫Gallery

[日期:2012-06-18] 来源:Linux社区  作者:aomandeshangxiao [字体:大 中 小]

只有一个自定义的GalleryFlow,来看下它的代码:

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

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/10/9 10:23:10
能不能把你的xml布局代码也发出来啊!