首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何制作正确的/左边的动画?

如何制作正确的/左边的动画?
EN

Stack Overflow用户
提问于 2015-12-04 23:01:29
回答 2查看 142关注 0票数 0

我有三个项目在我的布局。中间水平的文本,文本的左imageview1,文本的右视图2。

我的布局

代码语言:javascript
复制
<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"/>

我的动画尝试:

代码语言:javascript
复制
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

其结果是:

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-12-05 09:04:51

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

代码语言:javascript
复制
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;
    }

}

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

代码语言:javascript
复制
porn.setAnimation(MyAnimator.inFromLeftAnimation(800));
hub.setAnimation(MyAnimator.inFromRightAnimation(800));

上面的值800是以毫秒为单位的持续时间参数;如果将其设置为零(0),则MyAnimation类将使用DEFAULT_DURATION值(500)。当然,您可以将默认持续时间设置为任何您喜欢的,并且始终将零作为参数传递,或者根据每个动画视图的首选项,将其重写为“多/少”。

为了获得适当的效果,还可以将您的两个ImageViews设置为android:visibility="gone",并在调用幻灯片动画时使它们“可见”。如果你想让它们滑出,你会让它们在调用滑出动画时“消失”:

幻灯片-IN:

代码语言:javascript
复制
porn.setVisibility(View.VISIBLE);
porn.setAnimation(MyAnimator.inFromLeftAnimation(800));

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

滑出:

代码语言:javascript
复制
porn.setVisibility(View.GONE);
porn.setAnimation(MyAnimator.outToLeftAnimation(800));

hub.setVisibility(View.GONE);
hub.setAnimation(MyAnimator.outToRightAnimation(800));
票数 1
EN

Stack Overflow用户

发布于 2015-12-05 03:34:08

尝试xml

代码语言:javascript
复制
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>

在您的活动代码中:

代码语言:javascript
复制
this.overridePendingTransition(R.anim.animation_enter,
                   R.anim.animation_leave);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34098935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档