Android - 捏缩放手势放大按钮

时间:2013-06-25 14:47:51

标签: android android-ui

查看指南和示例给出了关于如何执行此操作的明确线索:

我正在尝试在Button上使用自定义背景(圆形Drawable)启用此类手势,以便在一定范围内增加或减少其大小。我正在尝试这个,但在继续之前,我想知道这是否是正确的方法,如果有更有效的替代方法来实现这一目标。

private ScaleGestureDetector mScaleDetector = new ScaleGestureDetector(v.getContext(), new ScaleListener());
private float mScaleFactor=1.f;
private Button MyButtonView;
MyButtonView.setOnTouchListener(this);
isBtn=false;
//...
public boolean onTouch(View v, MotionEvent event) {
    if(!isBtn) mScaleDetector.onTouchEvent(event);
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();
        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));
        //TODO set width = getWidth()*mScaleFactor;
        //TODO set height = getHeight()*mScaleFactor; 
        MyClass.this.MyButtonView.invalidate();
        return true;
    }
}

0 个答案:

没有答案