为避免每次都要去找,还是自己整理一下好了。有需要的同学自行下载源码,只要稍微有点基本功的同学估计都看得懂!至于新手而言,能拿来用就好了,我们并不关心“手机”是如何生产出来的,只要知道手机怎么使用就好。。。
Gallery修改类:
/**
* The camera class is used to 3D transformation matrix.
*/
private Camera mCamera = new Camera();
private Matrix mMatrix = new Matrix();
/**
* The max rotation angle.
*/
private int mMaxRotationAngle = 60;
/**
* The max zoom value (Z axis).
*/
private int mMaxZoom = -120;
/**
* The center of the gallery.
*/
private int mCoveflowCenter = 0;
public GalleryFlow(Context context) {
this(context, null);
// this.setStaticTransformationsEnabled(true);
// Enable set the children drawing order.
this.setChildrenDrawingOrderEnabled(true);
}
public GalleryFlow(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// this.setStaticTransformationsEnabled(true);
// Enable set the children drawing order.
this.setChildrenDrawingOrderEnabled(true);
}
public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// Enable set transformation.
// this.setStaticTransformationsEnabled(true);
// Enable set the children drawing order.
this.setChildrenDrawingOrderEnabled(true);
}
public int getMaxRotationAngle() {
return mMaxRotationAngle;
}
public void setMaxRotationAngle(int maxRotationAngle) {
mMaxRotationAngle = maxRotationAngle;
}
public int getMaxZoom() {
return mMaxZoom;
}
public void setMaxZoom(int maxZoom) {
mMaxZoom = maxZoom;
}
@Override
protected int getChildDrawingOrder(int childCount, int i) {
// Current selected index.
int selectedIndex = getSelectedItemPosition()
- getFirstVisiblePosition();
if (selectedIndex < 0) {
return i;
}
if (i < selectedIndex) {
return i;
} else if (i >= selectedIndex) {
return childCount - 1 - i + selectedIndex;
} else {
return i;
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mCoveflowCenter = getCenterOfCoverflow();
super.onSizeChanged(w, h, oldw, oldh);
}
// 获得Coverflow的中点(应该是图片流视图的中点)
private int getCenterOfCoverflow() {
return ((getWidth() - getPaddingLeft() - getPaddingRight()) >> 1)
+ getPaddingLeft();
// return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 +
// getPaddingLeft();
}
// 获得子view的中点
private int getCenterOfView(View view) {
return view.getLeft() + view.getWidth() / 2;
}
// 计算 child 偏离 父控件中心的 offset 值, -1 <= offset <= 1
protected float calculateOffsetOfCenter(View view) {
final int pCenter = getCenterOfCoverflow();
final int cCenter = getCenterOfView(view);
float offset = (cCenter - pCenter) / (pCenter * 1.0f);
offset = Math.min(offset, 1.0f);
offset = Math.max(offset, -1.0f);
return offset;
}
// @Override
// public void setUnselectedAlpha(float unselectedAlpha) {
// unselectedAlpha=0xff;
// super.setUnselectedAlpha(unselectedAlpha);
// }
private void transformImageBitmap(View child, Matrix mt, int rotationAngle) {
mCamera.save();
final Matrix imageMatrix = mt;
final int halfWidth = child.getLeft() + (child.getMeasuredWidth() >> 1);
final int halfHeight = child.getMeasuredHeight() >> 1;
final int rotation = Math.abs(rotationAngle);
// Zoom on Z axis.
mCamera.translate(0, 0, mMaxZoom);
if (rotation < mMaxRotationAngle) {
float zoomAmount = (float) (mMaxZoom + rotation * 1.5f);
mCamera.translate(0, 0, zoomAmount);
}
// Rotate the camera on Y axis.
mCamera.rotateY(rotationAngle);
// Get the matrix from the camera, in fact, the matrix is S (scale)
// transformation.
mCamera.getMatrix(imageMatrix);
// The matrix final is T2 * S * T1, first translate the center point to
// (0, 0),
// then scale, and then translate the center point to its original
// point.
// T * S * T
mMatrix.preTranslate(-halfWidth, -halfHeight - 15); // 还没清楚为什么要减去15
mMatrix.postTranslate(halfWidth, halfHeight - 15);
mCamera.restore();
}
void getTransformationMatrix(View child, float offset) {
final int halfWidth = child.getLeft() + (child.getMeasuredWidth() >> 1);
final int halfHeight = child.getMeasuredHeight() >> 1;
mCamera.save();
mCamera.translate(-offset * 50, 0.0f, Math.abs(offset) * 200);
mCamera.getMatrix(mMatrix);
mCamera.restore();
mMatrix.preTranslate(-halfWidth, -halfHeight);
mMatrix.postTranslate(halfWidth, halfHeight);
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
// TODO Auto-generated method stub
boolean ret;
// Android SDK 4.1
if (android.os.Build.VERSION.SDK_INT > 15) {
getChildStaticTrans(child, mMatrix);
// final float offset = calculateOffsetOfCenter(child);
// getTransformationMatrix(child, offset);
// child.setAlpha(1 - Math.abs(offset));
//
final int saveCount = canvas.save();
canvas.concat(mMatrix);
ret = super.drawChild(canvas, child, drawingTime);
canvas.restoreToCount(saveCount);
} else {
ret = super.drawChild(canvas, child, drawingTime);
}
return ret;
}
protected boolean getChildStaticTrans(View child, Matrix mt) {
final int childCenter = getCenterOfView(child);
final int childWidth = child.getWidth();
int rotationAngle = 0;
if (childCenter == mCoveflowCenter) {
transformImageBitmap(child, mt, 0);
} else {
// Calculate the rotation angle.
rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
// Make the angle is not bigger than maximum.
if (Math.abs(rotationAngle) > mMaxRotationAngle) {
rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle
: mMaxRotationAngle;
}
transformImageBitmap(child, mt, rotationAngle);
}
return true;
}
使用代码片段
/**
* 三步骤
*/
private void initPBListCtrl() {
mGallery = (GalleryFlow) findViewById(R.id.gallery_phonebook);
mGallery.setSpacing(3);// 设置画间间隔
mGallery.setAnimationDuration(2000);
mGallery.setGravity(Gravity.CENTER_VERTICAL);
mGallery.setOnItemClickListener(this);
mGallery.setOnItemSelectedListener(this);
mAdapter = new MyAdapter(this, mAllContactsList);
mGallery.setAdapter(mAdapter);
}