android多张图片翻转和倒影效果

来源:互联网 发布:mp5冲锋枪知乎 编辑:程序博客网 时间:2024/05/01 03:01

  在无事的时候想做一个好看的手机界面,尤其是在软件刚开始启动时的界面展示,在网上搜索相关的设计,后来发现画廊的展示效果挺好看的,在网上找到资源查看实例代码,就自己学习每行代码意义和实现的功能,具体转载的实例网址不记得了,希望原创见谅,想通过此方式分享更多的人,同时也是对自己的总结和未来的代码复用做基础。

下面来介绍自己界面设计学习的过程,具体介绍功能都不一一介绍了,每行代码功能实现都有注释,便于随学随懂,理解如何实现的。

实现效果:

 

 

1.代码展示:实现图片的翻转效果,可以自定义翻转角度,实现翻转的图片会有一定的尺寸缩小,实现过程应用到重写gallery的相关方法来控制实现翻转。

 

[java] view plaincopy
  1. public class CoverFlow extends Gallery {  
  2.     private Camera mCamera = new Camera();// 相机类  
  3.     private int mMaxRotationAngle = 60;// 最大转动角度  
  4.     private int mMaxZoom = -280;// //最大缩放值  
  5.     private int mCoveflowCenter;// 半径值  
  6.   
  7.     public CoverFlow(Context context) {  
  8.         super(context);  
  9.         // 支持转换 ,执行getChildStaticTransformation方法  
  10.         this.setStaticTransformationsEnabled(true);  
  11.     }  
  12.   
  13.     public CoverFlow(Context context, AttributeSet attrs) {  
  14.         super(context, attrs);  
  15.         this.setStaticTransformationsEnabled(true);  
  16.     }  
  17.   
  18.     public CoverFlow(Context context, AttributeSet attrs, int defStyle) {  
  19.         super(context, attrs, defStyle);  
  20.         this.setStaticTransformationsEnabled(true);  
  21.     }  
  22.   
  23.     /** * 获取旋转最大角度 * @return */  
  24.     public int getMaxRotationAngle() {  
  25.         return mMaxRotationAngle;  
  26.     }  
  27.   
  28.     /** * 设置旋转最大角度 * @param maxRotationAngle */  
  29.     public void setMaxRotationAngle(int maxRotationAngle) {  
  30.         mMaxRotationAngle = maxRotationAngle;  
  31.     }  
  32.   
  33.     /** * 获取最大缩放值 * @return */  
  34.     public int getMaxZoom() {  
  35.         return mMaxZoom;  
  36.     }  
  37.   
  38.     /** * 设置最大缩放值 * @param maxZoom */  
  39.     public void setMaxZoom(int maxZoom) {  
  40.         mMaxZoom = maxZoom;  
  41.     }  
  42.   
  43.     /** * 获取半径值 * @return */  
  44.     private int getCenterOfCoverflow() {  
  45.         return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2  
  46.                 + getPaddingLeft();  
  47.     }  
  48.   
  49.     /** * @param view * @return */  
  50.     private static int getCenterOfView(View view) {  
  51.         return view.getLeft() + view.getWidth() / 2;  
  52.     }  
  53.   
  54.     // 控制gallery中每个图片的旋转(重写的gallery中方法)  
  55.     protected boolean getChildStaticTransformation(View child, Transformation t) {  
  56.         // 取得当前子view的半径值  
  57.         final int childCenter = getCenterOfView(child);  
  58.         final int childWidth = child.getWidth();  
  59.         // 旋转角度  
  60.         int rotationAngle = 0;  
  61.         // 重置转换状态  
  62.         t.clear();  
  63.         // 设置转换类型  
  64.         t.setTransformationType(Transformation.TYPE_MATRIX);  
  65.         // 如果图片位于中心位置不需要进行旋转  
  66.         if (childCenter == mCoveflowCenter) {  
  67.             transformImageBitmap((ImageView) child, t, 0);  
  68.         } else {  
  69.             // 根据图片在gallery中的位置来计算图片的旋转角度  
  70.             rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);  
  71.             System.out.println("rotationAngle:" + rotationAngle);  
  72.             // 如果旋转角度绝对值大于最大旋转角度返回(-mMaxRotationAngle或mMaxRotationAngle;)  
  73.             if (Math.abs(rotationAngle) > mMaxRotationAngle) {  
  74.                 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle  
  75.                         : mMaxRotationAngle;  
  76.             }  
  77.             transformImageBitmap((ImageView) child, t, rotationAngle);  
  78.         }  
  79.         return true;  
  80.     }  
  81.   
  82.     /** * */  
  83.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  84.         mCoveflowCenter = getCenterOfCoverflow();  
  85.         super.onSizeChanged(w, h, oldw, oldh);  
  86.     }  
  87.   
  88.     private void transformImageBitmap(ImageView child, Transformation t,  
  89.             int rotationAngle) {  
  90.         // 对效果进行保存  
  91.         mCamera.save();  
  92.         final Matrix imageMatrix = t.getMatrix();  
  93.         // 图片高度  
  94.         final int imageHeight = child.getLayoutParams().height;  
  95.         // 图片宽度  
  96.         final int imageWidth = child.getLayoutParams().width;  
  97.   
  98.         // 返回旋转角度的绝对值  
  99.         final int rotation = Math.abs(rotationAngle);  
  100.   
  101.         // 在Z轴上正向移动camera的视角,实际效果为放大图片。  
  102.         // 如果在Y轴上移动,则图片上下移动;X轴上对应图片左右移动。  
  103.         mCamera.translate(0.0f, 0.0f, 100.0f);  
  104.         // As the angle of the view gets less, zoom in  
  105.         if (rotation < mMaxRotationAngle) {  
  106.             float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));  
  107.             mCamera.translate(0.0f, 0.0f, zoomAmount);  
  108.         }  
  109.         // 在Y轴上旋转,对应图片竖向向里翻转。  
  110.         // 如果在X轴上旋转,则对应图片横向向里翻转。  
  111.         mCamera.rotateY(rotationAngle);  
  112.         mCamera.getMatrix(imageMatrix);  
  113.         imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));  
  114.         imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));  
  115.         mCamera.restore();  
  116.     }  
  117. }  

