Android系统。控制缩放速度

时间:2016-01-14 07:48:50

标签: android scale

我正在创建可以存储,移动,缩放和裁剪图片的View。我已经知道许多已经完成此项目的项目,因此链接到其他项目无法回答,但我不确定是否有可能实现我的请求。

所以基本上我在ScaleGestureDetector上使用ScaleGestureDetector.SimpleOnScaleGestureListener来扩展我的Canvas,这里是一段代码:

private float MIN_SCALE_FACTOR = 0.3f;
private float MAX_SCALE_FACTOR = 10.0f;
private float mScaleFactor = 1.f;
private ScaleGestureDetector mScaleDetector;
private ScaleGestureDetector.SimpleOnScaleGestureListener mScaleListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();
        mScaleFactor = Math.max(MIN_SCALE_FACTOR, Math.min(mScaleFactor, MAX_SCALE_FACTOR));
        invalidate();
        return super.onScale(detector);
    }
};

@Override
protected void onDraw(Canvas canvas) {
    if (mBitmap != null) {
        canvas.save();
        super.onDraw(canvas);
        correctPositionValues();
        canvas.scale(mScaleFactor, mScaleFactor, mPositionX, mPositionY );
        canvas.drawBitmap(mBitmap, mPositionX, mPositionY, null);
        canvas.restore();
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    mScaleDetector.onTouchEvent(event);
}

在我的手机上,刻度正在发生,这看起来很糟糕。 任何建议高度赞赏

1 个答案:

答案 0 :(得分:0)

根据先前的比例因子进行缩放:

private ScaleGestureDetector.SimpleOnScaleGestureListener mScaleListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
    private preScaleFactor = 1f;
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor = preScaleFactor*detector.getScaleFactor();
        mScaleFactor = Math.max(MIN_SCALE_FACTOR, Math.min(mScaleFactor, MAX_SCALE_FACTOR));
        invalidate();
        return super.onScale(detector);
    }

    @Override
    public boolean onScaleBegin(ScaleGestureDetector detector) {
       preScaleFactor = mScaleFactor;
       return true;
    }
};