如何使 android 对象动画师响应?
How to make android object animator responsive?
此处的值 200 在不同的屏幕上会产生不同的结果。有什么办法可以解决这个问题吗?
MarginProxy marginProxy = new MarginProxy(fab);
ObjectAnimator marginAnimation = ObjectAnimator.ofInt(marginProxy, "bottomMargin", 530, 200).setDuration(300);
marginAnimation.start();
当动画开启时 运行,按钮在另一个屏幕(像素)上和另一个设备(三星 J7)上的另一个位置移动不同
这是保证金代理 class
class MarginProxy {
private View mView;
public MarginProxy(View view) {
mView = view;
}
public int getLeftMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.leftMargin;
}
public void setLeftMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(margin, lp.topMargin, lp.rightMargin, lp.bottomMargin);
mView.requestLayout();
}
public int getTopMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.topMargin;
}
public void setTopMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(lp.leftMargin, margin, lp.rightMargin, lp.bottomMargin);
mView.requestLayout();
}
public int getRightMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.rightMargin;
}
public void setRightMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(lp.leftMargin, lp.topMargin, margin, lp.bottomMargin);
mView.requestLayout();
}
public int getBottomMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.bottomMargin;
}
public void setBottomMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, margin);
mView.requestLayout();
}
}
您必须使用 Dips 而不是使用像素。
public int convertDiptoPix(int dip){
float scale = getDensity();
return (int) (dip * scale + 0.5f);}
然后将您的“200”像素值交换为该方法的结果。
此处的值 200 在不同的屏幕上会产生不同的结果。有什么办法可以解决这个问题吗?
MarginProxy marginProxy = new MarginProxy(fab);
ObjectAnimator marginAnimation = ObjectAnimator.ofInt(marginProxy, "bottomMargin", 530, 200).setDuration(300);
marginAnimation.start();
当动画开启时 运行,按钮在另一个屏幕(像素)上和另一个设备(三星 J7)上的另一个位置移动不同
这是保证金代理 class
class MarginProxy {
private View mView;
public MarginProxy(View view) {
mView = view;
}
public int getLeftMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.leftMargin;
}
public void setLeftMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(margin, lp.topMargin, lp.rightMargin, lp.bottomMargin);
mView.requestLayout();
}
public int getTopMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.topMargin;
}
public void setTopMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(lp.leftMargin, margin, lp.rightMargin, lp.bottomMargin);
mView.requestLayout();
}
public int getRightMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.rightMargin;
}
public void setRightMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(lp.leftMargin, lp.topMargin, margin, lp.bottomMargin);
mView.requestLayout();
}
public int getBottomMargin() {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
return lp.bottomMargin;
}
public void setBottomMargin(int margin) {
MarginLayoutParams lp = (MarginLayoutParams) mView.getLayoutParams();
lp.setMargins(lp.leftMargin, lp.topMargin, lp.rightMargin, margin);
mView.requestLayout();
}
}
您必须使用 Dips 而不是使用像素。
public int convertDiptoPix(int dip){
float scale = getDensity();
return (int) (dip * scale + 0.5f);}
然后将您的“200”像素值交换为该方法的结果。