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. Android Handler使用实例

    本文主要介绍Android中Handler的简单使用方法,Handler跟多线程,消息队列联系很紧密,在平常的实际程序开发中比较常见.本文分为4个简单的例子来学校handler Handler使用例1 ...

  2. Java API —— 异常

    1.异常:异常就是Java程序在运行过程中出现的错误. 2.异常由来:问题也是现实生活中一个具体事务,也可以通过java 的类的形式进行描述,并封装成对象.其实就是Java对不正常情况进行描述后的对象 ...

  3. C# progressbar 用法

    http://blog.chinaunix.net/uid-9236609-id-3069624.html progressBar1.Maximum = 100;//设置最大长度值          ...

  4. POJ 1681 Painter's Problem (高斯消元 枚举自由变元求最小的步数)

    题目链接 题意: 一个n*n 的木板 ,每个格子 都 可以 染成 白色和黄色,( 一旦我们对也个格子染色 ,他的上下左右 都将改变颜色): 给定一个初始状态 , 求将 所有的 格子 染成黄色 最少需要 ...

  5. Android提供的LruCache类简介

    分类: Android开发 2013-02-06 15:26 26733人阅读 评论(10) 收藏 举报 package android.util; import import /** * A cac ...

  6. Android中View转换为Bitmap及getDrawingCache=null的解决方法

    1.前言 Android中经常会遇到把View转换为Bitmap的情形,比如,对整个屏幕视图进行截屏并生成图片:Coverflow中需要把一页一 页的view转换为Bitmap.以便实现复杂的图形效果 ...

  7. BootStrap图标

  8. 本来运行的好的Ajax.dll怎么突然不起作用了

    客户中有个好多年前老项目用了Ajax.dll,前几天突然不起作用了. 问了下原因,系统从Windows Server2003 升级为 Windows Server 2008了,IIS也从6升级成7了. ...

  9. UVa 10088 (Pick定理) Trees on My Island

    这种1A的感觉真好 #include <cstdio> #include <vector> #include <cmath> using namespace std ...

  10. 转:MVC3系列:~Html.BeginForm与Ajax.BeginForm

    Html.BeginForm与Ajax.BeginForm都是MVC架构中的表单元素,它们从字面上可以看到区别,即Html.BeginForm是普通的表单提交,而Ajax.BeginForm是支持异步 ...