Android旋轉(zhuǎn)、平移、縮放和透明度漸變的補(bǔ)間動畫
android實(shí)現(xiàn)旋轉(zhuǎn)、平移、縮放和透明度漸變的補(bǔ)間動畫,具體實(shí)現(xiàn)如下:
1.在新建項(xiàng)目的res目錄中,創(chuàng)建一個名為anim的目錄,并在該目錄中創(chuàng)建實(shí)現(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實(shí)現(xiàn)補(bǔ)間動畫
- Android動畫系列之幀動畫和補(bǔ)間動畫的示例代碼
- Android動畫學(xué)習(xí)筆記之補(bǔ)間動畫
- Android補(bǔ)間動畫基本使用(位移、縮放、旋轉(zhuǎn)、透明)
- Android控件Tween動畫(補(bǔ)間動畫)實(shí)現(xiàn)方法示例
- android 幀動畫,補(bǔ)間動畫,屬性動畫的簡單總結(jié)
- Android幀動畫、補(bǔ)間動畫、屬性動畫用法詳解
- Android動畫之補(bǔ)間動畫(Tween Animation)基礎(chǔ)學(xué)習(xí)
- Android動畫之補(bǔ)間動畫(Tween Animation)實(shí)例詳解
- Android?Studio實(shí)現(xiàn)簡單補(bǔ)間動畫
相關(guān)文章
Android?換膚實(shí)現(xiàn)指南demo及案例解析
這篇文章主要為大家介紹了Android換膚指南demo及案例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06Android編程實(shí)現(xiàn)帶漸變效果的圓角矩形示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)帶漸變效果的圓角矩形,涉及Android界面布局及屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Android自定義View原理(實(shí)戰(zhàn))
這篇文章主要介紹了Android自定義View原理,由于Android系統(tǒng)內(nèi)置的View不滿足我們的業(yè)務(wù)需求,變產(chǎn)生了需要自定義View的原因,關(guān)于自定義詳情,需要的小伙伴可以參考下面文章具體詳情2022-05-05Android三種網(wǎng)絡(luò)通訊方式及Android的網(wǎng)絡(luò)通訊機(jī)制
在android平臺目前提供了三種網(wǎng)絡(luò)接口可以使用:分別是java.net.*(標(biāo)準(zhǔn)Java接口)、Org.apache接口和Android.net.*(Android網(wǎng)絡(luò)接口),本文主要給大家介紹android三種網(wǎng)絡(luò)通訊方式及android的網(wǎng)絡(luò)通訊機(jī)制,小伙伴們一起學(xué)習(xí)吧2015-11-11Android ViewPager實(shí)現(xiàn)無限循環(huán)輪播廣告位Banner效果
這篇文章主要為大家詳細(xì)介紹了Android ViewPager實(shí)現(xiàn)無限循環(huán)輪播廣告位Banner效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-07-07Android FaceDetector實(shí)現(xiàn)人臉檢測功能
這篇文章主要為大家詳細(xì)介紹了Android FaceDetector實(shí)現(xiàn)人臉檢測功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05