首页 > 代码库 > ScaleGestureDetector缩放view
ScaleGestureDetector缩放view
public class ScaleGesture implements OnScaleGestureListener {
private float beforeFactor;
private float mPivotX;
private float mPivotY;
private View mVSouce;
private boolean isFillAfter;
public void setSourceView(View destinyView) {
mVSouce = destinyView;
}
@Override
public boolean onScale(ScaleGestureDetector detector) {
if (checkIsNull()) {
return false;
}
final float factor = detector.getScaleFactor();
Animation animation = new ScaleAnimation(beforeFactor, factor,
beforeFactor, factor, mPivotX, mPivotY);
animation.setFillAfter(true);
mVSouce.startAnimation(animation);
beforeFactor = factor;
return false;
}
@Override
public boolean onScaleBegin(ScaleGestureDetector detector) {
if (checkIsNull()) {
return false;
}
beforeFactor = 1f;
mPivotX = detector.getFocusX() - mVSouce.getLeft();
mPivotY = mVSouce.getTop() + (mVSouce.getHeight() >> 1);
return true;
}
@Override
public void onScaleEnd(ScaleGestureDetector detector) {
if (checkIsNull()) {
return;
}
final float factor = detector.getScaleFactor();
final int nWidth = (int) (mVSouce.getWidth() * factor);
final int nHeight = (int) mVSouce.getHeight();
final int nLeft = (int) (mVSouce.getLeft() - ((nWidth - mVSouce
.getWidth()) * (mPivotX / mVSouce.getWidth())));
final int nTop = (int) mVSouce.getTop();
if (isFillAfter) {
mVSouce.layout(nLeft, nTop, nLeft + nWidth, nTop + nHeight);
}
// MUST BE CLEAR ANIMATION. OTHERWISE WILL BE FLICKER
// if can not clear animation the layout will keep the size
// mVSouce.clearAnimation();
}
public boolean checkIsNull() {
return mVSouce == null ? true : false;
}
/**
* if parameter is true that keeping same scale when next scaling.
*
* @param isFill
*/
public void setFillAfter(boolean isFill) {
isFillAfter = isFill;
}
}
/*在activity里面定义变量*/
ScaleGesture sg = new ScaleGesture();
ScaleGestureDetector detector;
/*在oncreat方法里面*/
detector = new ScaleGestureDetector(你要缩放的view.getContext(), sg);
sg.setSourceView(你要缩放的view);
/*可以实现缩放,但移动view实现的不好,可以使用scrollby实现移动查看*/
ScaleGestureDetector缩放view