CoverFlow
View Code
1 package com.gallery.coverflow; 2 3 import android.content.Context; 4 import android.graphics.Bitmap; 5 import android.graphics.Bitmap.Config; 6 import android.graphics.BitmapFactory; 7 import android.graphics.Canvas; 8 import android.graphics.LinearGradient; 9 import android.graphics.Matrix; 10 import android.graphics.Paint; 11 import android.graphics.PorterDuff.Mode; 12 import android.graphics.PorterDuffXfermode; 13 import android.graphics.Shader.TileMode; 14 import android.graphics.drawable.BitmapDrawable; 15 import android.view.View; 16 import android.view.ViewGroup; 17 import android.widget.BaseAdapter; 18 import android.widget.ImageView; 19 20 import com.gallery.R; 21 22 /** 23 * @author junxu.wang 24 * 25 */ 26 public class ImageAdapter extends BaseAdapter { 27 28 int mGalleryItemBackground; 29 30 private Context mContext; 31 32 private Bitmap[]bitmaps; 33 34 private Integer[] mImageIds = { 35 36 R.drawable.img0001, 37 38 R.drawable.img0030, 39 40 R.drawable.img0100, 41 42 R.drawable.img0130, 43 44 R.drawable.img0200, 45 46 R.drawable.img0230, 47 48 R.drawable.img0300, 49 50 R.drawable.img0330, 51 52 R.drawable.img0354 }; 53 54 public ImageAdapter(Context c) { 55 56 mContext = c; 57 bitmaps=new Bitmap[mImageIds.length]; 58 59 } 60 61 public int getCount() { 62 63 return mImageIds.length; 64 65 } 66 67 public Object getItem(int position) { 68 69 return position; 70 71 } 72 73 public long getItemId(int position) { 74 75 return position; 76 77 } 78 79 public View getView(int position, View convertView, ViewGroup parent) { 80 if(convertView==null){ 81 ImageView imageView = new ImageView(mContext); 82 imageView.setLayoutParams(new CoverFlow.LayoutParams(120, 100)); 83 imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 84 convertView=imageView; 85 } 86 87 if(bitmaps[position]==null){ 88 bitmaps[position]=createReflectedImages(mContext, mImageIds[position]); 89 } 90 ((ImageView) convertView).setImageBitmap(bitmaps[position]); 91 BitmapDrawable drawable = (BitmapDrawable) ((ImageView) convertView).getDrawable(); 92 drawable.setAntiAlias(true); 93 94 return convertView; 95 96 } 97 98 public float getScale(boolean focused, int offset) { 99 100 return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset))); 101 102 } 103 104 /**@param mContext 105 * @param imageId 106 * @return 107 */ 108 public Bitmap createReflectedImages(Context mContext, int imageId) { 109 110 Bitmap originalImage = BitmapFactory.decodeResource( mContext.getResources(), imageId); 111 112 final int reflectionGap = 4; 113 114 int width = originalImage.getWidth(); 115 116 int height = originalImage.getHeight(); 117 118 Matrix matrix = new Matrix(); 119 120 matrix.preScale(1, -1); 121 122 Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false); 123 124 Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); 125 126 Canvas canvas = new Canvas(bitmapWithReflection); 127 canvas.drawBitmap(originalImage, 0, 0, null); 128 129 Paint deafaultPaint = new Paint(); 130 131 canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint); 132 canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); 133 134 Paint paint = new Paint(); 135 136 LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() 137 138 + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.MIRROR); 139 140 paint.setShader(shader); 141 paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 142 143 canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); 144 145 return bitmapWithReflection; 146 147 } 148 149 }
CoverFlow
1 package com.gallery.coverflow; 2 3 import android.content.Context; 4 5 import android.graphics.Camera; 6 7 import android.graphics.Matrix; 8 9 import android.util.AttributeSet; 10 11 import android.view.View; 12 13 import android.view.animation.Transformation; 14 15 import android.widget.Gallery; 16 17 import android.widget.ImageView; 18 19 /** 20 * @author junxu.wang 21 * 22 */ 23 public class CoverFlow extends Gallery { 24 private Camera mCamera = new Camera(); 25 26 private int mMaxRotationAngle = 50; 27 28 private int mMaxZoom = -380; 29 30 private int mCoveflowCenter; 31 32 private boolean mAlphaMode = true; 33 34 private boolean mCircleMode = false; 35 36 public CoverFlow(Context context) { 37 38 super(context); 39 40 this.setStaticTransformationsEnabled(true); 41 42 } 43 44 public CoverFlow(Context context, AttributeSet attrs) { 45 46 super(context, attrs); 47 48 this.setStaticTransformationsEnabled(true); 49 50 } 51 52 public CoverFlow(Context context, AttributeSet attrs, int defStyle) { 53 54 super(context, attrs, defStyle); 55 56 this.setStaticTransformationsEnabled(true); 57 58 } 59 60 public int getMaxRotationAngle() { 61 62 return mMaxRotationAngle; 63 64 } 65 66 public void setMaxRotationAngle(int maxRotationAngle) { 67 68 mMaxRotationAngle = maxRotationAngle; 69 70 } 71 72 public boolean getCircleMode() { 73 74 return mCircleMode; 75 76 } 77 78 public void setCircleMode(boolean isCircle) { 79 80 mCircleMode = isCircle; 81 82 } 83 84 public boolean getAlphaMode() { 85 86 return mAlphaMode; 87 88 } 89 90 public void setAlphaMode(boolean isAlpha) { 91 92 mAlphaMode = isAlpha; 93 94 } 95 96 public int getMaxZoom() { 97 98 return mMaxZoom; 99 100 } 101 102 public void setMaxZoom(int maxZoom) { 103 104 mMaxZoom = maxZoom; 105 106 } 107 108 private int getCenterOfCoverflow() { 109 110 // return (getWidth() - getPaddingLeft() - getPaddingRight())/2 + 111 // getPaddingLeft(); 112 return (getWidth() + getPaddingLeft() - getPaddingRight()) >> 1; 113 114 } 115 116 private static int getCenterOfView(View view) { 117 118 return view.getLeft() + view.getWidth() / 2; 119 120 } 121 122 /** 123 * 124 * 125 * @param w 126 * Current width of this view. 127 * @param h 128 * Current height of this view. 129 * 130 * @param oldw 131 * Old width of this view. 132 * @param oldh 133 * Old height of this view. 134 */ 135 136 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 137 138 mCoveflowCenter = getCenterOfCoverflow(); 139 140 super.onSizeChanged(w, h, oldw, oldh); 141 142 } 143 144 /** 145 * (non-Javadoc) 146 * @see android.widget.Gallery#getChildStaticTransformation(android.view.View, android.view.animation.Transformation) 147 */ 148 protected boolean getChildStaticTransformation(View child, Transformation t) { 149 150 final int childCenter = getCenterOfView(child); 151 152 final int childWidth = child.getWidth(); 153 154 int rotationAngle = 0; 155 156 t.clear(); 157 158 t.setTransformationType(Transformation.TYPE_MATRIX 159 if (childCenter == mCoveflowCenter) { transformImageBitmap((ImageView) child, t, 0); 160 161 } else { 162 163 rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle); 164 165 if (Math.abs(rotationAngle) > mMaxRotationAngle) { 166 167 rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle; 168 169 } 170 171 transformImageBitmap((ImageView) child, t, rotationAngle); 172 173 } 174 175 return true; 176 177 } 178 179 /** 180 * @param imageView 181 * ImageView the ImageView whose bitmap we want to rotate 182 * @param t 183 * transformation 184 * @param rotationAngle 185 * the Angle by which to rotate the Bitmap 186 */ 187 private void transformImageBitmap(ImageView child, Transformation t, int rotationAngle) { 188 189 mCamera.save(); 190 191 final Matrix imageMatrix = t.getMatrix(); 192 193 final int imageHeight = child.getLayoutParams().height; 194 195 final int imageWidth = child.getLayoutParams().width; 196 197 final int rotation = Math.abs(rotationAngle); 198 199 mCamera.translate(0.0f, 0.0f, 100.0f); 200 201 if (rotation <= mMaxRotationAngle) { 202 203 float zoomAmount = (float) (mMaxZoom + (rotation * 1.5)); 204 205 mCamera.translate(0.0f, 0.0f, zoomAmount); 206 207 if (mCircleMode) { 208 209 if (rotation < 40) 210 mCamera.translate(0.0f, 155, 0.0f); 211 else 212 mCamera.translate(0.0f, (255 - rotation * 2.5f), 0.0f); 213 214 } 215 216 if (mAlphaMode) { 217 ((ImageView) (child)).setAlpha((int) (255 - rotation * 2.5)); 218 } 219 220 } 221 222 mCamera.rotateY(rotationAngle); 223 224 mCamera.getMatrix(imageMatrix); 225 226 imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2)); 227 228 imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2)); 229 230 mCamera.restore(); 231 232 } 233 234 }
MainActivity
1 package com.gallery.coverflow; 2 3 import android.app.Activity; 4 5 import android.graphics.Color; 6 7 import android.os.Bundle; 8 9 10 import com.gallery.R; 11 12 13 public class MainActivity extends Activity { 14 15 /** Called when the activity is first created. */ 16 17 @Override 18 19 public void onCreate(Bundle savedInstanceState) { 20 21 super.onCreate(savedInstanceState); 22 23 CoverFlow cf = new CoverFlow(this); 24 25 // cf.setBackgroundResource(R.drawable.shape); 26 27 cf.setBackgroundColor(Color.BLACK); 28 29 cf.setAdapter(new ImageAdapter(this)); 30 31 ImageAdapter imageAdapter = new ImageAdapter(this); 32 33 cf.setAdapter(imageAdapter); 34 35 // cf.setAlphaMode(false); 36 37 // cf.setCircleMode(false); 38 39 cf.setSelection(2, true); 40 41 cf.setAnimationDuration(1000); 42 43 setContentView(cf); 44 45 } 46 47 }


浙公网安备 33010602011771号