Android动画Animation

Posted

tags:

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

第一种:代码格式:

技术分享
package com.itzb.annimation;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
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.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private Button btnxz;
    private Button btnsf;
    private Button btnrc;
    private Button btnpy;
    private LinearLayout ll;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnxz = (Button) findViewById(R.id.btnxz);
        btnsf = (Button) findViewById(R.id.btnsf);
        btnrc = (Button) findViewById(R.id.btnrc);
        btnpy = (Button) findViewById(R.id.btnyd);
        ll = (LinearLayout) findViewById(R.id.ll);
        btnxz.setOnClickListener(this);
        btnsf.setOnClickListener(this);
        btnrc.setOnClickListener(this);
        btnpy.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == btnxz.getId()) {
            rotaAnimationShow();
        } else if (id == btnsf.getId()) {
            scalAnimationShow();
        } else if (id == btnrc.getId()) {
            alphaAnimationShow();
        } else if (id == btnpy.getId()) {
            translateAnimationShow();
        }
    }

    // 渐入渐出动画
    public void alphaAnimationShow() {
        AlphaAnimation aa = new AlphaAnimation(0.1f, 1.0f);// 参数表示,透明度从0.1到1
        aa.setDuration(3000);// 设置动画时间。
        aa.setFillAfter(true);// 设置结果状态。不设置也行。
        ll.startAnimation(aa);// 启动动画
        // 其中AlphaAnimation类第一个参数fromAlpha表示动画起始时的透明度, 第二个参数toAlpha表示动画结束时的透明度。
    }

    // 旋转动画
    public void rotaAnimationShow() {
        // 其中RotateAnimation类第一个参数fromDegrees表示动画起始时的角度,
        // 第二个参数toDegrees表示动画结束时的角度。第三个第四个表示中心点旋转。
        // 另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y
        // 坐标的开始位置pivotXValue、pivotYValue等。
        RotateAnimation aa = new RotateAnimation(0f, 360f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        aa.setDuration(3000);
        ll.startAnimation(aa);// 启动动画
    }

    // 缩放动画
    public void scalAnimationShow() {
        /*
         * 第一个参数fromX ,第二个参数toX:分别是动画起始、结束时X坐标上的伸缩尺寸。 第三个参数fromY
         * ,第四个参数toY:分别是动画起始、结束时Y坐标上的伸缩尺寸。还可以加上锚点,Animation.RELATIVE_TO_SELF,
         * 0.5f, Animation.RELATIVE_TO_SELF, 0.5f
         * 另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y
         * 坐标的开始位置pivotXValue、pivotYValue等。
         */
        ScaleAnimation sa = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f);
        sa.setDuration(3000);
        ll.startAnimation(sa);
    }

    // 位移动画
    public void translateAnimationShow() {
        /*
         * 第一个参数fromXDelta ,第二个参数toXDelta:分别是动画起始、结束时X坐标。 第三个参数fromYDelta
         * ,第四个参数toYDelta:分别是动画起始、结束时Y坐标。
         */
        TranslateAnimation ta = new TranslateAnimation(0.1f, 100.0f, 0.1f,
                100.0f);
        ta.setDuration(3000);
        ll.startAnimation(ta);
    }

    // 动画集
    public void animationSetShow() {
        AlphaAnimation aa = new AlphaAnimation(0.1f, 1.0f);// 参数表示,透明度从0.1到1
        aa.setDuration(3000);// 设置动画时间。
        RotateAnimation ra = new RotateAnimation(0f, 360f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        AnimationSet as = new AnimationSet(true);
        as.addAnimation(aa);
        as.addAnimation(ra);
        ll.startAnimation(as);
    }
}
View Code

第二种:xml文件格式:

1.创建文件夹anim在res路径下。在anim中创建xml文件

如:

技术分享
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    >

    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="100%"
        android:toYDelta="0" >
    </translate>

    <alpha
        android:duration="500"
        android:fromAlpha="0"
        android:toAlpha="1" >
    </alpha>

</set>
View Code

2.在需要动画的地方写下列代码:

overridePendingTransition(R.anim.next_in, R.anim.next_out)

 

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

VSCode自定义代码片段7——CSS动画

VSCode自定义代码片段7——CSS动画

Android动画Drawable Animation

Android动画-Property Animation

Android动画Animation

Unity -- Animation(旧版动画组件)和Animator(新版动画器组件)