如何放大ViewGroup?

时间:2015-06-04 12:18:51

标签: android zoom scale relativelayout

我需要创建用户可以扩展的视图。我将在视图寻呼机中显示的这些组件的列表。所以我可以t do scaling in activity. That为什么我创建了扩展RelativeLayout的自定义视图。

public class JsonRelativeLayout extends RelativeLayout {

private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;

public JsonRelativeLayout(Context context) {
    super(context);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());

}

public JsonRelativeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}

public JsonRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    mScaleDetector.onTouchEvent(event);
    return true;
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.save();
    canvas.scale(mScaleFactor, mScaleFactor);
    //...
    //Your onDraw() code
    //...
    canvas.restore();
}

private class ScaleListener extends 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));

        invalidate();
        return true;
    }

}

但是onScale方法不起作用。而且我不知道为什么。

1 个答案:

答案 0 :(得分:0)

要缩放视图组,您需要扩展ScrollView并覆盖dispatchTouchEvent和dispatchDraw。