
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>
在创建滑动动画前,还需要播放一小段mp4视频作为Logo,这里使用android的VideoView完成。
除了layout中的activity_main.xml资源文件,我们把logo.mp4文件和一些用于滑动动画的图片放入drawable目录下。
package com.example.mytest; import java.util.Random; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.graphics.Color; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnPreparedListener; import android.net.Uri; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import android.view.ViewParent; 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.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.VideoView; import android.widget.ViewFlipper; public class MainActivity extends Activity implements OnCompletionListener, OnPreparedListener { private static MainActivity m_Activity; private VideoView m_videoView; private RelativeLayout m_rootLayout; private boolean m_logoFinished; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); m_Activity = this; m_Activity.runOnUiThread(new VideoSplashTask()); LayoutInflater inflater = LayoutInflater.from(m_Activity); m_rootLayout = (RelativeLayout)inflater.inflate(R.layout.activity_main, null); PrepareForStartLogo(); } private class VideoSplashTask implements Runnable { @Override public void run() { m_videoView = new VideoView((Context)m_Activity); m_videoView.setOnCompletionListener(MainActivity.m_Activity); m_videoView.setOnPreparedListener(MainActivity.m_Activity); Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.logo); m_videoView.setVideoURI(uri); LinearLayout ll = new LinearLayout(MainActivity.m_Activity); ll.setGravity(Gravity.CENTER); ll.setOrientation(LinearLayout.VERTICAL); ll.setBackgroundColor(Color.WHITE); ll.addView(m_videoView); m_Activity.addContentView(ll, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); m_videoView.setZOrderOnTop(true); m_videoView.requestFocus(); m_videoView.start(); } } @Override public void onCompletion(MediaPlayer arg0) { ViewParent layout = m_videoView.getParent(); ((ViewGroup)layout.getParent()).removeView((View)layout); m_videoView = null; m_Activity.runOnUiThread(m_UiThreadStartLogo); // destroy splash screen by view m_rootLayout.postDelayed(m_UiThreadStopLogo, 3000); } @Override public void onPrepared(MediaPlayer player) { int width = player.getVideoWidth(); int height = player.getVideoHeight(); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int screenWidth = dm.widthPixels;; int screenHeight = dm.heightPixels; // fill screen with the video float scale = Math.max((float)screenWidth / width, (float)screenHeight / height); LayoutParams lp = m_videoView.getLayoutParams(); lp.width = (int)Math.ceil(scale * width); lp.height = (int)Math.ceil(scale * height); m_videoView.setLayoutParams(lp); m_videoView.start(); } private void PrepareForStartLogo() { ViewFlipper mFlipper = (ViewFlipper)m_rootLayout.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(m_Activity); 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)m_rootLayout.findViewById(R.id.zz); zzImg.startAnimation(mZZ_animSet); } private final Runnable m_UiThreadStartLogo = new Runnable() { public void run() { m_Activity.addContentView(m_rootLayout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } }; private final Runnable m_UiThreadStopLogo = new Runnable() { public void run() { m_rootLayout.clearAnimation(); ViewGroup vg = (ViewGroup) (m_rootLayout.getParent()); vg.removeView(m_rootLayout); m_rootLayout = null; // add text view TextView text = new TextView(m_Activity); text.setText("End"); text.setGravity(Gravity.CENTER); m_Activity.addContentView(text, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); m_logoFinished = true; } }; @Override public void onBackPressed() { AlertDialog.Builder builder = new AlertDialog.Builder(m_Activity); builder.setMessage("确定退出吗?"); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { m_Activity.finish(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); if (m_logoFinished){ builder.show(); } }; @Override protected void onResume() { super.onResume(); if (m_videoView != null) { m_videoView.resume(); } } @Override protected void onPause() { super.onPause(); if (m_videoView != null) { m_videoView.pause(); } } }