http://blog.csdn.net/sunboy_2050/article/details/7483169


上篇介绍了使用Animation实现3D动画旋转翻页效果,现在介绍图片倒影实现,先看效果图

Android 滑动效果进阶篇(六)—— 倒影效果_第1张图片

本示例主要通过自定义Gallery和ImageAdapter(继承自BaseAdapter)实现


1、倒影绘制

ImageAdapter继承自BaseAdapter,详细实现可见Android 滑动效果入门篇(二)—— Gallery这里重点介绍倒影原理及实现

倒影原理:

倒影效果是主要由原图+间距+倒影三部分组成,高度大约为原图的3/2(原图为1、倒影为1/2)

原图,就是我们看到了最开始的图片

间距,是原图与倒影之间的间隙,如:reflectionGap = 4;

倒影,是原图下半部分1/2高度,通过矩阵变换matrix.preScale(1, -1); 获取倒立图片,然后再加上线性遮罩和阴影实现


倒影实现:

[java] view plain copy print ?
  1. /**反射倒影*/
  2. publicbooleancreateReflectedImages(){
  3. finalintreflectionGap=4;
  4. intindex=0;
  5. for(Map<String,Object>map:list){
  6. Integerid=(Integer)map.get("image");
  7. BitmaporiginalImage=BitmapFactory.decodeResource(mContext.getResources(),id);//获取原始图片
  8. intwidth=originalImage.getWidth();
  9. intheight=originalImage.getHeight();
  10. Matrixmatrix=newMatrix();
  11. matrix.preScale(1,-1);//图片矩阵变换(从低部向顶部的倒影)
  12. BitmapreflectionImage=Bitmap.createBitmap(originalImage,0,height/2,width,height/2,matrix,false);//截取原图下半部分
  13. BitmapbitmapWithReflection=Bitmap.createBitmap(width,(height+height/2),Config.ARGB_8888);//创建倒影图片(高度为原图3/2)
  14. Canvascanvas=newCanvas(bitmapWithReflection);//绘制倒影图(原图+间距+倒影)
  15. canvas.drawBitmap(originalImage,0,0,null);//绘制原图
  16. Paintpaint=newPaint();
  17. canvas.drawRect(0,height,width,height+reflectionGap,paint);//绘制原图与倒影的间距
  18. canvas.drawBitmap(reflectionImage,0,height+reflectionGap,null);//绘制倒影图
  19. paint=newPaint();
  20. LinearGradientshader=newLinearGradient(0,originalImage.getHeight(),0,bitmapWithReflection.getHeight()+reflectionGap,0x70ffffff,0x00ffffff,TileMode.CLAMP);
  21. paint.setShader(shader);//线性渐变效果
  22. paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));//倒影遮罩效果
  23. canvas.drawRect(0,height,width,bitmapWithReflection.getHeight()+reflectionGap,paint);//绘制倒影的阴影效果
  24. ImageViewimageView=newImageView(mContext);
  25. imageView.setImageBitmap(bitmapWithReflection);//设置倒影图片
  26. imageView.setLayoutParams(newmyGallery.LayoutParams(180,240));
  27. imageView.setScaleType(ScaleType.MATRIX);
  28. mImages[index++]=imageView;
  29. }
  30. returntrue;
  31. }

2、myGallery

自定义Gallery来实现倒影图片的浏览与选择

