Animation動畫2

AnimationSet的具體使用方法

 

       1.AnimationSetAnimation的子類;java

 

       2.一個AnimationSet包含了一系列的Animationandroid

 

       3.針對AnimationSet設置一些Animation的常見屬性(如startOffsetduration等),能夠被包含在AnimationSet當中的Animation集成;app

 

例:一個AnimationSet中有兩個Animation,效果疊加框架

 

第一種方法:dom

 

doubleani.xmlide

 

<?xml version="1.0" encoding="utf-8"?>佈局

<set xmlns:android="http://schemas.android.com/apk/res/android"測試

    android:interpolator="@android :anim/accelerate_interpolator"動畫

    android:shareInterpolator="true">this

    <!-- fromAlphatoAlpha是起始透明度和結束時透明度 -->

    <alpha

        android:fromAlpha="1.0"

        android:toAlpha="0.0"

        android:startOffset="500"

        android:duration="500"/>

    <translate

        android:fromXDelta="0%"

        android:toXDelta="100%"

        android:fromYDelta="0%"

        android:toYDelta="100%"

        android:duration="2000"/>

</set>

 

.java文件中

 

classDoubleButtonListener implements OnClickListener {

       public void onClick(View v) {

           // 使用AnimationUtils裝載動畫配置文件

           Animation animation = AnimationUtils.loadAnimation(

                  Animation2Activity.this, R.anim. doubleani);

           // 啓動動畫

           image.startAnimation(animation);

       }

    }

 

第二種方法:

 

.java文件中

 

classDoubleButtonListener implements OnClickListener {

       public void onClick(View v) {

           AnimationSet animationSet = new AnimationSet(true);

           AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);

           RotateAnimation rotateAnimation = new RotateAnimation(0, 360,

                  Animation.RELATIVE_TO_SELF,0.5f,

                  Animation.RELATIVE_TO_SELF,0.5f);

           rotateAnimation.setDuration(1000);

           animationSet.addAnimation(rotateAnimation);

           animationSet.addAnimation(alphaAnimation);

           image.startAnimation(animationSet);

 

       }

    }

 

Interpolator的具體使用方法

 

       Interpolator定義了動畫變化的速率,在Animations框架當中定義了一下幾種Interpolator

?         AccelerateDecelerateInterpolator:在動畫開始與結束的地方速率改變比較慢,在中間的時候速率快。

?         AccelerateInterpolator:在動畫開始的地方速率改變比較慢,而後開始加速

?         CycleInterpolator:動畫循環播放特定的次數,速率改變沿着正弦曲線

?         DecelerateInterpolator:在動畫開始的地方速率改變比較慢,而後開始減速

?         LinearInterpolator:動畫以均勻的速率改變

分爲如下幾種狀況:

1、在set標籤中

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android :anim/accelerate_interpolator"/>

2、若是在一個set標籤中包含多個動畫效果,若是想讓這些動畫效果共享一個Interpolator

    android:shareInterpolator="true"

3、若是不想共享一個interpolator,則設置android:shareInterpolator="true"而且須要在每個動畫效果處添加interpolator

<alpha

        android:interpolator="@android :anim/accelerate_decelerate_interpolator"

        android:fromAlpha="1.0"

        android:toAlpha="0.0"

        android:startOffset="500"

        android:duration="500"/>

 

4、若是是在代碼上設置共享一個interpolator則能夠在AnimationSet設置interpolator

 

AnimationSet animationSet = newAnimationSet(true);

animationSet.setInterpolator(new AccelerateInterpolator());

 

5、如果不設置共享一個interpolator則能夠在每個Animation對象上面設置interpolator

 

AnimationSet animationSet = newAnimationSet(false);

alphaAnimation.setInterpolator(new AccelerateInterpolator());

rotateAnimation.setInterpolator(new DecelerateInterpolator());

 

Frame-By-Frame Animations的使用方法

 

       Frame-By-Frame Animations是一幀一幀的格式顯示動畫效果。相似於電影膠片拍攝的手法。

 main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <LinearLayout

        android:orientation="horizontal"

        android:layout_height="wrap_content"

        android:layout_width="wrap_content">

       <Button

           android:id="@+id/button"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:text="運動"/>

    </LinearLayout>

    <LinearLayout

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent">

       <ImageView

           android:id="@+id/image"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_centerInParent="true"/>

    </LinearLayout>

</LinearLayout>

 

三、anim.xml

<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"

    android:oneshot="false">

    <item android:drawable="@drawable/a_01" android:duration="50"/>

    <item android:drawable="@drawable/a_02" android:duration="50"/>

    <item android:drawable="@drawable/a_03" android:duration="50"/>

    <item android:drawable="@drawable/a_04" android:duration="50"/>

    <item android:drawable="@drawable/a_05" android:duration="50"/>

    <item android:drawable="@drawable/a_06" android:duration="50"/>

</animation-list>

 

