Android旋轉(zhuǎn)、平移、縮放和透明度漸變的補間動畫
android實現(xiàn)旋轉(zhuǎn)、平移、縮放和透明度漸變的補間動畫,具體實現(xiàn)如下:
1.在新建項目的res目錄中,創(chuàng)建一個名為anim的目錄,并在該目錄中創(chuàng)建實現(xiàn)旋轉(zhuǎn)、平移、縮放和透明度漸變的動畫資源文件。
透明度漸變的動畫資源文件anim_alpha.xml(完全不透明->完全透明->完全不透明)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <alpha android:fromAlpha="1" android:toAlpha="0" android:fillAfter="true" android:repeatMode="reverse" android:repeatCount="1" android:duration="2000"/> </set>
旋轉(zhuǎn)的動畫資源文件anim_rotate.xml(0度->720度->360度->0度)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:interpolator="@android:anim/accelerate_interpolator" android:fromDegrees="0" android:toDegrees="720" android:pivotX="50%" android:pivotY="50%" android:duration="2000"/> <rotate android:interpolator="@android:anim/accelerate_interpolator" android:startOffset="2000" android:fromDegrees="360" android:toDegrees="0" android:pivotX="50%" android:pivotY="50%" android:duration="2000"/> </set>
縮放動畫資源文件anim_scale.xml(放大2倍->收縮回來)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="1" android:interpolator="@android:anim/decelerate_interpolator" android:fromYScale="1" android:toXScale="2.0" android:toYScale="2.0" android:pivotX="50%" android:pivotY="50%" android:fillAfter="true" android:repeatCount="1" android:repeatMode="reverse" android:duration="2000"/> </set>
平移動畫資源文件anim_translate.xml(屏幕左側(cè)->屏幕右側(cè)->屏幕左側(cè))
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromXDelta="0" android:toXDelta="860" android:fromYDelta="0" android:toYDelta="0" android:fillAfter="true" android:repeatMode="reverse" android:repeatCount="1" android:duration="2000"/> </set>
主界面資源文件:
res/layout/main.xml: [html] view plain copy <?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="fill_parent" android:layout_height="fill_parent" android:id="@+id/linearLayout1" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2" android:orientation="horizontal"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button1" android:text="旋轉(zhuǎn)"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button2" android:text="平移"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button3" android:text="縮放"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button4" android:text="透明度變化"/> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView1" android:src="@drawable/img1"/> </LinearLayout>
效果如圖

2.MainActivity:
在onCreat()方法中,首先獲取動畫資源文件中創(chuàng)建的動畫資源,然后獲取要應(yīng)用動畫效果的ImageView,再獲取“旋轉(zhuǎn)”按鈕,并為該按鈕添加單擊事件監(jiān)聽器,在重寫onClik()方法中,播放動畫。具體代碼如下:
[java] view plain copy
package com.example.test;
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 {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Animation rotate=AnimationUtils.loadAnimation(this, R.anim.anim_rotate);//獲取旋轉(zhuǎn)動畫資源
final Animation translate=AnimationUtils.loadAnimation(this, R.anim.anim_translate);//獲取平移動畫資源
final Animation scale=AnimationUtils.loadAnimation(this, R.anim.anim_scale);//獲取縮放動畫資源
final Animation alpha=AnimationUtils.loadAnimation(this, R.anim.anim_alpha);//獲取透明度變化動畫資源
//獲取要應(yīng)用動畫效果的ImageView
final ImageView iv=(ImageView)findViewById(R.id.imageView1);
Button button1=(Button)findViewById(R.id.button1);//獲取"旋轉(zhuǎn)"按鈕
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//播放旋轉(zhuǎn)動畫
iv.startAnimation(rotate);
}
});
Button button2=(Button)findViewById(R.id.button2);//獲取"平移"按鈕
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//播放平移動畫
iv.startAnimation(translate);
}
});
Button button3=(Button)findViewById(R.id.button3);//獲取"縮放"按鈕
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//播放縮放動畫
iv.startAnimation(scale);
}
});
Button button4=(Button)findViewById(R.id.button4);//獲取"透明度漸變"按鈕
button4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
//播放透明度漸變動畫
iv.startAnimation(alpha);
}
});
}
}
效果如圖1、圖2、圖3、圖4:




以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- Android Studio實現(xiàn)補間動畫
- Android動畫系列之幀動畫和補間動畫的示例代碼
- Android動畫學(xué)習(xí)筆記之補間動畫
- Android補間動畫基本使用(位移、縮放、旋轉(zhuǎn)、透明)
- Android控件Tween動畫(補間動畫)實現(xiàn)方法示例
- android 幀動畫,補間動畫,屬性動畫的簡單總結(jié)
- Android幀動畫、補間動畫、屬性動畫用法詳解
- Android動畫之補間動畫(Tween Animation)基礎(chǔ)學(xué)習(xí)
- Android動畫之補間動畫(Tween Animation)實例詳解
- Android?Studio實現(xiàn)簡單補間動畫
相關(guān)文章
Android編程實現(xiàn)仿iphone抖動效果的方法(附源碼)
這篇文章主要介紹了Android編程實現(xiàn)仿iphone抖動效果的方法,結(jié)合實例形式分析了仿iphone抖動效果的頁面布局及功能實現(xiàn)技巧,并附帶實例源碼供讀者下載,需要的朋友可以參考下2015-11-11
Android點亮屏幕或屏幕解鎖和鎖定以及其他相關(guān)權(quán)限實現(xiàn)代碼
本文將帶你實現(xiàn)Android屏幕解鎖和鎖定;Android屏幕常亮/點亮以及其他相關(guān)權(quán)限,感興趣的朋友可以參考下,希望本文對你有所幫助2013-01-01
使用Android系統(tǒng)提供的DownloadManager來下載文件
本篇文章主要介紹了使用Android系統(tǒng)提供的DownloadManager來下載文件,可以將長時間的下載任務(wù)交給系統(tǒng),完全由系統(tǒng)管理,有需要的可以了解下。2016-11-11
Android Studio中引入Lambda表達(dá)式的方法
這篇文章主要給大家介紹了在Android Studio中引入Lambda表達(dá)式的方法,文中通過圖文介紹的非常詳細(xì),對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
Android調(diào)用系統(tǒng)裁剪的實現(xiàn)方法
下面小編就為大家分享一篇Android調(diào)用系統(tǒng)裁剪的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02

