Android动画---视图动画 (一)

Android 视图动画分为:

  • 透明度动画 AlphaAnimation
  • 旋转动画 RotateAnimation
  • 位移动画 TranslateAnimation
  • 缩放动画 ScaleAnimation 

每一个动画的参数比较容易理解,这里我们直接使用一个例子来验证一下。

首先我们直接使用Java 代码来实现:

首先创建一个AnimSetDemo 的工程。

activity_main.xml 中添加一些美美的布局和图片资源

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rela_root"
    android:background="@drawable/splash_bg_newyear"
    tools:context="com.zx.animsetdemo.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/splash_horse_newyear" />

</RelativeLayout>

主工程中使用代码来写些简单的动画。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout rela_root = (RelativeLayout) findViewById(R.id.rela_root);
        // 旋转
        RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f	, Animation.RELATIVE_TO_SELF, 0.5f);
        rotateAnimation.setDuration(1000);
        rotateAnimation.setFillAfter(true);
        // 透明
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        alphaAnimation.setDuration(1000);
        alphaAnimation.setFillAfter(true);
        // 缩放
        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,	0.5f);
        scaleAnimation.setDuration(1000);
        scaleAnimation.setFillAfter(true);

        AnimationSet animationSet = new AnimationSet(true);
        animationSet.addAnimation(rotateAnimation);
        animationSet.addAnimation(scaleAnimation);
        animationSet.addAnimation(alphaAnimation);

        rela_root.startAnimation(animationSet);

    }

}

三种单独的动画,最中通过AnimationSet 来进行加载。

然后主工程的清单文件中设置主题是:

android:theme="@style/Theme.AppCompat.NoActionBar"

不要actionBar 了这样动画看起来直接入眼。

还有一种方式就是通过xml 来定义动画集合,在工程的res 文件下创建amin 文件夹用来保存 我们设置的各种动画xml.

我们创建一个splash_anim.xml 的文件在 刚刚创建的amin 文件夹下。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0"/>
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%" />

    <scale
        android:duration="1000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0"/>
</set>

这样我们只要在MainActivity.java 中加载一下这个xml文件,就可以实现和java 代码一样的效果。

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout rela_root = (RelativeLayout) findViewById(R.id.rela_root);
        AnimationSet animationSet =(AnimationSet) AnimationUtils.loadAnimation(this,R.anim.splash_anim);

        rela_root.startAnimation(animationSet);

    }

}

通过 AnimationUtils 来加载动画集合,并返回一个 Animation,强制转为AnimationSet,动画集合。

最后运行我们的代码,效果如下:

demo 效果

如果我们需要在动画结束的时候去做一些事情,可以给动画设置监听

animationSet.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation animation) {
				
			}
			
			@Override
			public void onAnimationRepeat(Animation animation) {
				
			}
			
			@Override
			public void onAnimationEnd(Animation animation) {
				
			}
		});

平移动画可以参考其他动画的模式来实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值