Android动画视图从右到左

时间:2016-04-23 11:06:03

标签: android animation view transition

我做了一个小应用程序,我在Android动画/过渡中的新功能。

我想要的: 如果您按下按钮,背景(视图)应该滑出而另一个应该进入,但我不想开始其他活动,我只想更改现有的背景。

这正是我想要的:https://www.polymer-project.org/0.5/components/core-animated-pages/demos/simple.html (您必须更改左上角框中的框才能看到它)

我在其他帖子中读过一些关于它的内容,但通常有解决方案来启动另一个活动..

如果您理解我的意思,那么给我一个提示或教程或其他内容的链接会很高兴。

编辑 - 解决方案

我让它为我工作,我建立了一个完全符合我想做的代码,我给了所有答案1up并标记了最后一个,因为它接近我所做的。 谢谢大家。

此链接非常有用: https://github.com/codepath/android_guides/wiki/Animations(我到目前为止找到的最好的动画教程)

Slide right to left Android Animations(xmls表示从右到左,从上到下,......)

左转过来的例子:

代码:

public void transitionleft(final View viewcome, final View viewgo){
        animcome = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_right_in);
        animgo = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_out);
        animgo.setDuration(1000);
        animcome.setDuration(1000);

        animcome.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                viewcome.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        animgo.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                viewgo.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        viewcome.startAnimation(animcome);
        viewgo.startAnimation(animgo);
    }

res / anim / slide_right_in中的XML转换

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

RES /动画/ slide_left_out

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

工作原理: 我有2个视图都有屏幕大小(match_parent)。一个是visibile另一个不是。 然后你将函数看作viewgo(因为它应该消失)和看不见的(因为这应该是新的进入者) 在动画的开头,视图将设置为可见,因为它“滑入”。 动画完成后,viewgo将不可见,因为它“滑出”。 它非常简单,工作得非常好。

为了改进动画,您可以使用AnimatorSet同步两个动画(set.playtogether()),但如果没有它,它对我也有好处。

3 个答案:

答案 0 :(得分:1)

也许你可以尝试使用valueAnimator:

StreamEncoder

答案 1 :(得分:1)

java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder;
    javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:662)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

在onCreate中,默认颜色设置为黑色。现在每当发生一次&#39;事件&#39;检测到,如触摸,滑动(投掷),滚动等应用程序应更改颜色。 在这里我改变了onFling的颜色,你可以修改它来获得随机颜色,甚至在其他事件监听器中也这样做。

This link应该指出正确的方向。

希望这有帮助。

编辑:

对于过渡效果:

 public class MainActivity extends Activity implements OnGestureListener {    
    private LinearLayout main;    
    private TextView viewA;

    private GestureDetector gestureScanner;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //this will set default color to black
        View view = findViewById(R.id.textView);
        view.setBackgroundColor(Color.BLACK);
    }

    @Override
    public boolean onTouchEvent(MotionEvent me) {
        return gestureScanner.onTouchEvent(me);

    }

    @Override
    public boolean onDown(MotionEvent e) {
        viewA.setText("-" + "DOWN" + "-");
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        viewA.setText("-" + "FLING" + "-");

        //changes color on swipe
        view.setBackgroundColor(Color.GREEN);
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {
        viewA.setText("-" + "LONG PRESS" + "-");
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        viewA.setText("-" + "SCROLL" + "-");
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        viewA.setText("-" + "SHOW PRESS" + "-");
    }    

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        viewA.setText("-" + "SINGLE TAP UP" + "-");
        return true;
    }
}

您的后台资源文件:

animatedBackgroundView.setBackgroundResource(R.anim.background_touch);
animatedBackgroundView.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    TransitionDrawable transition = (TransitionDrawable) view.getBackground();
    transition.startTransition(500);
    transition.reverseTransition(500);
}
});

答案 2 :(得分:1)

这是一个完整的解决方案:

在jour xml文件中,在同一位置设置两个视图,并将第二个视图的可见性设置为invisible

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:id="@+id/currentView"
        android:text="currentView"/>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="invisible"
        android:id="@+id/nextView"
        android:text="nextView"/>

    </RelativeLayout>

然后在你的活动中:

//First in you onCreate translate the second Layout to the right
comingView.setTranslationX(testbutton.getWidth());

// Here is the method that will do the job
// We slide both views (current one and waiting one) at the same time
// and in the same direction

private void slideExit(View currentView,final View comingView) {
    currentView.animate().
            translationXBy(-currentView.getWidth()).
            setDuration(1000).
            setInterpolator(new LinearInterpolator()).
            setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    super.onAnimationStart(animation);
                    slideFromRightToLeft(comingView);
                    comingView.setVisibility(View.VISIBLE);
                }
            });
}

private void slideFromRightToLeft(View v) {
    v.animate().
            translationX(0).
            setDuration(1000).
            setInterpolator(new LinearInterpolator());
}

//call the method whenever you want
slideExit(currentView, comingView);