android 之 animations 動畫,androidanimations

來源:互聯網
上載者:User

android 之 animations 動畫,androidanimations

android 提供的了兩種機制你可以用來建立簡單的動畫:tweedned animation(漸層動畫) 和 frame-by-frame animation(逐幀動畫)(有道翻譯的,汗!!!) 。這裡主要介紹tweedned animation中的四種動畫形式:alpha(淡入淡出效果)、scale(放縮)、rotate(旋轉) 、translate(平移)。

那麼怎麼來實現這樣的效果呢?大家向下看:(這裡是在代碼內部實現,雖然容易看,但是不利於維護和重用,下面還會介紹一種在外部實現的方法)

內部實現:

1.建立一個AnimationSet對象

2.建立一個你想要實現效果的Animation對象,如AlphaAnimations

3.為動畫設定一些屬性

4.將alpha對象加入到AnimationSet中

5.對圖片開始執行AnimationSet

    private class alphaButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            //建立一個AnimationSet對象            AnimationSet animationSet = new AnimationSet(true);            //建立一個AlphaAnimation對象,設定透明度為由1到0            AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);            //設定整個動畫持續的時間            alphaAnimation.setDuration(10000);            //將Animation對象添加到animationSet對象中            animationSet.addAnimation(alphaAnimation);            //對圖片開始執行AnimationSet            imagView.startAnimation(animationSet);        }    }

=========================

這裡介紹一些常用的動畫屬性的設定

  • setDuration(long durationMillis):用於設定動畫的期間,單位是毫秒。
  • setFillAfter:設定為true後,該動畫專化在動畫結束後應用,也就是說,動畫將會停留在結束時的狀態
  • setFillBefore:設定為true後,在動畫開始前應用動畫轉化。(這個系統預設是設定為true的)(這個我有點不太懂,網上有的說是設定動畫完成後回到起始位置,好像不太對,如果你知道的話,一定要留言告訴我)
  • setStartOffset:設定當發出開始動畫的指令後經過多長時間開始執行動畫
  • setRepeatMode:設定動畫的重複次數

===========================

下面是具體的ManiActivity.java代碼,對各個動畫都有詳細的解釋:

package com.mecury.animationstest;import android.app.Activity;import android.os.Bundle;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.ImageView;public class MainActivity extends Activity {    private ImageView imagView;    private Button alphaButton;    private Button scaleButton;    private Button rotateButton;    private Button translateButton;        @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);                imagView = (ImageView) findViewById(R.id.imageView);        alphaButton = (Button) findViewById(R.id.alphaButton);        alphaButton.setOnClickListener(new alphaButtonListener());        scaleButton = (Button) findViewById(R.id.scaleButton);        scaleButton.setOnClickListener(new scaleButtonListener());        rotateButton = (Button) findViewById(R.id.rotateButton);        rotateButton.setOnClickListener(new rotateButtonListener());        translateButton = (Button) findViewById(R.id.translateButton);        translateButton.setOnClickListener(new translateButtonListener());    }        /*     * 淡入淡出效果     */    private class alphaButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            //建立一個AnimationSet對象            AnimationSet animationSet = new AnimationSet(true);            //建立一個AlphaAnimation對象,設定透明度為由1到0            AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);            //設定整個動畫持續的時間            alphaAnimation.setDuration(10000);            //將Animation對象添加到animationSet對象中            animationSet.addAnimation(alphaAnimation);            //對圖片開始執行AnimationSet            imagView.startAnimation(animationSet);        }    }        /*     * 縮放效果     */    private class scaleButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            AnimationSet animationSet = new AnimationSet(true);            /*這裡用的是百分比標記法,f表示浮點型             * 四個參數代表縮放的比例,前兩個代表了x軸的縮放(這裡用的是百分比標記法:1f代表原來的寬度,0.5f代表原來寬度的一半)後兩個代表了y軸的縮放             * Animation中有兩個常用的屬性,RELATIVE_TO_SELF(依賴自身),RELATIVE_TO_PARENT(依賴父控制項)             * 後面四個表明縮放的軸為依賴自身的在x為0.5f,y為0.5f的地方             */            ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 0.1f, 1f, 0.1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);            animationSet.addAnimation(scaleAnimation);            animationSet.setDuration(2000);                        imagView.startAnimation(animationSet);        }    }        /*     * 旋轉效果     */    private class rotateButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            AnimationSet animationSet = new AnimationSet(true);            /*第一個參數,表示圖片相對於起始位置的角度,             *第二個參數:表示動畫結束時圖片的偏轉角度。這裡就是由0變到360即原點             *後面的四個參數是為了設定圖片旋轉圍繞的軸,意思是軸的橫座標是依賴父控制項的0f處,縱座標是依賴父控制項的-1f處             *這裡要著中說的是:關於座標的問題。這這個動畫中,座標原點是圖片的左上方,原點左x軸和下y軸代表正軸             *此時的圍繞的點的座標軸就在圖片的正上方             */             RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, -1f);            rotateAnimation.setDuration(2000);            animationSet.addAnimation(rotateAnimation);            imagView.startAnimation(animationSet);        }    }        /*     * 移動效果     */    private class translateButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            AnimationSet animationSet = new AnimationSet(true);            /*             * 前兩個參數決定了動畫開始時圖片的x軸起始位置             * 第三四個參數表示圖片將要到達位置的x軸             * 後面四個是表示y軸的和上面類似             * 代碼中表示:由起始位置位移到右上方             */            TranslateAnimation translateAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f,                     Animation.RELATIVE_TO_PARENT, 1f,                     Animation.RELATIVE_TO_SELF,0f,                    Animation.RELATIVE_TO_PARENT, -1f);            translateAnimation.setDuration(2000);            animationSet.addAnimation(translateAnimation);            imagView.startAnimation(animationSet);        }    }}

 

