简体   繁体   English

Android - animate() 在不同屏幕尺寸下的问题

[英]Android - Problems with animate() in different screen sizes

Im having this problem with the animate() in different screen sizes.我在使用不同屏幕尺寸的 animate() 时遇到了这个问题。 I was testing everything in my s9+ and when i tested the same on a Moto g3 the layouts that i animated with animate().translationY were a disaster.我在我的 s9+ 上测试了所有东西,当我在 Moto g3 上测试同样的东西时,我用 animate().translationY 动画的布局是一场灾难。 So i googled a little bit and of course i was doing the things in a wrong way.所以我用谷歌搜索了一下,当然我做的事情是错误的。 I found here that i have to use this piece of code:我在这里发现我必须使用这段代码:

Float distance = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 770,
                getResources().getDisplayMetrics()
        );

(770) is my value. (770) 是我的价值。

And then cardViewBottom.animate().translationY(distance).setDuration(1);然后cardViewBottom.animate().translationY(distance).setDuration(1);

But i am not having a good result.但我的结果并不好。 In my s9+ 770 is the correct value, and in my moto g3 (smaller device) is 560 the value.在我的 s9+ 中 770 是正确的值,而在我的 moto g3(较小的设备)中是 560 的值。 I don't know what to do.我不知道该怎么办。

Try this:尝试这个:

val metrics = DisplayMetrics()
windowManager?.defaultDisplay?.metrics(metrics)
val width = metrics.density

val distance = TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, width,
                resources.displayMetrics)

Not sure what TypedValue.applyDimension's params are If you need pixels then metrics.widthPixels不确定 TypedValue.applyDimension 的参数是什么 如果你需要像素然后metrics.widthPixels

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM