zoukankan      html  css  js  c++  java
  • 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实现移动查看*/

  • 相关阅读:
    bash 中 () {} [] [[]] (()) 的解释
    正则表达式速查笔记
    Makefile速查笔记
    gflags 编译动态库
    在Win10上运行ESXI-Comstomer
    GNU g++常用编译选项用法
    C++标准转换运算符reinterpret_cast
    BZOJ 3211: 花神游历各国【线段树区间开方问题】
    BZOJ 1597: [Usaco2008 Mar]土地购买【斜率优化+凸包维护】
    BZOJ 1046: [HAOI2007]上升序列【贪心+二分状态+dp+递归】
  • 原文地址:https://www.cnblogs.com/xgjblog/p/4050086.html
Copyright © 2011-2022 走看看