//package com.android.item.util;
import android.content.Context;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;
/**
* 实现鱼眼效果的Gallery
* @author ZhangZy
*
*/
public class FisheyeEffectsGallery extends Gallery
{
public FisheyeEffectsGallery(Context context)
{
super(context);
}
public FisheyeEffectsGallery(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public FisheyeEffectsGallery(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
/*
* (译者注: setStaticTransformationsEnabled这个属性设成 true的时候每次 viewGroup(看
* Gallery的源码就可以看到它是从 ViewGroup间接继承过来的 )在重新画它的 child的时候都会促发
* getChildStaticTransformation这个函数
*/
@Override
protected boolean getChildStaticTransformation(View child, Transformation t)
{
t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);
Matrix matrix = t.getMatrix();
float px=child.getWidth() / 2;
float py=child.getHeight() / 2;
float sx = (getWidth()-Math.abs(getCenterOfCoverflow()-getCenterOfView(child)))*1f/getWidth();
// Log.w(getClass().getSimpleName(), "sx="+sx+",Coverflow="+getCenterOfCoverflow()+",child="+getCenterOfView(child)+",getWidth()="+getWidth());
//缩放比例的限制
sx = sx<0.8f?0.8f:sx;
float sy = sx;
matrix.postScale(sx, sy, px,py );
// return super.getChildStaticTransformation(child, t);
return true;
}
/* Gallery的中心点 */
private int getCenterOfCoverflow()
{
return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
}
/* view的中心点 */
private static int getCenterOfView(View view)
{
return view.getLeft() + view.getWidth() / 2;
}
}
|