2.图片的倒影功能实现步骤,关键代码实现及注释解析:

创建一个类继承BaseAdapter ,创建createReflectedImages方法来定义自己想要显示的图片倒影效果,及可以设置图片间的间距及倒影大小、透明度,方法主体内容是:

[java] view plaincopy
  1. public boolean createReflectedImages() {  
  2.         final int reflectionGap = 4;// 倒影图和原图之间的距离  
  3.         int index = 0;  
  4.         for (int imageId : mImageIds) {  
  5.             // 返回原图解码之后的bitmap对象  
  6.             Bitmap originalImage = BitmapFactory.decodeResource(  
  7.                     mContext.getResources(), imageId);  
  8.             /*防止溢出。 
  9.              * InputStream is = 
  10.              * mContext.getResources().openRawResource(imageId); Bitmap 
  11.              * originalImage = BitmapFactory.decodeStream(is); 
  12.              */  
  13.             int width = originalImage.getWidth();  
  14.             int height = originalImage.getHeight();  
  15.             // 创建矩阵对象  
  16.             Matrix matrix = new Matrix();  
  17.   
  18.             // 指定一个角度以0,0为坐标  
  19.             // matrix.setRotate(30);  
  20.   
  21.             // 指定矩阵(x轴不变,y轴相反)屏幕中央两边图片旋转方向不同  
  22.             matrix.preScale(1, -1);  
  23.   
  24.             // 将矩阵应用到该原图之中,返回一个宽度不变,高度为原图1/2的倒影位图  
  25.             Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,  
  26.                     height / 2, width, height / 2, matrix, false);  
  27.   
  28.             // 创建一个宽度不变,高度为原图+倒影图高度的位图  
  29.             Bitmap bitmapWithReflection = Bitmap.createBitmap(width,  
  30.                     (height + height / 2), Config.ARGB_8888);  
  31.   
  32.             // 将上面创建的位图初始化到画布  
  33.             Canvas canvas = new Canvas(bitmapWithReflection);  
  34.             canvas.drawBitmap(originalImage, 00null);  
  35.   
  36.             Paint deafaultPaint = new Paint();  
  37.             deafaultPaint.setAntiAlias(false);  
  38.             /*canvas.drawRect(0, height, width, height + reflectionGap, 
  39.                     deafaultPaint);*/  
  40.             canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);  
  41.             Paint paint = new Paint();  
  42.             paint.setAntiAlias(false);  
  43.   
  44.             /** 
  45.              * 参数一:为渐变起初点坐标x位置, 参数二:为y轴位置, 参数三和四:分辨对应渐变终点, 最后参数为平铺方式, 
  46.              * 这里设置为镜像Gradient是基于Shader类,所以我们通过Paint的setShader方法来设置这个渐变 
  47.              */  
  48.             LinearGradient shader = new LinearGradient(0,  
  49.                     originalImage.getHeight(), 0,  
  50.                     bitmapWithReflection.getHeight() + reflectionGap,  
  51.                     0x70ffffff0x00ffffff, TileMode.MIRROR);  
  52.             // 设置阴影  
  53.             paint.setShader(shader);  
  54.             paint.setXfermode(new PorterDuffXfermode(  
  55.                     android.graphics.PorterDuff.Mode.DST_IN));  
  56.             // 用已经定义好的画笔构建一个矩形阴影渐变效果  
  57.             canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()  
  58.                     + reflectionGap, paint);  
  59.   
  60.             // 创建一个ImageView用来显示已经画好的bitmapWithReflection  
  61.             ImageView imageView = new ImageView(mContext);  
  62.             imageView.setImageBitmap(bitmapWithReflection);  
  63.             // 设置imageView大小 ,也就是最终显示的图片大小  
  64.             imageView.setLayoutParams(new CoverFlow.LayoutParams(140200));  
  65.             // imageView.setScaleType(ScaleType.MATRIX);  
  66.             mImages[index++] = imageView;  
  67.         }  
  68.         return true;  
  69.     }  


