Android xml實(shí)現(xiàn)animation的4種動(dòng)畫效果實(shí)例代碼
animation有四種動(dòng)畫類型:分別為alpha(透明的漸變)、rotate(旋轉(zhuǎn))、scale(尺寸伸縮)、translate(移動(dòng)),二實(shí)現(xiàn)的分發(fā)有兩種,一種是javaCode,另外一種是XML,而我今天要說(shuō)的是XML實(shí)現(xiàn)的方法,個(gè)人感覺(jué)javaCode的實(shí)現(xiàn)方法比xml要簡(jiǎn)單,所以有需要的可以自己去找找資料看看。
先給大家展示下效果圖,如果大家感覺(jué)還不錯(cuò),請(qǐng)繼續(xù)往下閱讀。
下面是我的四個(gè)xml文件,分別代表這四種動(dòng)畫類型。
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"> <!-- 漸變透明的動(dòng)畫效果 --> <!--fromAlpha 動(dòng)畫起始透明的 1.0完全不透明 toAlpha 動(dòng)畫結(jié)束時(shí)透明的 0.0完全透明 startOffset 設(shè)置啟動(dòng)時(shí)間 duration 屬性動(dòng)畫持續(xù)時(shí)間 --> <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"> <!-- 畫面轉(zhuǎn)移旋轉(zhuǎn)動(dòng)畫效果 --> <!-- fromDegrees開始角度 toDegrees結(jié)束角度 pivotX設(shè)置旋轉(zhuǎn)時(shí)的X軸坐標(biāo) --> <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"> <!-- 漸變尺寸伸縮動(dòng)畫效果 --> <!-- fromXScale 起始x軸坐標(biāo) toXScale 止x軸坐標(biāo) fromYScale 起始y軸坐標(biāo) toYScale 止y軸坐標(biāo) pivotX 設(shè)置旋轉(zhuǎn)時(shí)的X軸坐標(biāo) pivotY 設(shè)置旋轉(zhuǎn)時(shí)的Y軸坐標(biāo) duration 持續(xù)時(shí)間 --> <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"> <!-- 畫面轉(zhuǎn)移位置移動(dòng)動(dòng)畫效果 --> <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); } }
好了,就這樣就是先了四種動(dòng)畫效果,另外還有一個(gè)知識(shí)點(diǎn),是動(dòng)畫里面的速率問(wèn)題,有需要的可以去上網(wǎng)百度看看吧。
- Android SpringAnimation彈性動(dòng)畫解析
- Android自定義Animation實(shí)現(xiàn)View搖擺效果
- Android Animation實(shí)戰(zhàn)之一個(gè)APP的ListView的動(dòng)畫效果
- Android Animation實(shí)戰(zhàn)之屏幕底部彈出PopupWindow
- Android動(dòng)畫之漸變動(dòng)畫(Tween Animation)詳解 (漸變、縮放、位移、旋轉(zhuǎn))
- Android動(dòng)畫之逐幀動(dòng)畫(Frame Animation)實(shí)例詳解
- Android 中 Tweened animation的實(shí)例詳解
相關(guān)文章
android:TextView簡(jiǎn)單設(shè)置文本樣式和超鏈接的方法
這篇文章主要介紹了android:TextView簡(jiǎn)單設(shè)置文本樣式和超鏈接的方法,涉及TextView常見文字屬性的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08解析Android游戲中獲取電話狀態(tài)進(jìn)行游戲暫停或繼續(xù)的解決方法
本篇文章是對(duì)在Android游戲中獲取電話狀態(tài)進(jìn)行游戲暫?;蚶^續(xù)的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Flutter搞定寬高不統(tǒng)一布局開發(fā)的方法詳解
我們?cè)陂_發(fā)移動(dòng)端界面的時(shí)候,經(jīng)常會(huì)遇到一組尺寸不一的組件需要作為同一組展示,所以本文就將利用Wrap組件搞定寬高不統(tǒng)一布局開發(fā),需要的可以參考一下2023-06-06總結(jié)Android App內(nèi)存優(yōu)化之圖片優(yōu)化
網(wǎng)上有很多大拿分享的關(guān)于Android性能優(yōu)化的文章,主要是通過(guò)各種工具分析,使用合理的技巧優(yōu)化APP的體驗(yàn),提升APP的流暢度,但關(guān)于內(nèi)存優(yōu)化的文章很少有看到。下面是我在實(shí)踐過(guò)程中使用的一些方法,很多都是不太成熟的項(xiàng)目,只是將其作為一種處理方式分享給大家。2016-08-08Android屏幕適配工具類 Android自動(dòng)生成不同分辨率的值
這篇文章主要為大家詳細(xì)介紹了Android屏幕適配工具類,Android自動(dòng)生成不同分辨率的值,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03Android OpenGL入門之GLSurfaceView
這篇文章主要介紹了OpenGL入門知識(shí),如何在Android中使用GLSurfaceView,如果對(duì)OpenGL感興趣的同學(xué),可以參考下2021-04-04Android 網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽代碼實(shí)例(一)
本文給大家介紹Android 網(wǎng)絡(luò)狀態(tài)實(shí)時(shí)監(jiān)聽代碼實(shí)例(一),對(duì)android網(wǎng)絡(luò)狀態(tài)監(jiān)聽相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的代碼
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09Android應(yīng)用更新之自動(dòng)檢測(cè)版本及自動(dòng)升級(jí)
這篇文章主要為大家詳細(xì)介紹了Android應(yīng)用更新之自動(dòng)檢測(cè)版本及自動(dòng)升級(jí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09flutter PositionedTransition實(shí)現(xiàn)縮放動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了flutter PositionedTransition實(shí)現(xiàn)縮放動(dòng)畫,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07