[英]How to listen for Android View transformation change?
我正在创建一个自定义View
,我想听一下转换的变化。 例如,由View#setScaleX
触发的那些。 一种方法是覆盖所有方法:
我错过了什么吗? 我不关心 top/left/bottom/right 属性,所以它们被故意排除在外。 然而这很麻烦。 如果我能得到一个回调并监听它会更好。 那可能吗?
//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);
}
唯一的问题是它不会检测到这些函数“设置”的下划线值的内部变化。
例如, View
中有一个名为protected int mLeft;
多次修改,内部不使用函数。
该变量也protected
意味着View
的abstractions
也可以在没有 function 调用的情况下修改它。
在大多数情况下,只有与Views
混淆的外部类才会使用那些可能会或可能不会影响您的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.