此处,值200在不同的屏幕上会有所不同。有什么办法可以解决这个问题?
MarginProxy marginProxy = new MarginProxy(fab);
ObjectAnimator marginAnimation = ObjectAnimator.ofInt(marginProxy, "bottomMargin", 530, 200).setDuration(300);
marginAnimation.start();
运行动画时,按钮在另一个屏幕(像素)上的移动方式不同,并在另一个设备(三星J7)上的另一个位置移动
这是保证金代理类
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();
}
}
答案 0 :(得分:0)
不必使用像素,而必须使用Dips。
public int convertDiptoPix(int dip){
float scale = getDensity();
return (int) (dip * scale + 0.5f);}
然后将您的“ 200”像素值交换为该方法的结果。