最近在做一個小播放器用到一些簡單動畫效果,記錄一下
1499226385177.gif
效果圖 一個旋轉的圓形圖片,右上角是幀動畫
直接上代碼
旋轉圓形圖片就是用原生的旋轉屬性動畫RotateAnimation
//自定義圓形圖片
xz = (CircleImageView) mview.findViewById(R.id.xz);
rotateAnimation = new RotateAnimation(0f,360f, Animation.RELATIVE_TO_SELF,
0.5f,Animation.RELATIVE_TO_SELF,0.5f);
LinearInterpolator lin = new LinearInterpolator();
rotateAnimation.setInterpolator(lin);//勻速
rotateAnimation.setDuration(8000);//設置動畫持續時間
rotateAnimation.setRepeatCount(-1);//設置重復次數 -1不停
rotateAnimation.setFillAfter(true);//動畫執行完后是否停留在執行完的狀態
rotateAnimation.setStartOffset(10);//執行前的等待時間
xz.setAnimation(rotateAnimation);
rotateAnimation.startNow();
然后不停的幀動畫 原理類似小時候看的小人書 很多圖片重疊,產生動畫的錯覺
首先drawable里新建一個動畫集合
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/a1w" android:duration="200">
</item>
<item android:drawable="@drawable/a1x" android:duration="200">
</item>
<item android:drawable="@drawable/a1y" android:duration="200">
</item>
<item android:drawable="@drawable/a1z" android:duration="200">
</item>
<!--根標簽為animation-list,其中oneshot代表著是否只展示一遍,設置為false會不停的循環播放動畫
根標簽下,通過item標簽對動畫中的每一個圖片進行聲明
android:duration 表示展示所用的該圖片的時間長度-->
</animation-list>
代碼使用
//使用圖片作為載體
playnow = (ImageView) findViewById(R.id.playnow);
AnimationDrawable animationDrawable = (AnimationDrawable) playnow.getDrawable();
animationDrawable.start();