Android插屏动画效果

Posted brucemengbm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android插屏动画效果相关的知识,希望对你有一定的参考价值。

公司研发SDK,须要类似有米插屏的动画效果,研究了下,写了一个DEMO,凝视非常具体了。

技术分享

<span style="font-size:24px;">package com.example.animationactivity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimato
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

@SuppressLint("NewApi")
public class MainActivity extends ActionBarActivity {
	private Button button;
	ImageView image;
	private int a = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) findViewById(R.id.button);

		image = new ImageView(getApplicationContext());
		image.setImageDrawable(getResources().getDrawable(R.drawable.bg1));
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				(int) (getWidth(getApplicationContext()) * 0.8),
				(int) (getHeight(getApplicationContext()) * 0.8));
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
		RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.parent);
		image.setLayoutParams(params);
		relativeLayout.addView(image); 
        /**
		// 动画1
		final Animation animation = new TranslateAnimation(-600f, 0f, 0f, 0f);

		final Animation scaleAnimation = new ScaleAnimation(0.2f, 1f, 0.2f, 1f);

		final AnimationSet animationSet = new AnimationSet(true);
		animationSet.addAnimation(scaleAnimation);
		animationSet.addAnimation(animation);
		animationSet.setDuration(1000);

		Animation translationanimation = new TranslateAnimation(0f, 1800f, 0f,
				0f);

		Animation scaleAnimation2 = new ScaleAnimation(1f, 0.4f, 1f, 0.4f);
		final AnimationSet animationSet2 = new AnimationSet(true);
		animationSet2.addAnimation(translationanimation);
		animationSet2.addAnimation(scaleAnimation2);
		animationSet2.setDuration(1000);

		animationSet2.setFillAfter(true);
       **/
		button.setOnClickListener(new OnClickListener() {

			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 自己写的
				/**
				 * if (a == 1) { a = 2; image.startAnimation(animationSet); }
				 * else { a = 1; image.startAnimation(animationSet2); }
				 **/

				// 从有米那里拷过来的动画
				if (a == 1) {
					EnterAnimation(image, getApplicationContext());
					a = 2;
				} else {
					BackAnimation(image, getApplicationContext());
					a = 1;
				}
			}
		});
	}

	// 获取屏幕的宽度
	public static int getWidth(Context context) {
		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		int width = windowManager.getDefaultDisplay().getWidth();
		return width;

	}

	// 获取屏幕的高度
	public int getHeight(Context context) {
		WindowManager windowManager = (WindowManager) context
				.getSystemService(Context.WINDOW_SERVICE);
		int height = windowManager.getDefaultDisplay().getHeight();
		return height;

	}

	// 出去动画

	public static void BackAnimation(ImageView paramView, Context context) {
		int f = (getWidth(context) - paramView.getWidth()) / 2;

		paramView.setPivotX(paramView.getWidth() / 2);
		paramView.setPivotY(paramView.getHeight() / 2);
		ObjectAnimator localObjectAnimator1 = ObjectAnimator.ofFloat(paramView,
				"scaleX", new float[] { 1.0F, 0.8F }).setDuration(1L);
		ObjectAnimator localObjectAnimator2 = ObjectAnimator.ofFloat(paramView,
				"scaleY", new float[] { 1.0F, 0.8F }).setDuration(1L);
		ObjectAnimator localObjectAnimator3 = ObjectAnimator.ofFloat(paramView,
				"alpha", new float[] { 1.0F, 0.7F }).setDuration(1L);
		ObjectAnimator localObjectAnimator4 = ObjectAnimator.ofFloat(paramView,
				"X", new float[] { f, getWidth(context) }).setDuration(800L);
		ObjectAnimator localObjectAnimator5 = ObjectAnimator.ofFloat(paramView,
				"scaleX", new float[] { 0.8F, 1.0F }).setDuration(400L);
		localObjectAnimator5.setStartDelay(400L);
		ObjectAnimator localObjectAnimator6 = ObjectAnimator.ofFloat(paramView,
				"scaleY", new float[] { 0.8F, 1.0F }).setDuration(400L);
		localObjectAnimator6.setStartDelay(400L);
		ObjectAnimator localObjectAnimator7 = ObjectAnimator.ofFloat(paramView,
				"alpha", new float[] { 0.7F, 1.0F }).setDuration(400L);
		localObjectAnimator7.setStartDelay(400L);
		AnimatorSet localAnimatorSet = new AnimatorSet();
		localAnimatorSet.playTogether(new Animator[] { localObjectAnimator1,
				localObjectAnimator2, localObjectAnimator3,
				localObjectAnimator4, localObjectAnimator5,
				localObjectAnimator6, localObjectAnimator7 });
		localAnimatorSet.start();

	}

	// 进入动画
	public static void EnterAnimation(ImageView paramView, Context context) {
		// 屏幕的宽度减去动画的宽度。就是图片要移动的距离
		int f = (getWidth(context) - paramView.getWidth()) / 2;
		paramView.setPivotX(paramView.getWidth() / 2);
		paramView.setPivotY(paramView.getHeight() / 2);
		ObjectAnimator localObjectAnimator1 = ObjectAnimator.ofFloat(paramView,
				"scaleX", new float[] { 1.0F, 0.8F }).setDuration(1L);
		ObjectAnimator localObjectAnimator2 = ObjectAnimator.ofFloat(paramView,
				"scaleY", new float[] { 1.0F, 0.8F }).setDuration(1L);
		ObjectAnimator localObjectAnimator3 = ObjectAnimator.ofFloat(paramView,
				"alpha", new float[] { 1.0F, 0.7F }).setDuration(1L);
		ObjectAnimator localObjectAnimator4 = ObjectAnimator.ofFloat(paramView,
				"X", new float[] { -paramView.getWidth(), f })
				.setDuration(800L);
		ObjectAnimator localObjectAnimator5 = ObjectAnimator.ofFloat(paramView,
				"scaleX", new float[] { 0.8F, 1.0F }).setDuration(400L);
		localObjectAnimator5.setStartDelay(400L);
		ObjectAnimator localObjectAnimator6 = ObjectAnimator.ofFloat(paramView,
				"scaleY", new float[] { 0.8F, 1.0F }).setDuration(400L);
		localObjectAnimator6.setStartDelay(400L);
		ObjectAnimator localObjectAnimator7 = ObjectAnimator.ofFloat(paramView,
				"alpha", new float[] { 0.7F, 1.0F }).setDuration(400L);
		localObjectAnimator7.setStartDelay(400L);
		AnimatorSet localAnimatorSet = new AnimatorSet();
		localAnimatorSet.playTogether(new Animator[] { localObjectAnimator1,
				localObjectAnimator2, localObjectAnimator3,
				localObjectAnimator4, localObjectAnimator5,
				localObjectAnimator6, localObjectAnimator7 });
		localAnimatorSet.start();

	}

}</span><span style="font-size:18px;">
</span>



以上是关于Android插屏动画效果的主要内容,如果未能解决你的问题,请参考以下文章

Android 动画嵌套片段

Android使用片段在viewpager中的页面滚动上放置动画

Android分别通过代码和xml实现动画效果

Android 动画布局和视图

如何在Android中加载带有动画的cardview GridView?

Android——浅析Activity过渡动画