效果图差不多这样子
print? 1. import android.content.Context; 2. import android.graphics.Camera; 3. import android.graphics.Matrix; 4. import android.util.AttributeSet; 5. import android.view.View; 6. import android.view.animation.Transformation; 7. import android.widget.Gallery; 8. import android.widget.ImageView; 9. public class CoverFlow extends Gallery { 10. 11. private Camera mCamera = new Camera(); 12. private int mMaxRotationAngle = 50; 13. private int mMaxZoom = -380; 14. private int mCoveflowCenter; 15. private boolean mAlphaMode = true; 16. private boolean mCircleMode = false; 17. public CoverFlow(Context context) { 18. super(context); 19. this.setStaticTransformationsEnabled(true); 20. } 21. public CoverFlow(Context context, AttributeSet attrs) { 22. super(context, attrs); 23. this.setStaticTransformationsEnabled(true); 24. } 25. public CoverFlow(Context context, AttributeSet attrs, int defStyle) { 26. super(context, attrs, defStyle); 27. this.setStaticTransformationsEnabled(true); 28. } 29. public int getMaxRotationAngle() { 30. return mMaxRotationAngle; 31. } 32. public void setMaxRotationAngle(int maxRotationAngle) { 33. mMaxRotationAngle = maxRotationAngle; 34. } 35. public boolean getCircleMode() { 36. return mCircleMode; 37. } 38. public void setCircleMode(boolean isCircle) { 39. mCircleMode = isCircle; 40. } 41. public boolean getAlphaMode() { 42. return mAlphaMode; 43. } 44. public void setAlphaMode(boolean isAlpha) { 45. mAlphaMode = isAlpha; 46. } 47. public int getMaxZoom() { 48. return mMaxZoom; 49. } 50. public void setMaxZoom(int maxZoom) { 51. mMaxZoom = maxZoom; 52. } 53. private int getCenterOfCoverflow() { 54. return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 55. + getPaddingLeft(); 56. } 57. private static int getCenterOfView(View view) { 58. return view.getLeft() + view.getWidth() / 2; 59. } 60. protected boolean getChildStaticTransformation(View child, Transformation t) { 61. final int childCenter = getCenterOfView(child); 62. final int childWidth = child.getWidth(); 63. int rotationAngle = 0; 64. t.clear(); 65. t.setTransformationType(Transformation.TYPE_MATRIX); 66. if (childCenter == mCoveflowCenter) { 67. transformImageBitmap((ImageView) child, t, 0); 68. } else { 69. rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle); 70. if (Math.abs(rotationAngle) > mMaxRotationAngle) { 71. rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle 72. : mMaxRotationAngle; 73. } 74. transformImageBitmap((ImageView) child, t, rotationAngle); 75. } 76. return true; 77. } 78. /** 79. * This is called during layout when the size of this view has changed. If 80. * you were just added to the view hierarchy, you're called with the old 81. * values of 0. 82. * 83. * @param w 84. * Current width of this view. 85. * @param h 86. * Current height of this view. 87. * @param oldw 88. * Old width of this view. 89. * @param oldh 90. * Old height of this view. 91. */ 92. protected void onSizeChanged(int w, int h, int oldw, int oldh) { 93. mCoveflowCenter = getCenterOfCoverflow(); 94. super.onSizeChanged(w, h, oldw, oldh); 95. } 96. /** 97. * Transform the Image Bitmap by the Angle passed 98. * 99. * @param imageView 100. * ImageView the ImageView whose bitmap we want to rotate 101. * @param t 102. * transformation 103. * @param rotationAngle 104. * the Angle by which to rotate the Bitmap 105. */ 106. private void transformImageBitmap(ImageView child, Transformation t, 107. int rotationAngle) { 108. mCamera.save(); 109. final Matrix imageMatrix = t.getMatrix(); 110. final int imageHeight = child.getLayoutParams().height; 111. final int imageWidth = child.getLayoutParams().width; 112. final int rotation = Math.abs(rotationAngle); 113. mCamera.translate(0.0f, 0.0f, 100.0f); 114. // As the angle of the view gets less, zoom in 115. if (rotation <= mMaxRotationAngle) { 116. float zoomAmount = (float) (mMaxZoom + (rotation * 1.5)); 117. mCamera.translate(0.0f, 0.0f, zoomAmount); 118. if (mCircleMode) { 119. if (rotation < 40) 120. mCamera.translate(0.0f, 155, 0.0f); 121. else 122. mCamera.translate(0.0f, (255 - rotation * 2.5f), 0.0f); 123. } 124. if (mAlphaMode) { 125. ((ImageView) (child)).setAlpha((int) (255 - rotation * 2.5)); 126. } 127. } 128. mCamera.rotateY(rotationAngle); 129. mCamera.getMatrix(imageMatrix); 130. imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2)); 131. imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2)); 132. mCamera.restore(); 133. } 134. } 135. 136. 137. <pre class="java" name="code">import android.app.Activity; 138. import android.content.Context; 139. import android.graphics.drawable.BitmapDrawable; 140. import android.os.Bundle; 141. import android.view.View; 142. import android.view.ViewGroup; 143. import android.widget.BaseAdapter; 144. import android.widget.ImageView; 145. 146. import com.mmb.shop.R; 147. import com.mmb.shop.view.CoverFlow; 148. 149. public class TestActivity extends Activity { 150. @Override 151. public void onCreate(Bundle savedInstanceState) { 152. super.onCreate(savedInstanceState); 153. 154. CoverFlow cf = new CoverFlow(this); 155. //cf.setBackgroundResource(R.drawable.icon);//// 156. cf.setAdapter(new ImageAdapter(this)); 157. ImageAdapter imageAdapter = new ImageAdapter(this); 158. cf.setAdapter(imageAdapter); 159. // cf.setAlphaMode(false); 160. // cf.setCircleMode(false); 161. // cf.setSelection(3, true); 162. cf.setAnimationDuration(1000); 163. setContentView(cf); 164. } 165. 166. 167. 168. public class ImageAdapter extends BaseAdapter { 169. private Context mContext; 170. private Integer[] mImageIds = { 171. R.drawable.a, 172. R.drawable.b, 173. R.drawable.c, 174. R.drawable.d, 175. R.drawable.e, 176. R.drawable.f, 177. R.drawable.g 178. }; 179. 180. public ImageAdapter(Context c) { 181. mContext = c; 182. } 183. public int getCount() { 184. return mImageIds.length; 185. } 186. public Object getItem(int position) { 187. return position; 188. } 189. public long getItemId(int position) { 190. return position; 191. } 192. public View getView(int position, View convertView, ViewGroup parent) { 193. //ReflectionImage image = new ReflectionImage(mContext); 194. ImageView image = new ImageView(mContext); 195. image.setImageResource(mImageIds[position]); 196. image.setLayoutParams(new CoverFlow.LayoutParams(96, 76)); 197. image.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 198. // Make sure we set anti-aliasing otherwise we get jaggies 199. BitmapDrawable drawable = (BitmapDrawable) image.getDrawable(); 200. drawable.setAntiAlias(true); 201. return image; 202. } 203. 204. public float getScale(boolean focused, int offset) { 205. return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset))); 206. } 207. } 208. 209. 210. }
ReflectionImage
1. import android.content.Context; 2. import android.graphics.Bitmap; 3. import android.graphics.BitmapFactory; 4. import android.graphics.Canvas; 5. import android.graphics.LinearGradient; 6. import android.graphics.Matrix; 7. import android.graphics.Paint; 8. import android.graphics.PorterDuffXfermode; 9. import android.graphics.Bitmap.Config; 10. import android.graphics.PorterDuff.Mode; 11. import android.graphics.Shader.TileMode; 12. import android.graphics.drawable.BitmapDrawable; 13. import android.util.AttributeSet; 14. import android.widget.ImageView; 15. /** 16. * 17. * @author 18. */ 19. public class ReflectionImage extends ImageView { 20. //是否为Reflection模式 21. private boolean mReflectionMode = true; 22. public ReflectionImage(Context context) { 23. super(context); 24. } 25. public ReflectionImage(Context context, AttributeSet attrs) { 26. super(context, attrs); 27. //取得原始图片的bitmap并重画 28. Bitmap originalImage = ((BitmapDrawable)this.getDrawable()).getBitmap(); 29. doReflection(originalImage); 30. } 31. public ReflectionImage(Context context, AttributeSet attrs, 32. int defStyle) { 33. super(context, attrs, defStyle); 34. Bitmap originalImage = ((BitmapDrawable)this.getDrawable()).getBitmap(); 35. doReflection(originalImage); 36. } 37. public void setReflectionMode(boolean isRef) { 38. mReflectionMode = isRef; 39. } 40. public boolean getReflectionMode() { 41. return mReflectionMode; 42. } 43. //偷懒了,只重写了setImageResource,和构造函数里面干了同样的事情 44. @Override 45. public void setImageResource(int resId) { 46. Bitmap originalImage = BitmapFactory.decodeResource( 47. getResources(), resId); 48. doReflection(originalImage); 49. //super.setImageResource(resId); 50. } 51. private void doReflection(Bitmap originalImage) { 52. final int reflectionGap = 4; //原始图片和反射图片中间的间距 53. int width = originalImage.getWidth(); 54. int height = originalImage.getHeight(); 55. 56. //反转 57. Matrix matrix = new Matrix(); 58. matrix.preScale(1, -1); 59. //reflectionImage就是下面透明的那部分,可以设置它的高度为原始的3/4,这样效果会更好些 60. Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, 61. 0, width, height, matrix, false); 62. //创建一个新的bitmap,高度为原来的两倍 63. Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height), Config.ARGB_8888); 64. Canvas canvasRef = new Canvas(bitmapWithReflection); 65. 66. //先画原始的图片 67. canvasRef.drawBitmap(originalImage, 0, 0, null); 68. //画间距 69. Paint deafaultPaint = new Paint(); 70. canvasRef.drawRect(0, height, width, height + reflectionGap, deafaultPaint); 71. 72. //画被反转以后的图片 73. canvasRef.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 74. // 创建一个渐变的蒙版放在下面被反转的图片上面 75. Paint paint = new Paint(); 76. LinearGradient shader = new LinearGradient(0, 77. originalImage.getHeight(), 0, bitmapWithReflection.getHeight() 78. + reflectionGap, 0x80ffffff, 0x00ffffff, TileMode.CLAMP); 79. // Set the paint to use this shader (linear gradient) 80. paint.setShader(shader); 81. // Set the Transfer mode to be porter duff and destination in 82. paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 83. // Draw a rectangle using the paint with our linear gradient 84. canvasRef.drawRect(0, height, width, bitmapWithReflection.getHeight() 85. + reflectionGap, paint); 86. //调用ImageView中的setImageBitmap 87. this.setImageBitmap(bitmapWithReflection); 88. } 89. }