How to call invalidate() method from another class?

1k Views Asked by At

I'm trying to implement pinch zooming functionality on my custom view in my Android application. I have use following class to do it.

public class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {

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

    public ScaleListener(){

    }

    @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(); //Error

        return true;
    }
}

I want to call invalidate() method as above within onScale but it's wrong because this is outside from my custom view. However I try to do it by instantiating a custom view here as follows.

View myview = new DrawingView();
myview.invalidate();

But this one also returns an error. Can't instantiate my custom view. So how to call invalidate() method here?

1

There are 1 best solutions below

0
On BEST ANSWER

invalidate() is method of View class. Therefor if you want to call it within your class that not extended by the View, you have to pass your view you want to invalidate. Follow the following instruction.

First create local variable using View class as public DrawingView view; within your ScaleListener class. Then instantiate your class within your custom view as scale = new ScaleListener();. Then you can pass the view as scale.view = this;. Now you can call to invalidate() method within your class as view.invalidate();