4.java文件

importandroid.app.Activity;

importandroid.graphics.drawable.AnimationDrawable;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.ImageView;

public class AnimationsActivity extends Activity {

    private Button button = null;

    private ImageView imageView = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        button = (Button)findViewById(R.id.button);

        imageView = (ImageView)findViewById(R.id.image);

        button.setOnClickListener(newButtonListener());

    }

    class ButtonListener implementsOnClickListener{

       public void onClick(View v) {

           imageView.setBackgroundResource(R.anim.anim);

           AnimationDrawable animationDrawable = (AnimationDrawable)

              imageView.getBackground();

           animationDrawable.start();

       }

    }

}

 

   LayoutAnimationsController

1、什麼是LayoutAnimationsController

LayoutAnimationsController能夠用於實現使多個控件按順序一個一個的顯示。

1)LayoutAnimationsController用於爲一個layout裏面的控件,或者是一個ViewGroup裏面的控件設置統一的動畫效果。

2)每個控件都有相同的動畫效果

3)控件的動畫效果能夠在不一樣的時間顯示出來

4)LayoutAnimationsController能夠在xml文件當中設置,以能夠在代碼當中進行設置。

2、在xml當中使用LayoutAnimationController

1)res/anim文件夾下建立一個名爲list_anim_layout.xml文件:

android:delay - 動畫間隔時間;子類動畫時間間隔 (延遲)   70% 也能夠是一個浮點數 如「1.2」等

android:animationOrder - 動畫執行的循序(normal:順序,random:隨機,reverse:反向顯示)

android:animation – 引用動畫效果文件

<layoutAnimation

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:delay="0.5"

    android:animationOrder="normal"

    android:animation="@anim/list_anim"/>

2)建立list_anim.xml文件,設置動畫效果

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/accelerate_interpolator"

    android:shareInterpolator="true">

    <alpha

       android:fromAlpha="0.0"

       android:toAlpha="1.0"

       android:duration="1000"/>

</set>

 

3在佈局文件main.xml當中爲ListVIew添加以下配置

<ListView

       android:id="@id/android:list"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:scrollbars="vertical"

        android:layoutAnimation="@anim/list_anim_layout"/>

4)程序結構

 

 

 

5)list_anim_layout.xml

<layoutAnimation

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:delay="0.5"

    android:animationOrder="normal"

    android:animation="@anim/list_anim"/>

6)list_anim.xml

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"

    android:interpolator="@android:anim/accelerate_interpolator"

    android:shareInterpolator="true">

    <alpha

       android:fromAlpha="0.0"

       android:toAlpha="1.0"

       android:duration="1000"/>

</set>

 

7)main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <ListView

       android:id="@id/android:list"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:scrollbars="vertical"

        android:layoutAnimation="@anim/list_anim_layout"/>

    <Button

        android:id="@+id/button"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="測試"/>

</LinearLayout>

8)item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="horizontal"

    android:paddingLeft="10dip"

    android:paddingRight="10dip"

    android:paddingTop="1dip"

    android:paddingBottom="1dip">

    <TextView android:id="@+id/name"

       android:layout_width="180dip"

       android:layout_height="30dip"

       android:textSize="7px"

       android:singleLine="true" />

    <TextView android:id="@+id/sex"

       android:layout_width="fill_parent"

       android:layout_height="fill_parent"

       android:textSize="7px"

       android:singleLine="true"/>

</LinearLayout>

9)java文件

public class Animation2Activity extendsListActivity {

    private Button button = null;

    private ListView listView = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        listView = getListView();

        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(newButtonListener());

    }

    private ListAdapter createListAdapter() {

       List<HashMap<String,String>> list =

           new ArrayList<HashMap<String,String>>();

       HashMap<String,String> m1 = new HashMap<String,String>();

       m1.put("name", "bauble");

       m1.put("sex", "male");

       HashMap<String,String> m2 = new HashMap<String,String>();

       m2.put("name", "Allorry");

       m2.put("sex", "male");

       HashMap<String,String> m3 = new HashMap<String,String>();

       m3.put("name", "Allotory");

       m3.put("sex", "male");

       HashMap<String,String> m4 = new HashMap<String,String>();

       m4.put("name", "boolbe");

       m4.put("sex", "male");

       list.add(m1);

       list.add(m2);

       list.add(m3);

       list.add(m4);

       SimpleAdapter simpleAdapter = new SimpleAdapter(

              this,list,R.layout.item,new String[]{"name","sex"},

              new int[]{R.id.name,R.id.sex});

       return simpleAdapter;

    }

    private class ButtonListener implementsOnClickListener{

       public void onClick(View v) {

           listView.setAdapter(createListAdapter());

       }

    }

}

 

備註:要將整個動畫效果設置到LinerLayout中,能夠這樣設置:

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:layoutAnimation="@anim/list_anim_layout"

 

3、在代碼當中使用LayoutAnimationController