[java] view plain copy print ?
  1. publicclassmyGalleryextendsGallery{
  2. privateCameramCamera=newCamera();
  3. privateintmMaxRotationAngle=60;//最大旋转角度60
  4. privateintmMaxZoom=-120;
  5. privateintmCoveflowCenter;
  6. publicmyGallery(Contextcontext){
  7. super(context);
  8. this.setStaticTransformationsEnabled(true);
  9. }
  10. publicmyGallery(Contextcontext,AttributeSetattrs){
  11. super(context,attrs);
  12. this.setStaticTransformationsEnabled(true);
  13. }
  14. publicmyGallery(Contextcontext,AttributeSetattrs,intdefStyle){
  15. super(context,attrs,defStyle);
  16. this.setStaticTransformationsEnabled(true);
  17. }
  18. publicintgetMaxRotationAngle(){
  19. returnmMaxRotationAngle;
  20. }
  21. publicvoidsetMaxRotationAngle(intmaxRotationAngle){
  22. mMaxRotationAngle=maxRotationAngle;
  23. }
  24. publicintgetMaxZoom(){
  25. returnmMaxZoom;
  26. }
  27. publicvoidsetMaxZoom(intmaxZoom){
  28. mMaxZoom=maxZoom;
  29. }
  30. /**获取Gallery的中心x*/
  31. privateintgetCenterOfCoverflow(){
  32. return(getWidth()-getPaddingLeft()-getPaddingRight())/2+getPaddingLeft();
  33. }
  34. /**获取View的中心x*/
  35. privatestaticintgetCenterOfView(Viewview){
  36. returnview.getLeft()+view.getWidth()/2;
  37. }
  38. @Override
  39. protectedvoidonSizeChanged(intw,inth,intoldw,intoldh){
  40. mCoveflowCenter=getCenterOfCoverflow();
  41. super.onSizeChanged(w,h,oldw,oldh);
  42. }
  43. @Override
  44. protectedbooleangetChildStaticTransformation(Viewchild,Transformationtrans){
  45. finalintchildCenter=getCenterOfView(child);
  46. finalintchildWidth=child.getWidth();
  47. introtationAngle=0;
  48. trans.clear();
  49. trans.setTransformationType(Transformation.TYPE_BOTH);//alpha和matrix都变换
  50. if(childCenter==mCoveflowCenter){//正中间的childView
  51. transformImageBitmap((ImageView)child,trans,0);
  52. }else{//两侧的childView
  53. rotationAngle=(int)(((float)(mCoveflowCenter-childCenter)/childWidth)*mMaxRotationAngle);
  54. if(Math.abs(rotationAngle)>mMaxRotationAngle){
  55. rotationAngle=(rotationAngle<0)?-mMaxRotationAngle:mMaxRotationAngle;
  56. }
  57. transformImageBitmap((ImageView)child,trans,rotationAngle);
  58. }
  59. returntrue;
  60. }
  61. privatevoidtransformImageBitmap(ImageViewchild,Transformationtrans,introtationAngle){
  62. mCamera.save();
  63. finalMatriximageMatrix=trans.getMatrix();
  64. finalintimageHeight=child.getLayoutParams().height;
  65. finalintimageWidth=child.getLayoutParams().width;
  66. finalintrotation=Math.abs(rotationAngle);
  67. //在Z轴上正向移动camera的视角,实际效果为放大图片;如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。
  68. mCamera.translate(0.0f,0.0f,100.0f);
  69. //Astheangleoftheviewgetsless,zoomin
  70. if(rotation<mMaxRotationAngle){
  71. floatzoomAmount=(float)(mMaxZoom+(rotation*1.5));
  72. mCamera.translate(0.0f,0.0f,zoomAmount);
  73. }
  74. mCamera.rotateY(rotationAngle);//rotationAngle为正,沿y轴向内旋转;为负,沿y轴向外旋转
  75. mCamera.getMatrix(imageMatrix);
  76. imageMatrix.preTranslate(-(imageWidth/2),-(imageHeight/2));
  77. imageMatrix.postTranslate((imageWidth/2),(imageHeight/2));
  78. mCamera.restore();
  79. }
  80. }

3、Activity

Activity中,主要实现自定义Gallery的图片填充ImageAdapter、myGallery选择事件监听、点击事件监听

[java] view plain copy print ?
  1. privatevoidinitRes(){
  2. tvTitle=(TextView)findViewById(R.id.tvTitle);
  3. gallery=(myGallery)findViewById(R.id.mygallery);//获取自定义的myGallery控件
  4. adapter=newImageAdapter(this);
  5. adapter.createReflectedImages();//创建倒影效果
  6. gallery.setAdapter(adapter);
  7. gallery.setOnItemSelectedListener(newOnItemSelectedListener(){//设置选择事件监听
  8. @Override
  9. publicvoidonItemSelected(AdapterView<?>parent,Viewview,intposition,longid){
  10. tvTitle.setText(adapter.titles[position]);
  11. }
  12. @Override
  13. publicvoidonNothingSelected(AdapterView<?>parent){
  14. }
  15. });
  16. gallery.setOnItemClickListener(newOnItemClickListener(){//设置点击事件监听
  17. @Override
  18. publicvoidonItemClick(AdapterView<?>parent,Viewview,intposition,longid){
  19. Toast.makeText(Main.this,"img"+(position+1)+"selected",Toast.LENGTH_SHORT).show();
  20. }
  21. });
  22. }

main.xml布局文件中,通过实现自定义的myGallery,来显示图片集合

[html] view plain copy print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:id="@+id/tvTitle"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:layout_centerHorizontal="true"
  11. android:textSize="16sp"/>
  12. <com.homer.reflect.myGallery
  13. android:id="@+id/mygallery"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:layout_below="@id/tvTitle"
  17. android:layout_marginTop="10dip"/>
  18. </RelativeLayout>


源码下载



参考推荐:

Android实现图片的倒影效果

Android中图片倒影、圆角效果重绘


更多相关文章

  1. 利用多张图片实现动态图
  2. android读取大图片并缓存
  3. Android多媒体学习二:检索Android的图片库,并显示
  4. Android工作学习笔记之图片自适应imageview属性android:scaleTyp
  5. android图片压缩总结
  6. 从网络获取图片,并缓存到SD卡
  7. ionic3 图片选取imagepicker以及camera汉化
  8. Android修改图片颜色-转成灰度图

随机推荐

  1. 关于android图片的传输,android图片传输方
  2. android中发生OOM探究及解决
  3. Unity3D For Android 开发教程【转http:/
  4. Android(安卓)Things学习的一点体验
  5. Android Q (十七) Android Q 行为变更:以
  6. android 图片处理 (滤镜,图片位置)
  7. 报道:Android成为96%的新手机恶意软件的宿
  8. Android(安卓)Studio底边栏选项不见了,如
  9. android App设计的工具+灵感
  10. Android中Http传递参数json参数请求数据