1. package xiaosi.GalleryFlow;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. public class GalleryFlowActivity extends Activity {
  5. public void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.main);
  8. Integer[] images = { R.drawable.a, R.drawable.b,
  9. R.drawable.c, R.drawable.d, R.drawable.e,
  10. };
  11. ImageAdapter adapter = new ImageAdapter(this, images);
  12. adapter.createReflectedImages();
  13. GalleryFlow galleryFlow = (GalleryFlow) findViewById(R.id.Gallery01);
  14. galleryFlow.setAdapter(adapter);
  15. }
  16. }

ImageAdapter.Java

  1. package xiaosi.GalleryFlow;
  2. import android.content.Context;
  3. import android.content.res.Resources;
  4. import android.graphics.Bitmap;
  5. import android.graphics.BitmapFactory;
  6. import android.graphics.Canvas;
  7. import android.graphics.LinearGradient;
  8. import android.graphics.Matrix;
  9. import android.graphics.Paint;
  10. import android.graphics.PorterDuffXfermode;
  11. import android.graphics.Bitmap.Config;
  12. import android.graphics.PorterDuff.Mode;
  13. import android.graphics.Shader.TileMode;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.BaseAdapter;
  17. import android.widget.ImageView;
  18. import android.widget.ImageView.ScaleType;
  19. public class ImageAdapter extends BaseAdapter
  20. {
  21. int mGalleryItemBackground;
  22. private Context    mContext;
  23. private Integer[]  mImageIds;
  24. private ImageView[] mImages;
  25. public ImageAdapter(Context c, Integer[] ImageIds)
  26. {
  27. mContext  = c;
  28. mImageIds = ImageIds;
  29. mImages   = new ImageView[mImageIds.length];
  30. }
  31. public boolean createReflectedImages()
  32. {
  33. final int reflectionGap = 4;
  34. int index = 0;
  35. for (int imageId : mImageIds)
  36. {
  37. Bitmap originalImage = BitmapFactory.decodeResource(mContext.getResources(), imageId);
  38. int width  = originalImage.getWidth();
  39. int height = originalImage.getHeight();
  40. Matrix matrix = new Matrix();
  41. matrix.preScale(1, -1);
  42. Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false);
  43. Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);
  44. Canvas canvas = new Canvas(bitmapWithReflection);
  45. canvas.drawBitmap(originalImage, 0, 0, null);
  46. Paint deafaultPaint = new Paint();
  47. canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
  48. canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
  49. Paint paint = new Paint();
  50. LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
  51. + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
  52. paint.setShader(shader);
  53. paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
  54. canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
  55. ImageView imageView = new ImageView(mContext);
  56. imageView.setImageBitmap(bitmapWithReflection);
  57. imageView.setLayoutParams(new GalleryFlow.LayoutParams(250, 340));
  58. imageView.setScaleType(ScaleType.FIT_XY);
  59. mImages[index++] = imageView;
  60. }
  61. return true;
  62. }
  63. private Resources getResources()
  64. {
  65. // TODO Auto-generated method stub
  66. return null;
  67. }
  68. public int getCount()
  69. {
  70. return mImageIds.length;
  71. }
  72. public Object getItem(int position)
  73. {
  74. return position;
  75. }
  76. public long getItemId(int position)
  77. {
  78. return position;
  79. }
  80. public View getView(int position, View convertView, ViewGroup parent)
  81. {
  82. return mImages[position];
  83. }
  84. public float getScale(boolean focused, int offset)
  85. {
  86. return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
  87. }
  88. }