1)去掉main.xml中的android:layoutAnimation="@anim/list_anim_layout"/>

2)建立一個Animation對象:能夠經過裝載xml文件,或者是直接使用Animation的構造方法建立Animation對象;

Animation animation = (Animation) AnimationUtils.loadAnimation(

                  Animation2Activity.this, R.anim.list_anim);

3)建立LayoutAnimationController對象:  

LayoutAnimationController controller = new LayoutAnimationController(animation); 

            

4)設置控件的顯示順序以及延遲時間

controller.setOrder(LayoutAnimationController.ORDER_NORMAL); 

controller.setDelay(0.5f);        

5)爲ListView設置LayoutAnimationController屬性:

listView.setLayoutAnimation(controller);

完整代碼:

private class ButtonListener implementsOnClickListener {

       public void onClick(View v) {

           listView.setAdapter(createListAdapter());

           Animation animation = (Animation) AnimationUtils.loadAnimation(

                  Animation2Activity.this, R.anim.list_anim);

          

           LayoutAnimationController controller = new LayoutAnimationController(animation); 

           controller.setOrder(LayoutAnimationController.ORDER_NORMAL); 

           controller.setDelay(0.5f);

           listView.setLayoutAnimation(controller); 

       }

    }

 

 AnimationListener

1、什麼是AnimationListener

1).AnimationListener是一個監聽器,該監聽器在動畫執行的各個階段會獲得通知,從而調用相應的方法;

2).AnimationListener主要包括以下三個方法:

n         ·onAnimationEnd(Animation animation) - 當動畫結束時調用

n         ·onAnimationRepeat(Animation animation) - 當動畫重複時調用

n         ·onAniamtionStart(Animation animation) - 當動畫啓動時調用

2、具體實現

1main.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/layout"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">

    <Button android:id="@+id/addButton"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:layout_alignParentBottom="true"

       android:text="添加圖片" />

    <Button android:id="@+id/deleteButton"

       android:layout_width="fill_parent"

       android:layout_height="wrap_content"

       android:layout_above="@id/addButton"

       android:text="刪除圖片" />

    <ImageView android:id="@+id/image"

       android:layout_width="wrap_content"

       android:layout_height="wrap_content"

       android:layout_centerInParent="true"

       android:layout_marginTop="100dip"

       android:src="@drawable/an" />

</RelativeLayout>

2).java文件

public class Animation2Activity extends Activity {

    private Button addButton = null;

    private Button deleteButton = null;

    private ImageView imageView = null;

    private ViewGroup viewGroup = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        addButton = (Button)findViewById(R.id.addButton);

        deleteButton = (Button)findViewById(R.id.deleteButton);

        imageView = (ImageView)findViewById(R.id.image);

        //LinearLayout下的一組控件

        viewGroup = (ViewGroup)findViewById(R.id.layout);

        addButton.setOnClickListener(newAddButtonListener());

        deleteButton.setOnClickListener(newDeleteButtonListener());

    }

    private class AddButtonListener implements OnClickListener{

       public void onClick(View v) {

           //淡入

           AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);

           animation.setDuration(1000);

           animation.setStartOffset(500);

           //建立一個新的ImageView

           ImageView newImageView = new ImageView(

              Animation2Activity.this);

           newImageView.setImageResource(R.drawable.an);

           viewGroup.addView(newImageView,

              new LayoutParams(

                  LayoutParams.FILL_PARENT,

                  LayoutParams.WRAP_CONTENT));

           newImageView.startAnimation(animation);

       }

    }

    private class DeleteButtonListener implements OnClickListener{

       public void onClick(View v) {

           //淡出

           AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);

           animation.setDuration(1000);

           animation.setStartOffset(500);

           //Aniamtion對象設置監聽器

           animation.setAnimationListener(

              new RemoveAnimationListener());

           imageView.startAnimation(animation);

       }

    }

    private class RemoveAnimationListener implements AnimationListener{

       //動畫效果執行完時remove

       public void onAnimationEnd(Animation animation) {

           System.out.println("onAnimationEnd");

           viewGroup.removeView(imageView);

       }

       public void onAnimationRepeat(Animation animation) {

           System.out.println("onAnimationRepeat");

       }

       public void onAnimationStart(Animation animation) {

           System.out.println("onAnimationStart");

       }

    }

}

3、總結一下

能夠在Activity中動態添加和刪除控件,方法是:

1)取到那個Layout

viewGroup = (ViewGroup)findViewById(R.id.layout);

2)添加時,先建立對象,而後添加

ImageView newImageView = new ImageView(

              Animation2Activity.this);

newImageView.setImageResource(R.drawable.an);

viewGroup.addView(newImageView,

              new LayoutParams(

                  LayoutParams.FILL_PARENT,

                  LayoutParams.WRAP_CONTENT));

3)刪除時,直接刪除。

viewGroup.removeView(imageView);

相關文章
相關標籤/搜索