不知道尺寸时如何移动视图?
How to move a view when you don't know its dimensions?
我想做的是在 ImageView
上制作尺寸和位置变化的动画。为此,我使用以下方法:
void doAnimation() {
ImageView logo = findViewById(R.id.logo);
float scaleVal = R.attr.actionBarSize / logo.getHeight(); // Finds the scale needed to make the logo as tall as the actionBar
// Gets the width and height in dp
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float density = getResources().getDisplayMetrics().density;
float dpH = displayMetrics.heightPixels / density;
float dpW = displayMetrics.widthPixels / density;
// The final X and Y in dp
float finalX = dpW - (logo.getWidth() / density) - 16;
float finalY = dpH - (logo.getHeight() / density) - 16;
// The final X and Y converted to %
float finalXPercentage = finalX / dpW;
float finalYPercentage = finalY / dpH;
logo.animate()
.setDuration(750)
.scaleX(scaleVal)
.scaleY(scaleVal)
.translationX(finalXPercentage)
.translationY(finalYPercentage);
}
我正在从 onResume()
方法中调用 doAnimation()
方法,但是由于显而易见的原因,我无法在 [=13= 中使用 getHeight()
或 getWidth()
] 方法。
问题是我不知道徽标的高度或宽度,因为我使用的是 ConstraintLayout
,所以我没有指定宽度或高度。我想要做的是将 ImageView
移动到右上角(距它 16dp space)并将其调整为与 ActionBar(工具栏)高度一样高。
你能在onLayoutChange
里面试一下吗。我这样解决了我的问题。
logo.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
mLayout.removeOnLayoutChangeListener(this);
doyourAnimationcodehere();
}
});
您可以尝试使用 View.Post()
void doAnimation() {
ImageView logo = findViewById(R.id.logo);
// new Runnable() can be substituted with () -> if you are using Java 8
logo.post(new Runnable() {
@Override
public void run() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float density = getResources().getDisplayMetrics().density;
// Finds the scale needed to make the logo as tall as the actionBar
float scaleVal = 56 / (logo.getHeight() / density);
// Gets the width and height in dp
float width = displayMetrics.widthPixels / density;
float height = displayMetrics.heightPixels;
// The final X and Y in dp
float finalX = logo.getX() + width / 2;
logo.animate()
.setDuration(ANIMATION_DURATION)
.scaleX(scaleVal).scaleY(scaleVal)
.xBy(finalX).y(0);
}
});
}
我想做的是在 ImageView
上制作尺寸和位置变化的动画。为此,我使用以下方法:
void doAnimation() {
ImageView logo = findViewById(R.id.logo);
float scaleVal = R.attr.actionBarSize / logo.getHeight(); // Finds the scale needed to make the logo as tall as the actionBar
// Gets the width and height in dp
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float density = getResources().getDisplayMetrics().density;
float dpH = displayMetrics.heightPixels / density;
float dpW = displayMetrics.widthPixels / density;
// The final X and Y in dp
float finalX = dpW - (logo.getWidth() / density) - 16;
float finalY = dpH - (logo.getHeight() / density) - 16;
// The final X and Y converted to %
float finalXPercentage = finalX / dpW;
float finalYPercentage = finalY / dpH;
logo.animate()
.setDuration(750)
.scaleX(scaleVal)
.scaleY(scaleVal)
.translationX(finalXPercentage)
.translationY(finalYPercentage);
}
我正在从 onResume()
方法中调用 doAnimation()
方法,但是由于显而易见的原因,我无法在 [=13= 中使用 getHeight()
或 getWidth()
] 方法。
问题是我不知道徽标的高度或宽度,因为我使用的是 ConstraintLayout
,所以我没有指定宽度或高度。我想要做的是将 ImageView
移动到右上角(距它 16dp space)并将其调整为与 ActionBar(工具栏)高度一样高。
你能在onLayoutChange
里面试一下吗。我这样解决了我的问题。
logo.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
mLayout.removeOnLayoutChangeListener(this);
doyourAnimationcodehere();
}
});
您可以尝试使用 View.Post()
void doAnimation() {
ImageView logo = findViewById(R.id.logo);
// new Runnable() can be substituted with () -> if you are using Java 8
logo.post(new Runnable() {
@Override
public void run() {
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float density = getResources().getDisplayMetrics().density;
// Finds the scale needed to make the logo as tall as the actionBar
float scaleVal = 56 / (logo.getHeight() / density);
// Gets the width and height in dp
float width = displayMetrics.widthPixels / density;
float height = displayMetrics.heightPixels;
// The final X and Y in dp
float finalX = logo.getX() + width / 2;
logo.animate()
.setDuration(ANIMATION_DURATION)
.scaleX(scaleVal).scaleY(scaleVal)
.xBy(finalX).y(0);
}
});
}