下面是我的布局檔案:

<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="com.mecury.animationstest.MainActivity" >        <Button         android:id="@+id/translateButton"        android:layout_width="match_parent"        android:layout_height="40dp"        android:layout_alignParentBottom="true"        android:text="Translate平移"/>    <Button         android:id="@+id/rotateButton"        android:layout_width="match_parent"        android:layout_height="40dp"        android:layout_above="@id/translateButton"        android:text="Rotate旋轉"/>    <Button         android:id="@+id/scaleButton"        android:layout_width="match_parent"        android:layout_height="40dp"        android:layout_above="@id/rotateButton"        android:text="Scale縮放"/>    <Button         android:id="@+id/alphaButton"        android:layout_width="match_parent"        android:layout_height="40dp"        android:layout_above="@id/scaleButton"        android:text="Alpha淡入淡出"/>        <ImageView         android:id="@+id/imageView"        android:layout_width="50dp"        android:layout_height="50dp"     android:layout_centerInParent="true"        android:src="@drawable/china"/>    </RelativeLayout>    

 

在外部資源中實現:

1.在res檔案中建立一個名為anim的檔案

2.在檔案中中建立.xml檔案(這裡以alpha為例),如alpha.xml

3.在檔案中設定相應的屬性

4.使用AnimationUtils來裝載動畫設定檔案

這裡我的anim中四個動畫代碼檔案:

<?xml version="1.0" encoding="utf-8"?><!-- aplha是一個淡入淡出的效果startOffser時間位移量,在動畫開始之前,停頓500毫秒 --><set xmlns:android="http://schemas.android.com/apk/res/android">    <alpha        android:fromAlpha="1"        android:toAlpha="0"        android:startOffset="500"        android:duration="2000"/></set><?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <!-- scale是實現縮放     privat 是設定縮放圍繞的軸的縮放    privatX="50"使用的是絕對位置    ="%50"相對控制項本身定位,在控制項的1/2處    ="50%p" 相對於父控制項的位置定位-->    <scale         android:fromXScale="1"        android:toXScale="0.1"        android:fromYScale="1"        android:toYScale="0.1"        android:pivotX="50%"        android:pivotY="50%"        android:startOffset="500"        android:duration="2000"/></set><?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <rotate         android:fromDegrees="0"        android:toDegrees="+360"        android:pivotX="100%p"        android:pivotY="0%p"        android:startOffset="500"        android:duration="2000"/></set><?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate         android:fromXDelta="0%"        android:toXDelta="100%p"        android:fromYDelta="0%"        android:toYDelta="-100%p"        android:interpolator="@android:anim/decelerate_interpolator"        android:startOffset="500"        android:duration="30000"        /></set>

 

intepolator是用來設定動畫執行的速度

Accelerate_Decelerate_Interpolator:在動畫開始和結束時速率較慢,中間加速

Accelerate_Interpolator:在動畫開始的時候速度較慢,逐漸加速

Cycle_Interpolator:速度改變沿著動畫迴圈播放的次數,以正弦曲線式變化

Deceletrate_Interpolator:在的動畫開始是速度改變比較慢,然後突然減速

Linear_Interpolator:動畫勻速改變

使用:android:Interpolator="@android:anim/....."

mianActivity.java:

 

package com.example.animation_01;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {    private ImageView imagView;    private Button alphaButton;    private Button scaleButton;    private Button rotateButton;    private Button translateButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        imagView = (ImageView) findViewById(R.id.imageView);        alphaButton = (Button) findViewById(R.id.alphaButton);        alphaButton.setOnClickListener(new alphaButtonListener());        scaleButton = (Button) findViewById(R.id.scaleButton);        scaleButton.setOnClickListener(new scaleButtonListener());        rotateButton = (Button) findViewById(R.id.rotateButton);        rotateButton.setOnClickListener(new rotateButtonListener());        translateButton = (Button) findViewById(R.id.translateButton);        translateButton.setOnClickListener(new translateButtonListener());    }        class alphaButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            //使用AnimaionUtils來裝載動畫設定檔案            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);            imagView.startAnimation(animation);        }    }    class scaleButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale);            imagView.startAnimation(animation);        }    }    class rotateButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.rotate);            imagView.startAnimation(animation);        }    }    class translateButtonListener implements OnClickListener{        @Override        public void onClick(View v) {            Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.translate);            imagView.startAnimation(animation);        }    }}

 

 

 

 

 布局檔案和第一個一樣。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.