简体   繁体   English

Android 活动开始时动画视图

[英]Animate Views when Activity starts in Android

I am finishing FlashScreen and starting MainActivity using startActivity();我正在完成FlashScreen并使用startActivity();启动MainActivity and I want to slide linearLayout of MainActivity from left to right slowly like an animation. But it's not happening.我想像 animation 一样从左向右缓慢滑动 MainActivity 的 linearLayout。但它没有发生。 At first the linearLayout loads up and then it performs the animation so it's of no use.首先 linearLayout 加载,然后它执行 animation 所以它没有用。 Here's my code:这是我的代码:

slide_from_left.xml slide_from_left.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate android:fromXDelta="-100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="700"/>
</set>

MainAcitivity.java MainAcitivity.java

Animation animFadein = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide_from_left);
mLinearLayoutParent.startAnimation(animFadein);

Please help me with this.请帮我解决一下这个。 Thanks谢谢

if you are sliding from any side, then this View should be "slided out" in this direction when initialised (on start).如果你从任何一侧滑动,那么这个View应该在初始化时(开始时)朝这个方向“滑出”。 consider adding proper param to XML, in your case it would be android:translationX for mLinearLayoutParent .考虑向 XML 添加适当的参数,在您的情况下,mLinearLayoutParent 的参数为mLinearLayoutParent android:translationX use some very high value to be shure that this View isn't visible, as in here you can't set translation by % of view like in <translate tag.使用一些非常高的值来确保此View不可见,因为在这里您不能像在<translate标记中那样按视图的百分比设置翻译。 when anim starts then android:fromXDelta="-100%" line will move View to this position (exact -100% of width, as initial for animation) at very beginning of animation当动画启动时, android:fromXDelta="-100%"行将在 animation 的最开始将View移动到此 position(宽度的精确-100% ,作为动画的初始值)

To animate every activity when it opens or closes, you need to create four animation files (namely: slide_in_right , slide_in_left , slide_out_right , slide_out_left ).要在每个活动打开或关闭时为其设置动画,您需要创建四个 animation 文件(即: slide_in_rightslide_in_leftslide_out_rightslide_out_left )。 After creating these animations, you will have to add the following code in your values/styles.xml:创建这些动画后,您必须在 values/styles.xml 中添加以下代码:

<style name="android:customActivityAnimation"     parent="@android:style/Animation.Activity">
<item name="activityOpenEnterAnimation">@anim/slide_in_right</item>
<item name="activityOpenExitAnimation">@anim/slide_out_left</item> 
<item name="activityCloseEnterAnimation">@anim/slide_in_left</item> 
<item name="activityCloseExitAnimation">@anim/slide_out_right</item> 

</style>

After that in your default application theme add this style:之后在您的默认应用程序主题中添加此样式:

<style  name="android:windowAnimationStyle">@style/customActivityAnimation</style> 

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

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