【问题标题】:Android animation view right to leftAndroid 动画视图从右到左
【发布时间】:2016-04-24 01:45:01
【问题描述】:

我制作了一个小应用程序,并且是 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/anim/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)。一个是可见的,另一个不可见。 然后你把可见的作为viewgo(因为这应该消失)和不可见的作为viewcome(因为这应该是新进来的人) 在动画开始时,viewcome 将设置为可见,因为它“滑入”。 动画完成后,viewgo 将不可见,因为它“滑出”。 这很简单,而且效果很好。

要改进动画,您可以使用 AnimatorSet 来同步两个动画 (set.playtogether()),但如果没有它,它对我也很有效。

【问题讨论】:

  • 这正是我要找的!

标签: android animation view transition


【解决方案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);

【讨论】:

    【解决方案2】:

    也许你可以试试 valueAnimator:

    //animate from your current color to red ValueAnimator 
    anim = ValueAnimator.ofInt(view.getBackgroundColor(), 
               Color.parseColor("#FF0000")); 
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
        @Override public void onAnimationUpdate(ValueAnimator animation) {       
             view.setBackgroundColor(animation.getAnimatedValue()); 
        } 
    }); 
    anim.start();
    

    【讨论】:

      【解决方案3】:
       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;
          }
      }
      

      在 onCreate 中,默认颜色设置为黑色。现在,每当检测到“事件”时,例如触摸、滑动(滑动)、滚动等,您的应用程序都应该改变颜色。 在这里,我更改了 onFling 的颜色,您可以对其进行修改以获得随机颜色,甚至在其他事件侦听器中也可以这样做。

      This link 应该为您指明正确的方向。

      希望这会有所帮助。

      编辑:

      过渡效果:

      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);
      }
      });
      

      你的后台资源文件:

      <?xml version="1.0" encoding="UTF-8"?>
      <transition xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="#22ffffff" />
      <item android:drawable="#00ffffff" />
      </transition>
      

      【讨论】:

      • 但这只会将背景设置为其他颜色,我希望颜色变化是我发布的链接中的动画。
      • 对不起,但我希望背景滑出而另一个进来,所以我需要一个动画。 (如果你改变盒子,就像我的链接一样)。这是活动stackoverflow.com/questions/18147840/… 的一个很好的例子,但我不知道如何在视图或背景上使用它。
      • 这正是你想要的Using ViewPager for Screen Slides。如果您仍有任何疑问,请告诉我。
      • 感谢您的提示,但我想让它随机滑动,所以我需要向上、向下、向右、向左,使用 Viewpager 会比我的解决方案更难
      猜你喜欢
      • 1970-01-01
      • 2012-01-30
      • 2011-07-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多