3.创建Activity来实现图片翻转、倒影功能,关键代码

[java] view plaincopy
  1.                 ImageAdapter adapter = new ImageAdapter(this, images);//new实现图片倒影功能的类,images为图片数组,构造方法传参  
  2. adapter.createReflectedImages();// 适配器添加倒影效果  
  3. CoverFlow flow = (CoverFlow) this.findViewById(R.id.gallery);                 /*                                 xml的布局文件:<包.CoverFlowandroid:id="@+id/gallery" />        */  
  4. flow.setFadingEdgeLength(0);//  
  5. flow.setSpacing(2); // 图片之间的间距  
  6. flow.setAdapter(adapter);  
  7.   
  8. flow.setOnItemClickListener(new OnItemClickListener() {  
  9.     public void onItemClick(AdapterView<?> parent, View view,  
  10.             int position, long id) {  
  11.         Toast.makeText(getApplicationContext(),  
  12.                 String.valueOf(position), Toast.LENGTH_SHORT).show();  
  13.     }  
  14.   
  15. });//点击当前图片触发事件  
  16. flow.setSelection(4); // 默认先选择第四张图片  


总结:这个案例里面牵涉的内容和新知识点不少,但是只要肯学习,每一个技术都不是难学的,掌握类继承和方法重写,及逻辑能力的学习,能够明确每一行代码要实现的。切记不要忘了给翻转的图片组设置自己重写的倒影适配器,flow.setAdapter(适配器);

0 0
原创粉丝点击