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

项目代码结构如下:

layout_gallery.xml是Gallery的布局文件:

[html] view plaincopy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:background="#ffffff"
  7. >
  8. <com.gallery.GalleryFlow
  9. android:id="@+id/Gallery01"
  10. android:layout_width="fill_parent"
  11. android:layout_height="wrap_content"android:layout_centerInParent="true"
  12. android:spacing="-60px"/>
  13. </RelativeLayout>
  14. ndroid:spacing="-60px"图片之间的间距。

ActivityMain主要代码如下:

[java] view plaincopy
  1. publicvoidonCreate(BundlesavedInstanceState){
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.layout_gallery);
  4. //将图片做成倒影效果
  5. Integer[]images={R.drawable.hawana_0,
  6. R.drawable.huanyinghei_6,R.drawable.wulonghui_24,R.drawable.wulonghui_28,
  7. R.drawable.xuanfenghei_10};
  8. ImageAdapteradapter=newImageAdapter(this,images);
  9. adapter.createReflectedImages();
  10. GalleryFlowgalleryFlow=(GalleryFlow)findViewById(R.id.Gallery01);
  11. galleryFlow.setAdapter(adapter);

其中:

[java] view plaincopy
  1. ImageAdapteradapter=newImageAdapter(this,images);
  2. adapter.createReflectedImages();

实现了图片的倒影效果,参见:Android实现图片的倒影效果。

GalleryFlow实现了图片的3D旋转效果:

下边的方法是根据传入的角度,做图片的旋转:

[java] view plaincopy
  1. privatevoidtransformImageBitmap(ImageViewchild,Transformationt,
  2. introtationAngle){
  3. mCamera.save();
  4. finalMatriximageMatrix=t.getMatrix();
  5. finalintimageHeight=child.getLayoutParams().height;
  6. finalintimageWidth=child.getLayoutParams().width;
  7. finalintrotation=Math.abs(rotationAngle);
  8. //在Z轴上正向移动camera的视角,实际效果为放大图片。
  9. //如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。
  10. mCamera.translate(0.0f,0.0f,100.0f);
  11. ////Astheangleoftheviewgetsless,zoomin
  12. //if(rotation<mMaxRotationAngle){
  13. //floatzoomAmount=(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();

实现根据图片的不同位置,确定旋转角度:

[java] view plaincopy
  1. protectedbooleangetChildStaticTransformation(Viewchild,Transformationt){
  2. finalintchildCenter=getCenterOfView(child);
  3. finalintchildWidth=child.getWidth();
  4. introtationAngle=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. transformImageBitmap((ImageView)child,t,rotationAngle);
  21. }
  22. returntrue;
  23. }

整体效果还不是很好,并不是真正的CoverFlow,需要进一步改进。

源码见:http://bigcateasymorse.googlecode.com/svn/trunk/android-coverflow/


更多相关文章

  1. android图片特效,图片过滤
  2. 不支持gif动态图片
  3. Android刮刮卡效果实现代码
  4. Android进阶2之检索Android的图片库并显示图片详细信息
  5. TextView实现跑马灯效果 就这么简单!
  6. Android保存图片到系统图库
  7. 仿网易新闻顶部滑动条效果
  8. 【Android】获取图片和视频缩略图
  9. Android使用HttpClient下载图片

随机推荐

  1. Android学习(10) -- 常见布局
  2. [android]-xml解析示便-SAX
  3. WeakReference在android中的使用场景
  4. Android第十二课 jni函数的静态绑定
  5. Android(安卓)取消GridView和ListView it
  6. 换一种方式理解 Android协程
  7. Android(安卓)SlidingMenu 的实现
  8. android下数据库SQLite
  9. android创建txt文件,读取txt文件内容
  10. Android录音上————AudioRecord实现录