天天看點

android幀動畫代碼示例

package com.luozhikai.demo.viewanimation;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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.ImageView;

public class MainActivity extends Activity {

private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
}

public void alpha(View view){
        AlphaAnimation aa = new AlphaAnimation(0.0f,1.0f);
aa.setDuration(2000);//動畫播放的時間長度
        //aa.setRepeatCount(1);//設定重複的次數,為1時共播放兩次
aa.setRepeatCount(Animation.INFINITE);//無限次
aa.setRepeatMode(Animation.REVERSE);//設定重複播放的模式:restart重新播放,reverse倒着播放

        //開啟動畫
iv.startAnimation(aa);
}

public void trans(View view){
        TranslateAnimation ta = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,1f,
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,1f);

ta.setDuration(2000);
ta.setRepeatCount(Animation.INFINITE);
ta.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ta);
}

public void scale(View view){
        ScaleAnimation sa = new ScaleAnimation(0.2f,2.0f,0.2f,2.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

sa.setDuration(2000);
sa.setRepeatCount(Animation.INFINITE);
sa.setRepeatMode(Animation.REVERSE);
iv.startAnimation(sa);
}

public void rotate(View view){
        RotateAnimation ra = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

ra.setDuration(2000);
ra.setRepeatCount(Animation.INFINITE);
ra.setRepeatMode(Animation.REVERSE);
iv.startAnimation(ra);
}

public void all(View view){
        AlphaAnimation aa = new AlphaAnimation(0.0f,1.0f);
aa.setDuration(2000);//動畫播放的時間長度
        //aa.setRepeatCount(1);//設定重複的次數,為1時共播放兩次
aa.setRepeatCount(Animation.INFINITE);//無限次
aa.setRepeatMode(Animation.REVERSE);//設定重複播放的模式:restart重新播放,reverse倒着播放

TranslateAnimation ta = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,1f,
Animation.RELATIVE_TO_SELF,0,
Animation.RELATIVE_TO_SELF,1f);

ta.setDuration(2000);
ta.setRepeatCount(Animation.INFINITE);
ta.setRepeatMode(Animation.REVERSE);

ScaleAnimation sa = new ScaleAnimation(0.2f,2.0f,0.2f,2.0f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

sa.setDuration(2000);
sa.setRepeatCount(Animation.INFINITE);
sa.setRepeatMode(Animation.REVERSE);

RotateAnimation ra = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

ra.setDuration(2000);
ra.setRepeatCount(Animation.INFINITE);
ra.setRepeatMode(Animation.REVERSE);

//建立動畫集合

AnimationSet set = new AnimationSet(false);//是否統一格式來播放動畫,true是,false按各自的動畫效果播放

set.addAnimation(aa);
set.addAnimation(ta);
set.addAnimation(sa);
set.addAnimation(ra);

iv.startAnimation(set);
}
}      

layout檔案

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

    <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

        <Button
android:onClick="alpha"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="alpha" />

        <Button
android:onClick="trans"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="trans" />

        <Button
android:onClick="scale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="scale" />

        <Button
android:onClick="rotate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="rotate" />


        <Button
android:onClick="all"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="all" />
    </LinearLayout>

    <ImageView
android:id="@+id/iv"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerInParent="true"
android:src="@drawable/pic" />
</RelativeLayout>      

繼續閱讀