使用Animation-list實現等待旋轉圓圈動畫
來自: http://blog.csdn.net//chenguang79/article/details/50147649
我們在做網絡交互或是從服務器提取數據的時候,常常會給出一個等待的動畫,就是一個小圓圈轉啊轉的。它的使用很簡單,我們只要使用Animation-list用三,五行代碼,就可以搞定了。
首先,我們要找幾個轉圈的圖片,注意這幾個圖片是有講究的,就是它們正好可以拼接成一個轉圈的動畫,如下圖
這個網上有很多,大家可以自行去找一下
下面看代碼,首先,建立anim文件夾,然后在里面建:imgloading.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 根標簽為animation-list,其中oneshot代表著是否只展示一遍,設置為false會不停的循環播放動畫 根標簽下,通過item標簽對動畫中的每一個圖片進行聲明 android:duration 表示展示所用的該圖片的時間長度 --> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/black_01" android:duration="150"/> <item android:drawable="@drawable/black_02" android:duration="150"/> <item android:drawable="@drawable/black_03" android:duration="150"/> <item android:drawable="@drawable/black_04" android:duration="150"/> <item android:drawable="@drawable/black_05" android:duration="150"/> <item android:drawable="@drawable/black_06" android:duration="150"/> <item android:drawable="@drawable/black_07" android:duration="150"/> <item android:drawable="@drawable/black_08" android:duration="150"/> <item android:drawable="@drawable/black_09" android:duration="150"/> <item android:drawable="@drawable/black_10" android:duration="150"/> <item android:drawable="@drawable/black_11" android:duration="150"/> <item android:drawable="@drawable/black_12" android:duration="150"/> </animation-list>
activity_main.xml
<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=".MainActivity" android:gravity="center"> <ImageView android:id="@+id/img_loading" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
程序代碼:mainActivity.java
import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private ImageView img_loading; private AnimationDrawable AniDraw; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img_loading = (ImageView)findViewById(R.id.img_loading); img_loading.setBackgroundResource(R.anim.imgloading); AniDraw = (AnimationDrawable)img_loading.getBackground(); AniDraw.start(); } }效果如下圖:
本文由用戶 BraSpringfi 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!