简体   繁体   English

如何监听 Android 查看转换变化?

[英]How to listen for Android View transformation change?

I am creating a custom View and I would like to listen for the transformation changes.我正在创建一个自定义View ,我想听一下转换的变化。 For example, the ones triggered by View#setScaleX .例如,由View#setScaleX触发的那些。 One way to do it is overriding all the methods:一种方法是覆盖所有方法:

  • setTranslationX设置翻译X
  • setTranslationY设置翻译Y
  • setTranslationZ设置翻译Z
  • setElevation设置海拔
  • setRotation设置旋转
  • setRotationX设置旋转X
  • setRotationY设置旋转Y
  • setScaleX setScaleX
  • setScaleY setScaleY
  • setPivotX 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.我不关心 top/left/bottom/right 属性,所以它们被故意排除在外。 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;例如, View中有一个名为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.该变量也protected意味着Viewabstractions也可以在没有 function 调用的情况下修改它。

For the most part only external classes that mess with Views will use those functions which may or may not effect you.在大多数情况下,只有与Views混淆的外部类才会使用那些可能会或可能不会影响您的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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