android xml實現animation 4種動畫效果,androidanimation

來源:互聯網
上載者:User

android xml實現animation 4種動畫效果,androidanimation

animation有四種動畫類型 分別為alpha(透明的漸層)、rotate(旋轉)、scale(尺寸伸縮)、translate(移動),二實現的分發有兩種,一種是javaCode,另外一種是XML,而我今天要說的是XML實現的方法,個人感覺javaCode的實現方法比xml要簡單,所以有需要的可以自己去找找資料看看。下面是我的四個xml檔案,分別代表這四種動畫類型。

alpha.xml

 COde:

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">

    <!-- 漸層透明的動畫效果 -->
<!--fromAlpha 動畫起始透明的 1.0完全不透明
    toAlpha  動畫結束時透明的 0.0完全透明
    startOffset 設定啟動時間
    duration 屬性動畫期間
    -->
<alpha
    android:fromAlpha="1.0"
    android:toAlpha="0.0"
    android:startOffset="500"
    android:duration="5000"
    />
</set>

 

rotate.xml

 

  <?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
  <!-- 畫面轉移旋轉動畫效果 -->
    <!--
    fromDegrees開始角度
    toDegrees結束角度
    pivotX設定旋轉時的X軸座標
    -->
   <rotate
       android:fromDegrees="0"
       android:toDegrees="+360"
       android:pivotX="50%"
       android:pivotY="50%"
       android:duration="5000"
       />
    </set>

 

scale.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
<!-- 漸層尺寸伸縮動畫效果 -->
    <!--
         fromXScale 起始x軸座標
           toXScale 止x軸座標
         fromYScale 起始y軸座標
           toYScale 止y軸座標
           pivotX 設定旋轉時的X軸座標
           pivotY  設定旋轉時的Y軸座標
           duration 期間
     -->
    
<scale
    android:fromXScale="1.0"
    android:toXScale="0.0"
    android:fromYScale="1.0"
    android:toYScale="0.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="5000"
    />
</set>

 

translate.xml

 

  <?xml version="1.0" encoding="UTF-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
<!-- 畫面轉移位置移動動畫效果 -->
    <translate
       android:fromXDelta="0%"
       android:toXDelta="100%"
       android:fromYDelta="0%"
       android:toYDelta="0%"
       android:duration="5000"  
        />

</set>

下面是主介面xml的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ImageView
        android:id="@+id/image1"
        android:layout_width="match_parent"
        android:layout_height="200px"
        />
    
    <ImageView
        android:id="@+id/image2"
        android:layout_width="match_parent"
        android:layout_height="200px"
        />
    
    <ImageView
        android:id="@+id/image3"
        android:layout_width="match_parent"
        android:layout_height="200px"
        />
    
    <ImageView
        android:id="@+id/image4"
        android:layout_width="match_parent"
        android:layout_height="200px"
        />

</LinearLayout>

然後是Activity代碼

 

 public class AnimationDemo extends Activity{
   private Animation animation,animation1,animation2,animation3;
    private ImageView image1,image2,image3,image4;
   @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.animation);
        initView();
    }

   public void initView()
   {
       animation=AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.rotate);
       animation1=AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.scale);
       animation2=AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.alpha);
       animation3=AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.translate);
       image1=(ImageView)findViewById(R.id.image1);
       image1.setImageResource(R.drawable.jpeg);
       image2=(ImageView)findViewById(R.id.image2);
       image2.setImageResource(R.drawable.jpg);
       image3=(ImageView)findViewById(R.id.image3);
       image3.setImageResource(R.drawable.png);
       image4=(ImageView)findViewById(R.id.image4);
       image4.setImageResource(R.drawable.gif);
       image1.startAnimation(animation);
       image2.startAnimation(animation1);
       image3.startAnimation(animation2);
       image4.startAnimation(animation3);
   }
}

好了,就這樣就是先了四種動畫效果,另外還有一個知識點,是動畫裡面的速率問題,有需要的可以去上網百度看看吧。

實現效果如下:

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.