Android中的常用動畫分為幀動畫(Drawable Animation)、View動畫(補間動畫)、屬性動畫。
1 幀動畫
1.1 首先我們介紹一下幀動畫(Drawable Animation).根據Android官方文檔中對于幀動畫的介紹就是加載一系列的圖檔資源。
Drawable animation lets you load a series of Drawable resources one after another to create an animation.
1.2 那麼我們從官方文檔中同樣也獲得了如何實作一個幀動畫效果步驟:
[1] 在Android 項目中的res/目錄下,建立drawable目錄.
[2] 将要顯示的圖檔放到res/drawable/
[3] 建立一個包含 <animation-list>元素的xml檔案
[4] 使用代碼通過xml檔案将圖檔顯示出來
1.3 具體的代碼實作如下:
為了示範效果,我随便在網上找了9張圖檔,動畫的效果不是很強。并且在res/drawable/下建立drawanim.xml的檔案。具體代碼如下:
1.3.1 MainActivity.java
package com.animationdemo;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
AnimationDrawable rocketAnimation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//查找到我們的ImageView元件
ImageView rocketImage = (ImageView) findViewById(R.id.iv);
//設定背景資源
rocketImage.setBackgroundResource(R.drawable.drawanim);
//擷取AnimationDrawable類型
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//當觸摸屏被點選之後,啟動動畫
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}
@Override
protected void onStop() {
super.onStop();
//停止動畫效果
rocketAnimation.stop();
}
}
1.3.2 drawanim.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<!--oneshot為false表示如下的9張圖檔不斷地循環播放,如果為true,表示隻顯示一次-->
<!--duration表示每張圖檔顯示的時間-->
<item android:drawable="@drawable/image_1" android:duration="1000" />
<item android:drawable="@drawable/image_2" android:duration="1000" />
<item android:drawable="@drawable/image_3" android:duration="1000" />
<item android:drawable="@drawable/image_4" android:duration="1000" />
<item android:drawable="@drawable/image_5" android:duration="1000" />
<item android:drawable="@drawable/image_6" android:duration="1000" />
<item android:drawable="@drawable/image_7" android:duration="1000" />
<item android:drawable="@drawable/image_8" android:duration="1000" />
<item android:drawable="@drawable/image_9" android:duration="1000" />
</animation-list>
1.3.3 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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.animationdemo.MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
1.3.4 Demo代碼
如果你不願意下載下傳,從我上面的代碼拷貝一下,然後自己随便添加幾張圖檔就可以使用了。不願意麻煩的就Drawable Animation Demo下載下傳
2 View動畫(補間動畫)
補間動畫又叫view動畫,主要有一下幾個動畫的操作:
動畫的建立有兩種方式,通過代碼的方式和通過布局的方式。下面針對這兩種方式來實作一下具體的例子. 動畫效果不會改變控件的實際坐标,就一個效果而已。
2.1 代碼方式
建立項目MyViewAnimDemo,然後展現一下代碼: MainActivity.java
package com.myviewanimdemo;
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.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button btn_alpha;
private Button btn_romate;
private Button btn_scale;
private Button btn_translate;
private Button btn_all;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到關心的控件
btn_alpha = (Button) findViewById(R.id.btn_alpha);//透明
btn_romate = (Button) findViewById(R.id.btn_rotate);//旋轉
btn_scale = (Button) findViewById(R.id.btn_scale);// 縮放
btn_translate = (Button) findViewById(R.id.btn_translate); // 平移
btn_all = (Button) findViewById(R.id.btn_all); // 所有效果
iv = (ImageView) findViewById(R.id.iv); //查找到圖檔
// 初始化控件
initView();
}
private void initView() {
btn_alpha.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 透明圖檔 1.0不透明,0.0透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
// 設定動畫執行的時間
alphaAnimation.setDuration(2000);
// 設定重複的次數1+X,X就是此處設定的
alphaAnimation.setRepeatCount(2);
// 設定動畫模式
alphaAnimation.setRepeatMode(Animation.REVERSE);
// 開始動畫
iv.startAnimation(alphaAnimation);
}
});
btn_romate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 按照預設的位置旋轉
// RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
// 旋轉圖檔,按照中心旋轉 0.5就是按照圖檔的寬或者高的1/2的位置
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(2000);// 設定動畫執行的時間
rotateAnimation.setRepeatCount(1);// 設定重複的次數1+X,X就是此處設定的
rotateAnimation.setRepeatMode(Animation.REVERSE);// 設定動畫模式
//開啟動畫
iv.startAnimation(rotateAnimation);
}
});
btn_scale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 縮放圖檔,按照自己的中心點,x、y軸各放大原來的兩倍。
// 1.0f表示原大小。2.0f表示原來的兩倍
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);//設定執行的時間
// 設定縮放的模式
scaleAnimation.setRepeatMode(Animation.REVERSE);
scaleAnimation.setRepeatCount(3);// 設定縮放的次數
// 啟動動畫
iv.startAnimation(scaleAnimation);
}
});
btn_translate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 平移圖檔,按照父窗體的長和寬的長度0.2倍的距離移動。
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.2f,
Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.2f);
translateAnimation.setDuration(2000);// 設定執行時間
translateAnimation.setRepeatCount(4);// 設定重複的次數
// 設定重複模式
translateAnimation.setRepeatMode(Animation.REVERSE);
// 啟動動畫
iv.startAnimation(translateAnimation);
}
});
btn_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 1 所有效果一起執行
AnimationSet animationSet = new AnimationSet(true);
// 2 将之前所有的圖檔效果增加
// 2.1 透明圖檔 1.0不透明,0.0透明
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
// 設定動畫執行的時間
alphaAnimation.setDuration(2000);
// 設定重複的次數1+X,X就是此處設定的
alphaAnimation.setRepeatCount(2);
// 設定動畫模式
alphaAnimation.setRepeatMode(Animation.REVERSE);
// 2.2 旋轉圖檔,按照中心旋轉 0.5就是按照圖檔的寬或者高的1/2的位置
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(2000);// 設定動畫執行的時間
rotateAnimation.setRepeatCount(1);// 設定重複的次數1+X,X就是此處設定的
rotateAnimation.setRepeatMode(Animation.REVERSE);// 設定動畫模式
// 2.3 縮放圖檔,按照自己的中心點,x、y軸各放大原來的兩倍。
// 1.0f表示原大小。2.0f表示原來的兩倍
ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);//設定執行的時間
// 設定縮放的模式
scaleAnimation.setRepeatMode(Animation.REVERSE);
scaleAnimation.setRepeatCount(3);// 設定縮放的次數
// 2.4 平移圖檔,按照父窗體的長和寬的長度0.2倍的距離移動。
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.2f,
Animation.RELATIVE_TO_PARENT,0, Animation.RELATIVE_TO_PARENT,0.2f);
translateAnimation.setDuration(2000);// 設定執行時間
translateAnimation.setRepeatCount(4);// 設定重複的次數
// 設定重複模式
translateAnimation.setRepeatMode(Animation.REVERSE);
// 3 添加動畫合集
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(alphaAnimation);
// 4 啟動動畫
iv.startAnimation(animationSet);
}
});
}
}
activity_main.xml
<?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="com.myviewanimdemo.MainActivity">
<!--添加5個按鈕和一張圖檔-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_alpha"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="透明" />
<Button
android:id="@+id/btn_rotate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="旋轉" />
<Button
android:id="@+id/btn_scale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="縮放" />
<Button
android:id="@+id/btn_translate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="平移" />
<Button
android:id="@+id/btn_all"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="一起" />
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
2.2 使用xml設定動畫效果
建立項目MyViewAnimByXmlDemo,展示代碼如下. MainActivity.java:
package com.myviewanimbyxmldemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button btn_alpha;
private Button btn_romate;
private Button btn_scale;
private Button btn_translate;
private Button btn_all;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到關心的控件
btn_alpha = (Button) findViewById(R.id.btn_alpha);//透明
btn_romate = (Button) findViewById(R.id.btn_rotate);//旋轉
btn_scale = (Button) findViewById(R.id.btn_scale);// 縮放
btn_translate = (Button) findViewById(R.id.btn_translate); // 平移
btn_all = (Button) findViewById(R.id.btn_all); // 所有效果
iv = (ImageView) findViewById(R.id.iv); //查找到圖檔
// 初始化控件
initView();
}
private void initView() {
btn_alpha.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 透明圖檔 1.0不透明,0.0透明
Animation alphaAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
// 開始動畫
iv.startAnimation(alphaAnimation);
}
});
btn_romate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 旋轉圖檔,按照中心旋轉 0.5就是按照圖檔的寬或者高的1/2的位置
Animation rotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
//開啟動畫
iv.startAnimation(rotateAnimation);
}
});
btn_scale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 縮放圖檔,按照自己的中心點,x、y軸各放大原來的兩倍。
// 1.0f表示原大小。2.0f表示原來的兩倍
Animation scaleAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale);
// 啟動動畫
iv.startAnimation(scaleAnimation);
}
});
btn_translate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 平移圖檔,按照父窗體的長和寬的長度0.2倍的距離移動。
Animation translateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.translate);
// 啟動動畫
iv.startAnimation(translateAnimation);
}
});
btn_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 所有效果一起執行
Animation animationSet = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.set);
// 啟動動畫
iv.startAnimation(animationSet);
}
});
}
}
activity_main.xml
<?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="com.myviewanimdemo.MainActivity">
<!--添加5個按鈕和一張圖檔-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_alpha"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="透明" />
<Button
android:id="@+id/btn_rotate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="旋轉" />
<Button
android:id="@+id/btn_scale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="縮放" />
<Button
android:id="@+id/btn_translate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="平移" />
<Button
android:id="@+id/btn_all"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="一起" />
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</alpha>
rotate.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</rotate>
scale.xml
<?xml version="1.0" encoding="utf-8"?>
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</scale>
translate.xml
<?xml version="1.0" encoding="utf-8"?>
<translate
android:fromXDelta="0%p"
android:toXDelta="20%p"
android:fromYDelta="0%p"
android:toYDelta="20%p"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
set.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0%p"
android:toXDelta="20%p"
android:fromYDelta="0%p"
android:toYDelta="20%p"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</scale>
<rotate
android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</rotate>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse"
xmlns:android="http://schemas.android.com/apk/res/android">
</alpha>
</set>
3 屬性動畫
會改變屬性控件的真實坐标 實際開發中用的比較少。主要是用的ObjectAnimator類實作。同樣的屬性動畫也有兩種方式實作。代碼和布局實作。
3.1 代碼的方式 建立項目MyAttributesAnimDemo,代碼如下: MainActivity.java
package com.myattributesanimdemo;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Button btn_alpha;
private Button btn_romate;
private Button btn_scale;
private Button btn_translate;
private Button btn_all;
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到關心的控件
btn_alpha = (Button) findViewById(R.id.btn_alpha);//透明
btn_romate = (Button) findViewById(R.id.btn_rotate);//旋轉
btn_scale = (Button) findViewById(R.id.btn_scale);// 縮放
btn_translate = (Button) findViewById(R.id.btn_translate); // 平移
btn_all = (Button) findViewById(R.id.btn_all); // 所有效果
iv = (ImageView) findViewById(R.id.iv); //查找到圖檔
// 初始化控件
initView();
}
private void initView() {
btn_alpha.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 透明圖檔 1.0不透明,0.0透明
ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.5f, 0, 1, 0, 1);
// 設定執行時間
alpha.setDuration(2000);
// 開始動畫
alpha.start();
}
});
btn_romate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 旋轉圖檔
ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360);
rotation.setDuration(2000);// 設定執行時間
rotation.start(); // 開始啟動
}
});
btn_scale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 縮放圖檔
ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0.1f, 2, 1, 0.5f);
scaleY.setDuration(2000);//設定執行時間
scaleY.start();
}
});
btn_translate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 平移圖檔從X軸的10走到90再回到30再走到100
ObjectAnimator translationX = ObjectAnimator.ofFloat(iv, "translationX", 10, 90, 30, 100);
translationX.setDuration(2000);// 設定執行時間
// 啟動動畫
translationX.start();
}
});
btn_all.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 所有效果一起執行
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator translationX = ObjectAnimator.ofFloat(iv, "translationX", 10, 90, 30, 100);
ObjectAnimator rotation = ObjectAnimator.ofFloat(iv, "rotation", 0, 180, 90, 360);
ObjectAnimator alpha = ObjectAnimator.ofFloat(iv, "alpha", 0, 0.5f, 0, 1, 0, 1);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(iv, "scaleY", 0.1f, 2, 1, 0.5f);
// 設定執行時間
animatorSet.setDuration(3000);
animatorSet.setTarget(iv);
// 往集合中添加對象
// 挨個執行
// animatorSet.playSequentially(translationX, rotation, alpha, scaleY);
// 一起飛
animatorSet.playTogether(translationX, rotation, alpha, scaleY);
// 一起執行
animatorSet.start();
}
});
}
}
activity_main.xml
<?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="com.myattributesanimdemo.MainActivity">
<!--添加5個按鈕和一張圖檔-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_alpha"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="透明" />
<Button
android:id="@+id/btn_rotate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="旋轉" />
<Button
android:id="@+id/btn_scale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="縮放" />
<Button
android:id="@+id/btn_translate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="平移" />
<Button
android:id="@+id/btn_all"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:layout_weight="1"
android:text="一起" />
</LinearLayout>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>
3.2 使用XML加載
由于屬性動畫不是很常用,在此我就舉個例子就可以了 MainActivity.java
package com.myattributesanimxmldemo;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView iv;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到關心的控件
btn = (Button) findViewById(R.id.btn);
iv = (ImageView) findViewById(R.id.iv);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 加載動畫
Animator anim = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.animator);
anim.setTarget(iv);
anim.start();
}
});
}
}
animator.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator
android:duration="2000"
android:propertyName="translateX"
android:valueFrom="10"
android:valueTo="100"
xmlns:android="http://schemas.android.com/apk/res/android">
</objectAnimator>
activity_main.xml
<?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="com.myattributesanimxmldemo.MainActivity">
<Button
android:id="@+id/btn"
android:text="旋轉動畫"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
</RelativeLayout>