Android自带的Gallery画廊控件位于屏幕的底部,是很不错很漂亮的一个控件。我们也可以自己更改gallery,来实现不同的效果,也可以加上倒影,翻转等华丽的界面,一直都比较喜欢研究android的UI,所以特别留意了一下有关重写Gallery的方法,网上有很多种样式,我找到了一个比较普遍的改写样式,找不到原作者了,自己收藏起来并重新做了调整,加上了更为详细的注释,便于日后有需要的时候用,这个Demo完全可以自己作为工具类在以后做图片处理的时候使用,方便,下面是实现后的效果图以及部分代码,可在文章底部下载本demo完整代码~!

                                           

自定义的gallery:

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


仅仅是收藏做学习用,虽然界面还不够漂亮,但是可以慢慢研究里面的代码自行改动,下面是源代码

 

点击下载demo源代码 

 

 

posted on 2012-08-30 11:24  Mr梵谷  阅读(2588)  评论(0编辑  收藏  举报