GalleryFlow.java

  1. package xiaosi.GalleryFlow;
  2. import android.content.Context;
  3. import android.graphics.Camera;
  4. import android.graphics.Matrix;
  5. import android.util.AttributeSet;
  6. import android.view.View;
  7. import android.view.animation.Transformation;
  8. import android.widget.Gallery;
  9. import android.widget.ImageView;
  10. public class GalleryFlow extends Gallery {
  11. private Camera mCamera = new Camera();
  12. private int mMaxRotationAngle = 60;
  13. private int mMaxZoom = -120;
  14. private int mCoveflowCenter;
  15. public GalleryFlow(Context context) {
  16. super(context);
  17. this.setStaticTransformationsEnabled(true);
  18. }
  19. public GalleryFlow(Context context, AttributeSet attrs) {
  20. super(context, attrs);
  21. this.setStaticTransformationsEnabled(true);
  22. }
  23. public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
  24. super(context, attrs, defStyle);
  25. this.setStaticTransformationsEnabled(true);
  26. }
  27. public int getMaxRotationAngle() {
  28. return mMaxRotationAngle;
  29. }
  30. public void setMaxRotationAngle(int maxRotationAngle) {
  31. mMaxRotationAngle = maxRotationAngle;
  32. }
  33. public int getMaxZoom() {
  34. return mMaxZoom;
  35. }
  36. public void setMaxZoom(int maxZoom) {
  37. mMaxZoom = maxZoom;
  38. }
  39. private int getCenterOfCoverflow() {
  40. return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
  41. + getPaddingLeft();
  42. }
  43. private static int getCenterOfView(View view) {
  44. return view.getLeft() + view.getWidth() / 2;
  45. }
  46. protected boolean getChildStaticTransformation(View child, Transformation t) {
  47. final int childCenter = getCenterOfView(child);
  48. final int childWidth = child.getWidth();
  49. int rotationAngle = 0;
  50. t.clear();
  51. t.setTransformationType(Transformation.TYPE_MATRIX);
  52. if (childCenter == mCoveflowCenter) {
  53. transformImageBitmap((ImageView) child, t, 0);
  54. } else {
  55. rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
  56. if (Math.abs(rotationAngle) > mMaxRotationAngle) {
  57. rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle
  58. : mMaxRotationAngle;
  59. }
  60. transformImageBitmap((ImageView) child, t, rotationAngle);
  61. }
  62. return true;
  63. }
  64. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  65. mCoveflowCenter = getCenterOfCoverflow();
  66. super.onSizeChanged(w, h, oldw, oldh);
  67. }
  68. private void transformImageBitmap(ImageView child, Transformation t,
  69. int rotationAngle) {
  70. mCamera.save();
  71. final Matrix imageMatrix = t.getMatrix();
  72. final int imageHeight = child.getLayoutParams().height;
  73. final int imageWidth = child.getLayoutParams().width;
  74. final int rotation = Math.abs(rotationAngle);
  75. // 在Z轴上正向移动camera的视角,实际效果为放大图片。
  76. // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。
  77. mCamera.translate(0.0f, 0.0f, 100.0f);
  78. // As the angle of the view gets less, zoom in
  79. if (rotation < mMaxRotationAngle) {
  80. float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
  81. mCamera.translate(0.0f, 0.0f, zoomAmount);
  82. }
  83. // 在Y轴上旋转,对应图片竖向向里翻转。
  84. // 如果在X轴上旋转,则对应图片横向向里翻转。
  85. mCamera.rotateY(rotationAngle);
  86. mCamera.getMatrix(imageMatrix);
  87. imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
  88. imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
  89. mCamera.restore();
  90. }
  91. }

源代码:点击打开链接

最新文章

  1. ubuntu 下安装 texlive 并设置 ctex 中文套装
  2. 编译llvm+clang
  3. centos7.2 yum安装lamp环境
  4. Oracle存储过程写法
  5. poj 1201 Intervals(差分约束)
  6. Linux命令面试常考的简单汇总
  7. iPhone分辨率
  8. 将USBASP改造成STK502编程器(转)
  9. Entity Framework - 基于外键关联的单向一对一关系
  10. python开发:初识python
  11. java框架篇---hibernate(多对多)映射关系
  12. LINUX部署SVN服务器
  13. 标准JSF的生命周期
  14. spring mvc中的一些注释:@PathVariable @RequestParam等
  15. Hive--可执行SQL的Hadoop数据仓库管理工具
  16. C++ 和 MFC的学习
  17. 新浪云部署java web程序 注意事项
  18. Maven nexus 安装nexus私服出现的两个问题
  19. J2EE--Hibernate基础笔记
  20. Java操作Redis工具类

热门文章

  1. Hadoop2 伪分布式部署
  2. BZOJ 2982 combination Lucas定理
  3. 菜鸟之webservice(一) 服务端搭建
  4. HDU 5370 Tree Maker
  5. HBase的单节点集群详细启动步骤(分为Zookeeper自带还是外装)
  6. 利用CORS实现跨域请求--转
  7. 负载均衡-lvs
  8. Web API总结
  9. refresh table tablename ;MSCK REPAIR TABLE table_name;
  10. Firewalld 用法解析