拖动,旋转和缩放相对布局

时间:2016-05-25 07:35:40

标签: android

我想要旋转&同时缩放,但在旋转和缩放布局时它会闪烁。这是我的代码

public class CustomRelaytivelayout extends RelativeLayout implements RotationGestureDetector.OnRotationGestureListener {

private float scale = 1.0f;
private float touchX = 0.0f;
private float touchY = 0.0f;
public ScaleGestureDetector mScaleDetector;
public RotationGestureDetector mRotationDetector;
private CustomRelaytivelayout customRelaytivelayout_;


float mScaleFactor = 1;
float mPivotX;
float mPivotY;

public CustomRelaytivelayout(Context context) {
    super(context);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener(this));
    mRotationDetector = new RotationGestureDetector(this);
    customRelaytivelayout_ = this;
}

public CustomRelaytivelayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener(this));
    mRotationDetector = new RotationGestureDetector(this);
    customRelaytivelayout_ = this;
}

private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

    private CustomRelaytivelayout customRelaytivelayout;

    float startingSpan; 
    float endSpan;
    float startFocusX;
    float startFocusY;

    public ScaleListener(CustomRelaytivelayout customRelaytivelayout_) {
        customRelaytivelayout = customRelaytivelayout_;
    }

    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        // Multiply scale factor
        scale *= detector.getScaleFactor();




        ViewHelper.setScaleX(customRelaytivelayout,  scale);    
        ViewHelper.setScaleY(customRelaytivelayout,  scale);
        invalidate();

        return true;
    }

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

    @Override
    public void onScaleEnd(ScaleGestureDetector detector) {


    }
}

private float angle = 0;

@Override
public void OnRotation(RotationGestureDetector rotationDetector) {
    angle = angle + rotationDetector.getAngle();
    Log.d("TAG_Angle", "" + angle);
    customRelaytivelayout_.setRotation(-angle);
}

protected void dispatchDraw(Canvas canvas) {
    canvas.save(Canvas.MATRIX_SAVE_FLAG);
    canvas.scale(mScaleFactor, mScaleFactor, mPivotX, mPivotY);
    super.dispatchDraw(canvas);
    canvas.restore();
}

public void scale(float scaleFactor, float pivotX, float pivotY) {
    mScaleFactor = scaleFactor;
    mPivotX = pivotX;
    mPivotY = pivotY;
    this.invalidate();
}

public void restore() {
    mScaleFactor = 1;
    this.invalidate();
}

}

在旋转和缩放布局时闪烁。如果有任何人有解决方案,请分享

提前致谢

0 个答案:

没有答案