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

Android滑动动画ViewFlipper和视频播放VideoView的使用的更多相关文章

  1. Android 滑动效果入门篇(一)—— ViewFlipper

    ViewFilpper 是Android官方提供的一个View容器类,继承于ViewAnimator类,用于实现页面切换,也可以设定时间间隔,让它自动播放.又ViewAnimator继承至于Frame ...

  2. Android 实现左右滑动效果ViewFlipper终结【转】

    本示例演示在Android中实现图片左右滑动效果.   关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现.接下来 ...

  3. Android的Activity屏幕切换滑动动画

    Activity的切换效果使用的是Android的动画效果,Android的动画在官方有相关资料:http://developer.android.com/guide/topics/graphics/ ...

  4. android仿微信红包动画、Kotlin综合应用、Xposed模块、炫酷下拉视觉、UC浏览器滑动动画等源码

    Android精选源码 仿微信打开红包旋转动画 使用Kotlin编写的Android应用,内容你想象不到 Android手机上的免Root Android系统日志Viewer 一个能让微信 Mater ...

  5. Android Animation动画实战(一): 从布局动画引入ListView滑动时,每一Item项的显示动画

    前言: 之前,我已经写了两篇博文,给大家介绍了Android的基础动画是如何实现的,如果还不清楚的,可以点击查看:Android Animation动画详解(一): 补间动画 及 Android An ...

  6. Android中使用ViewFlipper实现屏幕页面切换(关于坐标轴的问题已补充更改)

    屏幕切换指的是在同一个Activity内屏幕间的切换,ViewFlipper继承了Framelayout类,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果.如 ...

  7. 十六、Android 滑动效果汇总

    Android 滑动效果入门篇(一)—— ViewFlipper Android 滑动效果入门篇(二)—— Gallery Android 滑动效果基础篇(三)—— Gallery仿图像集浏览 And ...

  8. Android 滑动效果汇总

    Android 滑动效果入门篇(一)—— ViewFlipper Android 滑动效果入门篇(二)—— Gallery Android 滑动效果基础篇(三)—— Gallery仿图像集浏览 And ...

  9. Xamarin.Android之动画

    Translate动画 这个动画是最常使用到的,主要就是将控件从一个位置移动到另一个位置,并且还可以在这其中增加一定的效果,下面我们将采用两种方式实现动画,首选的是利用XML来制作动画,其次就是利用代 ...

随机推荐

  1. Failed to allocate memory: 8

    Failed to allocate memory: 8This application has requested the Runtime to terminate it in an unusual ...

  2. OpenCV码源笔记——Decision Tree决策树

    来自OpenCV2.3.1 sample/c/mushroom.cpp 1.首先读入agaricus-lepiota.data的训练样本. 样本中第一项是e或p代表有毒或无毒的标志位:其他是特征,可以 ...

  3. 即时通信Spark安装和配置

    spark:Cross-platform real-time collaboration client optimized for business and organizations.Spark i ...

  4. lightOJ 1172 Krypton Number System(矩阵+DP)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1172 题意:一个n进制(2<=n<=6)的数字,满足以下条件:(1)至少包 ...

  5. java开发之匿名内部类,接口的使用

    下面的例子是Java.JDK7学习笔记上的 打算开发多人联机程序,对每个联机客户端,都会建立Client对象封装相关信息 1.Client.java public class Client { pri ...

  6. [HDOJ4578]Transformation(线段树,多延迟标记)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4578 四种操作:查询.加法.乘法.改数.应该是需要维护三个lazy标记,然后就是套路了.查询是区间内所 ...

  7. cf 151 C. Win or Freeze (博弈 求大数质因子)

    题目 题意: 给一个数N,两人轮流操作每次将N变为一个N的非1非自身的因数,第一个无法进行操作的人获胜问先手是否有必胜策略,如果有的话在第二行输出第一步换成哪个数,如果第一步就不能操作则输出0数据规模 ...

  8. java实现DES算法

    import java.util.UUID; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypt ...

  9. Asp.Net时间戳与时间互转

    /// <summary> /// 时间戳转成时间类型 /// </summary> /// <param name="timeStamp">& ...

  10. Web Api 如何做上传文件的单元测试

    代码如下: //--------上传------------ HttpClient client = new HttpClient(); #region MultipartFormDataConten ...