0
点赞
收藏
分享

微信扫一扫

android Gallery3D效果


​​http://1002878825-qq-com.iteye.com/blog/1194788​​



Java代码 



  1. 主页面:Gallery3DActivity  
  2.   
  3.   
  4. package
  5.   
  6. import
  7. import
  8. import
  9. import
  10. import
  11. import
  12.   
  13. public class Gallery3DActivity extends
  14.   
  15. @Override
  16. protected void
  17. // TODO Auto-generated method stub
  18. super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);     
  20.           
  21.         Integer[] images = { R.drawable.aa, R.drawable.bb,     
  22.                 R.drawable.cc, R.drawable.dd, R.drawable.ee,     
  23. /* R.drawable.gg,R.drawable.hh ,
  24.                 R.drawable.jj, R.drawable.kk, R.drawable.ll*/};     
  25.              
  26. new ImageAdapter(this, images);     
  27. //创建倒影效果   
  28. this.findViewById(R.id.Gallery01);     
  29. 0);     
  30. 50); //图片之间的间距   
  31.         galleryFlow.setAdapter(adapter);     
  32.              
  33. new
  34. public void
  35. int position, long
  36.                 Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();     
  37.             }     
  38.                  
  39.         });     
  40. 0);     
  41.     }     
  42. }  



 



Java代码 



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

 


Java代码 



  1. 最后是把图片设置到Gallery中  
  2. ImageAdapter类:  
  3.   
  4. package
  5.   
  6. import
  7. import
  8. import
  9. import
  10. import
  11. import
  12. import
  13. import
  14. import
  15. import
  16. import
  17. import
  18. import
  19. import
  20. import
  21. import
  22.   
  23. public class ImageAdapter extends
  24.   
  25. int
  26. private
  27. private
  28. private
  29.          
  30. public
  31.      mContext = c;     
  32.      mImageIds = ImageIds;     
  33. new
  34.     }     
  35. /**  
  36.      * 创建倒影效果  
  37.      * @return  
  38.      */
  39. public boolean
  40. //倒影图和原图之间的距离   
  41. final int reflectionGap = 4;     
  42. int index = 0;     
  43. for (int
  44. //返回原图解码之后的bitmap对象   
  45.       Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);     
  46. int
  47. int
  48. //创建矩阵对象   
  49. new
  50.            
  51. //指定一个角度以0,0为坐标进行旋转   
  52. // matrix.setRotate(30);   
  53.            
  54. //指定矩阵(x轴不变,y轴相反)   
  55. 1, -1);     
  56.            
  57. //将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图   
  58. 0,     
  59. 2, width, height/2, matrix, false);     
  60.            
  61. //创建一个宽度不变,高度为原图+倒影图高度的位图   
  62.       Bitmap bitmapWithReflection = Bitmap.createBitmap(width,     
  63. 2), Config.ARGB_8888);     
  64.            
  65. //将上面创建的位图初始化到画布   
  66. new
  67. 0, 0, null);     
  68.            
  69. new
  70. false);     
  71. //    canvas.drawRect(0, height, width, height + reflectionGap,deafaultPaint);   
  72. 0, height + reflectionGap, null);     
  73. new
  74. false);     
  75.             
  76. /**  
  77.        * 参数一:为渐变起初点坐标x位置,  
  78.        * 参数二:为y轴位置,  
  79.        * 参数三和四:分辨对应渐变终点,  
  80.        * 最后参数为平铺方式,  
  81.        * 这里设置为镜像Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变  
  82.        */
  83. new LinearGradient(0,originalImage.getHeight(), 0,     
  84. 0x70ffffff, 0x00ffffff, TileMode.MIRROR);     
  85. //设置阴影   
  86.       paint.setShader(shader);     
  87. new
  88. //用已经定义好的画笔构建一个矩形阴影渐变效果   
  89. 0, height, width, bitmapWithReflection.getHeight()+ reflectionGap, paint);     
  90.            
  91. //创建一个ImageView用来显示已经画好的bitmapWithReflection   
  92. new
  93.       imageView.setImageBitmap(bitmapWithReflection);     
  94. //设置imageView大小 ,也就是最终显示的图片大小   
  95. new GalleryFlow.LayoutParams(220, 400));     
  96. //      imageView.setScaleType(ScaleType.MATRIX);   
  97.       mImages[index++] = imageView;     
  98.      }     
  99. return true;     
  100.     }     
  101. @SuppressWarnings("unused")     
  102. private
  103. return null;     
  104.     }     
  105. public int
  106. return
  107.     }     
  108. public Object getItem(int
  109. return
  110.     }     
  111. public long getItemId(int
  112. return
  113.     }     
  114. public View getView(int
  115. return
  116.     }     
  117. public float getScale(boolean focused, int
  118. return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));     
  119.     }     
  120. }  

 


Java代码 



  1. main.xml  
  2.   
  3. <?xml version="1.0" encoding="utf-8"?>  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5. "vertical"
  6. "fill_parent"
  7. "fill_parent"
  8.   
  9.     <com.xiawenquan.GalleryFlow  
  10. "@+id/Gallery01"
  11. "20dip"
  12. "fill_parent"
  13. "wrap_content"
  14.     />  
  15.   
  16.   
  17.   
  18. </LinearLayout>  


  • ​​TextBitmap.rar​​ (2.2 MB)
  • 下载次数: 266



举报

相关推荐

0 条评论