简体   繁体   中英

How to listen for Android View transformation change?

I am creating a custom View and I would like to listen for the transformation changes. For example, the ones triggered by View#setScaleX . One way to do it is overriding all the methods:

  • setTranslationX
  • setTranslationY
  • setTranslationZ
  • setElevation
  • setRotation
  • setRotationX
  • setRotationY
  • setScaleX
  • setScaleY
  • setPivotX
  • setPivotY
  • setCameraDistance
  • setAnimationMatrix

Am I missing anything? I don't care for the top/left/bottom/right properties so they are left out intentionally. However this is cumbersome. It would be better if I can just get a callback and listen for it. Is that possible?

//Make some kind of callback 
public interface TransformationCallback{
     //String whatWho is an example it can be anything.
     void onTransform(String whatWho);
}

public class YourView extends View{
     private TransformationCallback callback;
     //Pass an interface into the View constructor
     public YourView(Context context, TransformationCallback callback){
        super(context);
        this.callback = callback;
     }
}

@Override
public void setTranslationX(float x){
     //call onTransform from the callback
     callback.onTransform("setTranslationX was called");
     super.setTranslationX(x);
}

The only problem with this, is it will not detect internal changes to the underlining values that these functions "set".

For example there is a variable inside View called protected int mLeft; Which is modified multiple times, internally not using functions.

The variable is also protected meaning abstractions of View can also modify it without function calls.

For the most part only external classes that mess with Views will use those functions which may or may not effect you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM