【问题标题】:Translation of image from center to top in android在android中从中心到顶部的图像翻译
【发布时间】:2020-08-11 16:55:16
【问题描述】:

您好,我遇到了创建初始屏幕的问题,其中图像放置在 center_vertical|center Horizo​​ntal.how to translater over to center_horizo​​ntal|top in android.可以请任何人指导我如何做。

【问题讨论】:

  • 试试这个 android:layout_gravity="top|center_horizo​​ntal"
  • @Saravanan:如何翻译成它
  • 将此gravity="center_horizo​​ntal" 属性添加到您的父布局中,该布局包含您的图像视图。

标签: android android-layout android-animation


【解决方案1】:

使用动画来做到这一点,有关动画视图的更多信息,您可以参考this linkthis

在将视图从屏幕中心翻译到屏幕顶部时,您实际上正在寻找什么,您可以使用使用 xml 的翻译动画。

1.在res文件夹中创建文件夹anim 2.添加一个描述翻译效果的资源文件,如:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="50%p" android:fromYDelta="50%p" 
    android:toXDelta="50%p" android:toYDelta="0%p" 
    android:duration="1000" 
    android:fillAfter="true" />

您可以尝试使用 % 值。

3.在代码中实现这个:

translateAnim= AnimationUtils.loadAnimation(getApplicationContext(),
            R.anim.translate_anim);
imageView.startAnimation(translateAnim);

希望这么多线索就足够了!

P.S:你可以试验一下fillAfter是真还是假,这样你就可以更好地理解它们的效果。

【讨论】:

  • @Ratatouille:我需要像闪屏这样的facebook,我们可以实现它
  • 是的,你可以使用动画。
  • @Ratatouille:我正在翻译顶部的图像,但在该图像必须固定在它停止的位置并且登录布局已显示在 center_vertical 和 canter_horizo​​ntal 之后
  • 执行此操作,将图像设置为默认 center_horizo​​ntal 和 top ,然后当应用程序通过代码启动时,从屏幕中心启动视图动画,然后当动画停止时,它将保持在当您将 fillAfter 设置为 true(即顶部)时,它的默认位置,这就是动画的一般工作方式
  • @Ratatoulli:但是当它改变时会有轻微的偏差..你能指导我吗..
【解决方案2】:

这是我认为从中心到顶部实现动画的最佳和最简单的方法:

ivSplashCenter.animate()
            .translationY(-((rlContainer.height - (ivSplashCenter.height * 2)) / 2).toFloat())
            .setInterpolator(AccelerateInterpolator()).duration = 1500

这里,ivSplashCenter 是 imageview,rlContainer 是我的 XML 根视图

注意:

您需要在这里了解的一件事是,我在ivSplashCenter.height * 2 的图像高度的顶部边缘保留了一个空间, 如果您不希望顶部有任何填充/空格,则可以使用ivSplashCenter.height

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-18
    • 1970-01-01
    • 2018-01-01
    • 2018-11-20
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多