阅读更多
package com.putao.game.majiang.util;
import com.putao.game.majiang.data.AppData;
import com.putao.game.majiang.data.User;
import android.content.Context;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.Gallery;
/**
* 类功能描述:
*
* @author 兰唯
* @version 1.0 修改时间: 修改备注:
*/
public class CustomGallery extends Gallery {
public Camera mCamera = new Camera();
Matrix mMatrix = new Matrix();
public CustomGallery(Context context) {
super(context);
setStaticTransformationsEnabled(true);
}
public CustomGallery(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomGallery(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
final float offset = calculateOffsetOfCenter(child);
transformViewRoom(child, t, offset);
return true;
}
protected int getCenterOfCoverflow() {
return ((getWidth() - getPaddingLeft() - getPaddingRight()) >> 1) + getPaddingLeft();
}
// 获取 child 中心点 X 的位置
protected int getCenterOfView(View view) {
return view.getLeft() + (view.getWidth() >> 1);
}
// 计算 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, 0.3f);
offset = Math.max(offset, -0.3f);
return offset;
}
void transformViewRoom(View child, Transformation t, float race) {
User userInfo = AppData.getInstance().getUser();
Camera mCamera = new Camera();
mCamera.save();
final Matrix matrix = t.getMatrix();
final int halfHeight = child.getMeasuredHeight() >> 1;
final int halfWidth = child.getMeasuredWidth() >> 1;
// 平移 X、Y、Z 轴已达到立体效果
mCamera.translate(-race * 50, 0.0f, Math.abs(race) * 200);
// 也可设置旋转效果
mCamera.getMatrix(matrix);
// 获取当前屏幕的宽高来缩小当前的item
if (userInfo.getWidth() == 800 && userInfo.getHeight() == 480) {
matrix.postScale(0.9f, 0.9f);
}
// 以 child 的中心点变换
matrix.preTranslate(-halfWidth, -halfHeight);
matrix.postTranslate(halfWidth, halfHeight);
mCamera.restore();
// 设置 alpha 变换
t.setAlpha(1 - Math.abs(race));
}
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
if (android.os.Build.VERSION.SDK_INT > 15) {
if (child.getAnimation() == null) {
TransformationAnimation ta = new TransformationAnimation(child);
child.setAnimation(ta);
}
}
return super.drawChild(canvas, child, drawingTime);
}
final class TransformationAnimation extends Animation {
View v;
TransformationAnimation(View _v) {
v = _v;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
getChildStaticTransformation(v, t);
}
}
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
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
/**
* return false; 只可以滑动一张图片
* */
return super.onFling(e1, e2, velocityX, velocityY);
}
}
[size=x-small][/size]