首先贴Gallery仿3D的7源码:
1package cn.pyz.mypro.define; 2 3 4import android.content.Context; 5import android.graphics.Camera; 6import android.graphics.Matrix; 7import android.util.AttributeSet; 8import android.util.Log; 9import android.view.View; 10import android.view.animation.Transformation; 11import android.widget.Gallery; 12 13public class CoverFlow extends Gallery { 14 15private Camera mCamera = new Camera(); 16private int mMaxRotationAngle = 45; //60 17private int mMaxZoom = -150; //-120 18private int mCoveflowCenter; 19private boolean mAlphaMode = true; 20private boolean mCircleMode = false; 21 22 23public CoverFlow(Context context) { 24super(context); 25this.setStaticTransformationsEnabled(true); 26} 27 28public CoverFlow(Context context, AttributeSet attrs) { 29super(context, attrs); 30this.setStaticTransformationsEnabled(true); 31} 32 33public CoverFlow(Context context, AttributeSet attrs, int defStyle) { 34super(context, attrs, defStyle); 35this.setStaticTransformationsEnabled(true); 36} 37 38public int getMaxRotationAngle() { 39return mMaxRotationAngle; 40} 41 42public void setMaxRotationAngle(int maxRotationAngle) { 43mMaxRotationAngle = maxRotationAngle; 44} 45 46public boolean getCircleMode() { 47return mCircleMode; 48} 49 50 51public void setCircleMode(boolean isCircle) { 52mCircleMode = isCircle; 53} 54 55 56public boolean getAlphaMode() { 57return mAlphaMode; 58} 59 60 61public void setAlphaMode(boolean isAlpha) { 62mAlphaMode = isAlpha; 63} 64 65 66public int getMaxZoom() { 67return mMaxZoom; 68} 69 70 71public void setMaxZoom(int maxZoom) { 72mMaxZoom = maxZoom; 73} 74 75 76private int getCenterOfCoverflow() { 77return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 78+ getPaddingLeft(); 79} 80 81 82private static int getCenterOfView(View view) { 83System.out.println("-------%1---view.getLeft--------"+view.getLeft()); 84System.out.println("-------%2---view.getWith--------"+view.getWidth()); 85return view.getLeft() + view.getWidth() / 2; 86} 87 88 89@Override 90protected boolean getChildStaticTransformation(View child, Transformation t) { 91final int childCenter = getCenterOfView(child); 92final int childWidth = child.getWidth(); 93int rotationAngle = 0; 94t.clear(); 95t.setTransformationType(Transformation.TYPE_MATRIX); 96if (childCenter == mCoveflowCenter) { 97transformImageBitmap((View) child, t, 0, 0); 98} else { 99rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle); 100// Log.d("test", "recanglenum:"+Math.floor ((mCoveflowCenter - 101// childCenter) / childWidth)); 102if (Math.abs(rotationAngle) > mMaxRotationAngle) { 103rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle 104: mMaxRotationAngle; 105} 106transformImageBitmap((View) child, t, rotationAngle, 107(int) Math.floor((mCoveflowCenter - childCenter)/ (childWidth==0?1:childWidth))); 108} 109return true; 110} 111 112 113/** 114* This is called during layout when the size of this view has changed. If 115* you were just added to the view hierarchy, you're called with the old 116* values of 0. 117* 118* @param w 119* Current width of this view. 120* @param h 121* Current height of this view. 122* @param oldw 123* Old width of this view. 124* @param oldh 125* Old height of this view. 126*/ 127protected void onSizeChanged(int w, int h, int oldw, int oldh) { 128mCoveflowCenter = getCenterOfCoverflow(); 129super.onSizeChanged(w, h, oldw, oldh); 130} 131 132 133/** 134* Transform the Image Bitmap by the Angle passed 135* 136* @param imageView 137* ImageView the ImageView whose bitmap we want to rotate 138* @param t 139* transformation 140* @param rotationAngle 141* the Angle by which to rotate the Bitmap 142*/ 143private void transformImageBitmap(View child, Transformation t, 144int rotationAngle, int d) { 145mCamera.save(); 146final Matrix imageMatrix = t.getMatrix(); 147final int imageHeight = child.getLayoutParams().height; 148final int imageWidth = child.getLayoutParams().width; 149final int rotation = Math.abs(rotationAngle); 150mCamera.translate(0.0f, -5.0f, 100.0f); // 151// As the angle of the view gets less, zoom in 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167if (rotation <= mMaxRotationAngle) 168 169 170 171 172 173 174 175 176 177 178 179 180 181{ 182 183 184 185 186 187 188 189float zoomAmount = (float) (mMaxZoom + (rotation * 10f)); 190mCamera.translate(0.0f, -5.0f, zoomAmount); // 191if (mCircleMode) { 192if (rotation < 40) 193mCamera.translate(0.0f, 155, 5f); 194else 195mCamera.translate(0.0f, (255 - rotation * 10f), 10f); 196} 197// if (mAlphaMode) { 198// ((ImageView) (child)).setAlpha((int) (255 - rotation * 2.5)); 199// } 200} 201mCamera.rotateY(rotationAngle); 202mCamera.getMatrix(imageMatrix); 203 204 205imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2)); 206imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2)); 207mCamera.restore(); 208} 209}
这段代码在Android 4.1一下的版本中都是可以正常运行3D效果的,效果很炫,但是在Android4.1的版本上当你滑动的时候就会出现图片显示大小不一,错位等情况,光这个问题研究了一周,最后终于自己研究出来了,其实就是和硬件加速有关系,Android系统中会对2D和一些3D的应用进行硬件加速,因为这些都是非常好资源的,所以解决的办法就是在你的注册文件中添加 android:hardwareAccelerated="false",这句话是让硬件加速在你不需要的Activity中对它进行屏蔽,这样在Android4.1系统中就能正常显示Gallery3D效果啦