从右向左滑动Androidanimation

嘿,我正在开发一个Android项目,需要Android WebView上的幻灯片animation。 当用户从左向右滑动时,它将移动到新页面,当从右向左移动时,它将移动到上一页面。 但Android只有两个转换,即slide_out_right和slide_in_left。 使用它们后,从左到右滑动工作是完美的,但另一个看起来不可思议(相反)。

任何解决scheme。 我希望slide_out_leftanimation更加精确。

通过这个博客文章阅读过渡animation的例子,我已经包含了下面的代码:

调用活动

package com.as400samplecode; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button nextActivity = (Button) findViewById(R.id.nextActivity); nextActivity.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.nextActivity: Intent nextActivity = new Intent(this,NextActivity.class); startActivity(nextActivity); //push from bottom to top overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out); //slide from right to left //overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); break; // More buttons go here (if any) ... } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } 

呼叫活动的布局资源

 <?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:background="@color/ivory"> <Button android:id="@+id/nextActivity" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="15dp" android:text="Go to Next Activity" /> </RelativeLayout> 

被称为活动

 package com.as400samplecode; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class NextActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_next); Button previousActivity = (Button) findViewById(R.id.previousActivity); previousActivity.setOnClickListener(this); } public void onClick(View v) { switch (v.getId()) { case R.id.previousActivity: finish(); //push from top to bottom overridePendingTransition(R.anim.push_down_in, R.anim.push_down_out); //slide from left to right //overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right); break; // More buttons go here (if any) ... } } } 

被调用活动的布局资源

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".NextActivity" android:background="@color/khaki"> <Button android:id="@+id/previousActivity" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="15dp" android:text="Go to Previous Activity" /> </RelativeLayout> 

animation资源 – push_down_in.xml

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

animation资源 – push_down_out.xml

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

animation资源 – push_up_in.xml

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

animation资源 – push_up_out.xml

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

animation资源 – slide_in_left.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="0.0" android:toAlpha="1.0" /> </set> 

animation资源 – slide_in_right.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="0.0" android:toAlpha="1.0" /> </set> 

animation资源 – slide_out_left.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="0%" android:toXDelta="-100%"/> <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" /> </set> 

animation资源 – slide_out_right.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="0%" android:toXDelta="100%"/> <alpha android:duration="5000" android:fromAlpha="1.0" android:toAlpha="0.0" /> </set> 

你可以做你自己的animation风格像这样的XML文件(把它放在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="500"/> </set> 

右到左:

  <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="500" /> </set> 

这里你可以设置你自己的值,也许它取决于手机型号的animation是什么样的,尝试一些值,如果它看起来不好。

然后您可以在您的活动中调用它:

  Intent animActivity = new Intent(this,YourStartAfterAnimActivity.class); startActivity(nextActivity); overridePendingTransition(R.anim.your_left_to_right, R.anim.your_right_to_left); 

为了滑动两个活动(新旧)相同的方向:

left_in.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="-100%" android:toXDelta="0%" android:interpolator="@android:anim/decelerate_interpolator" /> 

right_in.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="100%" android:toXDelta="0%" android:interpolator="@android:anim/decelerate_interpolator" /> 

left_out.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0%" android:interpolator="@android:anim/decelerate_interpolator" android:toXDelta="-100%" /> 

right_out.xml

 <?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromXDelta="0%" android:interpolator="@android:anim/decelerate_interpolator" android:toXDelta="100%" /> 

startActivity转换:

 overridePendingTransition(R.anim.right_in, R.anim.left_out); 

onBackPressed转换:

 overridePendingTransition(R.anim.left_in, R.anim.right_out); 

看到这个链接..你可以在这里看到这么多种animation,只需将xml复制到你的res / anim文件夹中并像下面一样使用它。

 listView.setAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.slide_in_right)); 

animation列表

示例GIF

我发现的最简单的方法是使用Activity Transitions ,这非常简单

在要使用animation运行的活动中重写onCreate方法:

 @Override protected void onCreate(Bundle savedInstanceState) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Slide slide = new Slide(); slide.setSlideEdge(Gravity.RIGHT); getWindow().setEnterTransition(slide); } super.onCreate(savedInstanceState); } 

然后启动它使用转换(而不是activity.startActivity(上下文)):

 activity.startActivity(starter, ActivityOptions.makeSceneTransitionAnimation(activity).toBundle()); 

closuresanimation而不是使用this.finish()使用下面的代码:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { getActivity().finishAfterTransition(); } else getActivity().finish(); 

欲了解更多信息请点击以下链接

  • 带示例的材质animation(推荐)
  • 定义自定义animation

你可以制作自己的animation。 例如像这样在res / anim中创buildxml文件

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator"> <translate android:fromXDelta="100%p" android:toXDelta="0" android:startOffset="0" android:duration="500" /> </set> 

然后覆盖所选活动中的animation:

  overridePendingTransition(R.anim.animationIN, R.anim.animationOUT); 

新的页面animation的权利

 <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="0%" android:toXDelta="800%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="600" />