Android自定义Gallery,实现CoverFlow效果
16lz
2021-01-23
在Android实现显示文字的Gallery和Android修改Gallery页面布局的基础上,利用Gallery实现了CoverFlow效果,如下:
项目代码结构如下:
layout_gallery.xml是Gallery的布局文件:
[html] view plaincopy- <?xmlversion="1.0"encoding="utf-8"?>
- <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#ffffff"
- >
- <com.gallery.GalleryFlow
- android:id="@+id/Gallery01"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"android:layout_centerInParent="true"
- android:spacing="-60px"/>
- </RelativeLayout>
- ndroid:spacing="-60px"图片之间的间距。
ActivityMain主要代码如下:
[java] view plaincopy
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.layout_gallery);
- //将图片做成倒影效果
- Integer[]images={R.drawable.hawana_0,
- R.drawable.huanyinghei_6,R.drawable.wulonghui_24,R.drawable.wulonghui_28,
- R.drawable.xuanfenghei_10};
- ImageAdapteradapter=newImageAdapter(this,images);
- adapter.createReflectedImages();
- GalleryFlowgalleryFlow=(GalleryFlow)findViewById(R.id.Gallery01);
- galleryFlow.setAdapter(adapter);
其中:
[java] view plaincopy- ImageAdapteradapter=newImageAdapter(this,images);
- adapter.createReflectedImages();
实现了图片的倒影效果,参见:Android实现图片的倒影效果。
GalleryFlow实现了图片的3D旋转效果:
下边的方法是根据传入的角度,做图片的旋转:
[java] view plaincopy
- privatevoidtransformImageBitmap(ImageViewchild,Transformationt,
- introtationAngle){
- mCamera.save();
- finalMatriximageMatrix=t.getMatrix();
- finalintimageHeight=child.getLayoutParams().height;
- finalintimageWidth=child.getLayoutParams().width;
- finalintrotation=Math.abs(rotationAngle);
- //在Z轴上正向移动camera的视角,实际效果为放大图片。
- //如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。
- mCamera.translate(0.0f,0.0f,100.0f);
- ////Astheangleoftheviewgetsless,zoomin
- //if(rotation<mMaxRotationAngle){
- //floatzoomAmount=(float)(mMaxZoom+(rotation*1.5));
- ////mCamera.translate(0.0f,0.0f,zoomAmount);
- //}
- //在Y轴上旋转,对应图片竖向向里翻转。
- //如果在X轴上旋转,则对应图片横向向里翻转。
- mCamera.rotateY(rotationAngle);
- mCamera.getMatrix(imageMatrix);
- imageMatrix.preTranslate(-(imageWidth/2),-(imageHeight/2));
- imageMatrix.postTranslate((imageWidth/2),(imageHeight/2));
- mCamera.restore();
实现根据图片的不同位置,确定旋转角度:
[java] view plaincopy
- protectedbooleangetChildStaticTransformation(Viewchild,Transformationt){
- finalintchildCenter=getCenterOfView(child);
- finalintchildWidth=child.getWidth();
- introtationAngle=0;
- t.clear();
- t.setTransformationType(Transformation.TYPE_MATRIX);
- Log.v("tag","getChildStaticTransformation>>>>>>>>>>>>>>>>>>>childCenter"+childCenter+">>>>>"+Math.abs((mCoveflowCenter-childCenter)/(childWidth)));
- if(childCenter==mCoveflowCenter){
- transformImageBitmap((ImageView)child,t,0);
- }else{
- if((mCoveflowCenter–childCenter)>0){
- rotationAngle=(int)mMaxRotationAngle;
- }else{
- rotationAngle=(int)-mMaxRotationAngle;
- }
- if(Math.abs((mCoveflowCenter-childCenter)/(childWidth/2))==0){
- rotationAngle=(int)(((float)(mCoveflowCenter–childCenter)/
- childWidth)*mMaxRotationAngle);
- }
- transformImageBitmap((ImageView)child,t,rotationAngle);
- }
- returntrue;
- }
整体效果还不是很好,并不是真正的CoverFlow,需要进一步改进。
源码见:http://bigcateasymorse.googlecode.com/svn/trunk/android-coverflow/
更多相关文章
- android图片特效,图片过滤
- 不支持gif动态图片
- Android刮刮卡效果实现代码
- Android进阶2之检索Android的图片库并显示图片详细信息
- TextView实现跑马灯效果 就这么简单!
- Android保存图片到系统图库
- 仿网易新闻顶部滑动条效果
- 【Android】获取图片和视频缩略图
- Android使用HttpClient下载图片