標籤:pre rip 移動 sdn func lin 播放 bundle animation
Android-通過Java代碼來實現屬性動畫
除了能夠使用定義xml檔案來設定動畫之外。還能夠使用java代碼來進行控制動畫。
示比例如以下:
布局檔案:
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.xieth.as.againanimdemo.MainActivity2" > <ImageView android:id="@+id/id_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="click" android:src="@mipmap/ic_launcher" /> <Button android:id="@+id/id_btnMove" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:onClick="move" android:layout_centerHorizontal="true" android:layout_marginBottom="40dp" android:text="移動" /></RelativeLayout>
主活動:
package com.xieth.as.againanimdemo;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.view.animation.TranslateAnimation;import android.widget.ImageView;import android.widget.Toast;public class MainActivity2 extends AppCompatActivity { private ImageView imageView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); imageView = (ImageView) findViewById(R.id.id_img); } public void click(View view) { Toast.makeText(this, "click", Toast.LENGTH_SHORT).show(); } public void move(View view) { TranslateAnimation animation = new TranslateAnimation(0, 200, 0, 0); // 設定顯示的時間長度 animation.setDuration(1000); imageView.startAnimation(animation); }}
設定事件
移動
TranslateAnimation animation = new TranslateAnimation(0, 200, 0, 0);// 設定顯示的時間長度animation.setDuration(1000);imageView.startAnimation(animation);
執行:
ObjectAnimator.ofFloat(imageView, "translationX", 0F, 200F).setDuration(1000).start();
執行:
能夠看見效果是一樣的。
改動一下代碼:
ObjectAnimator.ofFloat(imageView, "translationY", 0F, 200F).setDuration(1000).start();
這次是Y方向:
旋轉
ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F).setDuration(1000).start();
執行:
組合動畫
ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F).setDuration(1000).start(); ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F).setDuration(1000).start(); ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F).setDuration(1000).start();
執行:
能夠看到這三個動畫同一時候執行
使用PropertyValuesHolder
PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation", 0F, 360F); PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX", 0F, 200F); PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY", 0F, 200F); ObjectAnimator.ofPropertyValuesHolder(imageView, p1, p2, p3).setDuration(1000).start();
執行:
使用AnimatorSet
ObjectAnimator a1 = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);ObjectAnimator a2 = ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F);ObjectAnimator a3 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F);AnimatorSet set = new AnimatorSet();set.setDuration(1000);set.playTogether(a1, a2, a3);set.start();
執行:
效果一樣。
之前都是同事播放的。如今能夠依照順序來執行。
set.playSequentially(a1, a2, a3);
執行:
能夠看到先旋轉,再向X方向移動。再向Y方向移動。
也可這樣進行順序控制。
先同一時候執行X和Y方向的移動,再進行旋轉
set.play(a2).with(a3);set.play(a1).after(a2);
執行:
Android-通過Java代碼來實現屬性動畫