如何制作toRightOf / toLeftOf动画?

时间:2015-12-04 23:01:30

标签: android android-animation

我的布局中有3个项目。中心水平的文本,文本左侧的imageview1,文本右侧的图像视图。

Review

我的布局

<TextView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text=""
    android:layout_centerHorizontal="true"
    android:id="@+id/mainTextView1"/>

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/hub"
    android:layout_toRightOf="@id/mainTextView1"
    android:id="@+id/mainImageView2"/>

<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/logo"
    android:layout_toLeftOf="@id/mainTextView1"
    android:id="@+id/mainImageView1"/>

我的动画尝试:

float ustY;
float altY;
public void animation(){
    ust.setVisibility(View.GONE);
    alt.setVisibility(View.GONE);
    ustY=ust.getX();
    ust.animate().y(0).x(-5000).setDuration(500).start();
    altY=alt.getX();
    alt.animate().y(0).x(4000).setDuration(500).start();
}
public void reverseAnimation(){
    ust.setVisibility(View.VISIBLE);
    alt.setVisibility(View.VISIBLE);
    ust.animate().y(0).x(ustY).setDuration(500).start(); 
    alt.animate().y(0).x(altY).setDuration(500).start(); 
}

我想制作与此相同的动画:

http://i.eyimg.com/M5AvDKXm.gif

结果:

review 2

2 个答案:

答案 0 :(得分:1)

试试xml

res/anim/


<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>

在您的活动代码中:

this.overridePendingTransition(R.anim.animation_enter,
                   R.anim.animation_leave);

答案 1 :(得分:1)

您可以创建一个动画类,如下图所示,其中包括从左侧或右侧滑入,向左或向右滑动的方法......

public class MyAnimator {

    private static int DEFAULT_DURATION = 500;

    public static Animation inFromRightAnimation(long duration) {
        return constructSlideAnimation(1.0f, 0, 0, 0, 
            duration == 0 ? DEFAULT_DURATION : duration);
    }

    public static Animation inFromLeftAnimation(long duration) {
        return construct(-1.0f, 0, 0, 0, 
            duration == 0 ? DEFAULT_DURATION : duration);
    }

    public static Animation outToRightAnimation(long duration) {
        return construct(0, 1.0f, 0, 0, 
            duration == 0 ? DEFAULT_DURATION : duration);
    }

    public static Animation outToLeftAnimation(long duration) {
        return construct(0, -1.0f, 0, 0, 
            duration == 0 ? DEFAULT_DURATION : duration);
    }

    private static Animation construct(float fromX, float toX, float fromY, float toY, long duration) {
        Animation animation = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, fromX, Animation.RELATIVE_TO_PARENT, toX,
            Animation.RELATIVE_TO_PARENT, fromY, Animation.RELATIVE_TO_PARENT, toY
        );
        animation.setDuration(duration);
        animation.setInterpolator(new LinearInterpolator());
        return animation;
    }

}

然后,您可以在需要时添加任何您喜欢的内容,例如淡入淡出动画,动画闪烁,旋转动画等,然后从您的活动/片段中调用方法,如下所示:

porn.setAnimation(MyAnimator.inFromLeftAnimation(800));
hub.setAnimation(MyAnimator.inFromRightAnimation(800));

上面的值800是持续时间参数,以毫秒为单位;如果将其设置为零(0),则MyAnimation类使用DEFAULT_DURATION值(500)。您当然可以将默认持续时间设置为您喜欢的任何值,并始终将零作为参数传递或将其覆盖为更多/更少,具体取决于您为每个动画制作的视图的首选项。

为了获得正确的效果,还可以将您的两个ImageViews设置为android:visibility="gone",然后将它们设置为#34;可见&#34;当你调用幻灯片动画时。如果你想让它们滑出来,你会让它们消失了#34;调用幻灯片动画时:

<强> SLIDE-IN:

porn.setVisibility(View.VISIBLE);
porn.setAnimation(MyAnimator.inFromLeftAnimation(800));

hub.setVisibility(View.VISIBLE);
hub.setAnimation(MyAnimator.inFromRightAnimation(800));

<强> SLIDE-OUT:

porn.setVisibility(View.GONE);
porn.setAnimation(MyAnimator.outToLeftAnimation(800));

hub.setVisibility(View.GONE);
hub.setAnimation(MyAnimator.outToRightAnimation(800));