Android自定义Gallery,实现CoverFlow效果

===============eoeAndroid社区推荐:=======================

使用Android自带Gallery组件实现CoverFlow,源码+解析
http://www.eoeandroid.com/thread-39709-1-1.html

代码仓库 会员贡献索引贴
http://www.eoeandroid.com/thread-21964-1-1.html

Android Gallery3D源码分析(一)
http://www.eoeandroid.com/thread-83537-1-1.html

===============帖子正文==============================

android 实现显示文字的GalleryAndroid修改Gallery页面布局的基础上,利用Gallery实现了CoverFlow效果,如下:
 

 

项目代码结构如下:
 

layout_gallery.xml是Gallery的布局文件:



  1. <relativelayout xmlns:android="[url=http://schemas.android.com/apk/res/android]http://schemas.android.com/apk/res/android[/url]" 
  2.     android:orientation="vertical" 
  3.     android:layout_width="fill_parent" 
  4.     android:layout_height="fill_parent" 
  5.     android:background="#ffffff" 
  6.     > 
  7. <com.gallery.galleryflow 
  8.     android:id="@+id/Gallery01" 
  9.     android:layout_width="fill_parent" 
  10.     android:layout_height="wrap_content" android:layout_centerInParent="true" 
  11.     android:spacing="-60px"/> 
复制代码
android:spacing="-60px" 图片之间的间距。ActivityMain主要代码如下:

  1. public void onCreate(Bundle savedInstanceState) { 
  2.         super.onCreate(savedInstanceState); 
  3.         
  4. setContentView(R.layout.layout_gallery); 
  5.         //将图片做成倒影效果 
  6.         Integer[] images = { R.drawable.hawana_0, 
  7.                 R.drawable.huanyinghei_6, R.drawable.wulonghui_24, R.drawable.wulonghui_28, 
  8.                 R.drawable.xuanfenghei_10}; 
  9.         ImageAdapter adapter = new ImageAdapter(this, images); 
  10.         adapter.createReflectedImages();

  11.         GalleryFlow galleryFlow = (GalleryFlow) findViewById(R.id.Gallery01); 
  12.         galleryFlow.setAdapter(adapter);
复制代码
其中:

  1. ImageAdapter adapter = new ImageAdapter(this, images); 
  2.         adapter.createReflectedImages();
复制代码
实现了图片的倒影效果,参见:android实现图片的倒影效果GalleryFlow实现了图片的3D旋转效果:下边的方法是根据传入的角度,做图片的旋转:

  1. private void transformImageBitmap(ImageView child, Transformation t, 
  2.             int rotationAngle) { 
  3.         mCamera.save(); 
  4.         final Matrix imageMatrix = t.getMatrix(); 
  5.         final int imageHeight = child.getLayoutParams().height; 
  6.         final int imageWidth = child.getLayoutParams().width; 
  7.         final int rotation = Math.abs(rotationAngle);

  8.         // 在Z轴上正向移动camera的视角,实际效果为放大图片。 
  9.         // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。 
  10.         mCamera.translate(0.0f, 0.0f, 100.0f);

  11. //        // As the angle of the view gets less, zoom in 
  12. //        if (rotation < mMaxRotationAngle) { 
  13. //            float zoomAmount = (float) (mMaxZoom + (rotation * 1.5)); 
  14. //            //mCamera.translate(0.0f, 0.0f, zoomAmount); 
  15. //        }

  16.         // 在Y轴上旋转,对应图片竖向向里翻转。 
  17.         // 如果在X轴上旋转,则对应图片横向向里翻转。 
  18.         mCamera.rotateY(rotationAngle); 
  19.         mCamera.getMatrix(imageMatrix); 
  20.         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2)); 
  21.         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2)); 
  22.         mCamera.restore();
复制代码
实现根据图片的不同位置,确定旋转角度:

  1. protected boolean getChildStaticTransformation(View child, Transformation t) {

  2.     final int childCenter = getCenterOfView(child); 
  3.     final int childWidth = child.getWidth(); 
  4.     int rotationAngle = 0;

  5.     t.clear(); 
  6.     t.setTransformationType(Transformation.TYPE_MATRIX); 
  7.     Log.v("tag", "getChildStaticTransformation>>>>>>>>>>>>>>>>>>>childCenter"+childCenter+">>>>>"+Math.abs((mCoveflowCenter-childCenter)/(childWidth))); 
  8.     if (childCenter == mCoveflowCenter) { 
  9.         transformImageBitmap((ImageView) child, t, 0); 
  10.     } else { 
  11.         if ((mCoveflowCenter – childCenter) > 0) { 
  12.             rotationAngle = (int) mMaxRotationAngle; 
  13.         } else { 
  14.             rotationAngle = (int) -mMaxRotationAngle; 
  15.         } 
  16.         if(Math.abs((mCoveflowCenter-childCenter)/(childWidth/2))==0){ 
  17.              rotationAngle = (int) (((float) (mCoveflowCenter – childCenter) / 
  18.              childWidth) * mMaxRotationAngle); 
  19.         } 
  20.     
  21.         transformImageBitmap((ImageView) child, t, rotationAngle); 
  22.     }

  23.     return true; 
  24. }
复制代码
整体效果还不是很好,并不是真正的CoverFlow,需要进一步改进。源码见: android-coverflow.rar (2.88 MB, 下载次数: 1602) 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值