Android滑动动画,可以用ViewPager或者ViewFlipper实现。
ViewPager自带触摸滑动功能,结合Fragment使用很好,来自补充组件android-support-v4.jar;
ViewFlipper要借助GestureDetector来实现手势滑动,是系统自带组件。
下面是用ViewFlipper实现的图片滑动动画,没有手势滑动功能,activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/splash" android:orientation="vertical">
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/centerPoint" android:text="center" android:gravity="center" android:layout_centerInParent="true"/>
<ImageView android:layout_width="138dp" android:layout_height="39dp" android:src="@drawable/loading" android:id="@+id/loading" android:layout_above="@+id/centerPoint" android:layout_alignLeft="@id/centerPoint" android:layout_marginBottom="90dp"/>
<ImageView android:layout_width="142dp" android:layout_height="113dp" android:id="@+id/pet" android:layout_toLeftOf="@id/loading" android:layout_alignTop="@id/loading" android:layout_marginTop="-35dp" android:layout_marginRight="-6dp" android:src="@drawable/pet"/>
<ImageView android:layout_width="18dp" android:layout_height="20dp" android:id="@+id/zz" android:background="@drawable/zz" android:layout_alignRight="@id/pet" android:layout_alignTop="@id/pet" android:layout_marginRight="30dp" android:layout_marginTop="5dp"/>
<ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bg" android:background="@drawable/bg" android:layout_alignTop="@id/loading" android:layout_marginTop="50dp"/>
<ViewFlipper android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/flipper" android:layout_alignTop="@id/loading" android:layout_marginTop="56dp"/>
</RelativeLayout>
下面是代码部分,这里不提供图片,代码中动态创建动画,MainActivity.java:
package com.activ8.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.view.animation.AnimationSet; import android.view.animation.DecelerateInterpolator; import android.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; import android.widget.ImageView; import android.widget.ViewFlipper; import java.util.Random; public class MainActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); ViewFlipper mFlipper = (ViewFlipper) findViewById(R.id.flipper); int[] imgResId = new int[] {R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e }; for (int i = 0; i < imgResId.length; i++){ ImageView imageView = new ImageView(this); imageView.setImageResource(imgResId[i]); mFlipper.addView(imageView); } mFlipper.setDisplayedChild(new Random().nextInt(imgResId.length)); // viewFlipper animation
AnimationSet mFlipper_animSet_in = new AnimationSet(true); TranslateAnimation mFlipper_animIn_t = new TranslateAnimation( TranslateAnimation.RELATIVE_TO_SELF, 1f, TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, 0f); mFlipper_animIn_t.setDuration(300); mFlipper_animSet_in.addAnimation(mFlipper_animIn_t); AnimationSet mFlipper_animSet_out = new AnimationSet(true); TranslateAnimation mFlipper_animOut_t = new TranslateAnimation( TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, -1f, TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, 0f); mFlipper_animOut_t.setDuration(300); mFlipper_animSet_out.addAnimation(mFlipper_animOut_t); mFlipper.setInAnimation(mFlipper_animSet_in); mFlipper.setOutAnimation(mFlipper_animSet_out); mFlipper.setFlipInterval(4000); mFlipper.startFlipping(); // image zz animation
final int animDuration = 1600; TranslateAnimation mZZ_anim_translate = new TranslateAnimation( TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, 1.8f, TranslateAnimation.RELATIVE_TO_SELF, 0f, TranslateAnimation.RELATIVE_TO_SELF, -1f); mZZ_anim_translate.setDuration(animDuration); mZZ_anim_translate.setInterpolator(new DecelerateInterpolator()); mZZ_anim_translate.setRepeatMode(Animation.RESTART); mZZ_anim_translate.setRepeatCount(Integer.MAX_VALUE); ScaleAnimation mZZ_anim_scale = new ScaleAnimation(1, 2, 1, 2, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 1f); mZZ_anim_scale.setDuration(animDuration); mZZ_anim_scale.setInterpolator(new DecelerateInterpolator()); mZZ_anim_scale.setRepeatMode(Animation.RESTART); mZZ_anim_scale.setRepeatCount(Integer.MAX_VALUE); AlphaAnimation mZZ_anim_alpha = new AlphaAnimation(1, 0); mZZ_anim_alpha.setDuration(animDuration); mZZ_anim_alpha.setInterpolator(new DecelerateInterpolator()); mZZ_anim_alpha.setRepeatMode(Animation.RESTART); mZZ_anim_alpha.setRepeatCount(Integer.MAX_VALUE); AnimationSet mZZ_animSet = new AnimationSet(true); mZZ_animSet.addAnimation(mZZ_anim_translate); mZZ_animSet.addAnimation(mZZ_anim_scale); mZZ_animSet.addAnimation(mZZ_anim_alpha); ImageView zzImg = (ImageView)findViewById(R.id.zz); zzImg.startAnimation(mZZ_animSet); } }