以下是上下文:我有一个复杂布局的Activity,我想使用相对复杂的Transition转换它。问题是我需要在图像下面放置一个视图(scroll_frame
)(我不能使用XML),所以我需要知道它的高度。它在第一次工作正常,但是在活动之间来回移动之后,图像的高度突然为零(缓存,竞争条件?)
以下是onCreate
:
super.onCreate(savedInstanceState)
postponeEnterTransition()
setContentView(R.layout.some_id)
// ... non-essential stuff.
Glide.with(this)
.load([some resource id])
.into(object : SimpleTarget<Drawable>() {
override fun onResourceReady(d: Drawable?, t: Transition<in Drawable>?) {
// Prepare image.
image.setImageDrawable(d)
val margin = Math.max(image.measuredHeight, 0)
// ... non-essential stuff
layoutParams.setMargins(0, margin, 0, 0)
// Critical part.
scroll_frame?.layoutParams = layoutParams
// Start transition.
startPostponedEnterTransition()
}
})
答案 0 :(得分:0)
问题是在onCreate中没有给出视图的测量高度。
试试这个:
//Put this in onCreate
imageView.post(new Runnable() {
@Override
public void run() {
//This is called after view measurements are done.
//Run your glide code here.
}
});
希望这有帮助。
答案 1 :(得分:0)
那是因为您的视图尚未设置:
使用它:
image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
//don't forget to remove the listener to prevent being called again by future layout events:
image.getViewTreeObserver().removeOnGlobalLayoutListener(this);
val margin = Math.max(image.measuredHeight, 0)
// ... non-essential stuff
layoutParams.setMargins(0, margin, 0, 0)
// Critical part.
scroll_frame?.layoutParams = layoutParams
// Start transition.
startPostponedEnterTransition()
}
}