android 實現載入動畫對話方塊

來源:互聯網
上載者:User

標籤:android   loading   對話方塊   動畫   

先來兩張

      



自訂對話方塊:

public class LoadingProgressDialog extends ProgressDialog {private AnimationDrawable mAnimation;private Context mContext;private ImageView mImageView;private String mLoadingTitle;private TextView mLoadingTv;private int mResid;public LoadingProgressDialog(Context context, String content, int id) {super(context);this.mContext = context;this.mLoadingTitle = content;this.mResid = id;setCanceledOnTouchOutside(true);}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);initView();initData();}private void initData() {mImageView.setBackgroundResource(mResid);mAnimation = (AnimationDrawable) mImageView.getBackground();mImageView.post(new Runnable() {@Overridepublic void run() {mAnimation.start();}});mLoadingTv.setText(mLoadingTitle);}public void setContent(String str) {mLoadingTv.setText(str);}private void initView() {setContentView(R.layout.progress_dialog);mLoadingTv = (TextView) findViewById(R.id.loadingTv);mImageView = (ImageView) findViewById(R.id.loadingIv);}}

在layout檔案夾下建立progress_dialog.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:gravity="center"    android:layout_gravity="center"    android:orientation="vertical">    <ImageView        android:id="@+id/loadingIv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:id="@+id/loadingTv"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#ffffff"        android:textSize="20sp" /></LinearLayout>

在res檔案夾下建立anim檔案夾,然後在裡面建立frame.xml

動畫是由一張一張的圖片逐幀播放的,裡面每一個item就是一張圖片,動畫有多少幀就有多少張圖片,有多少圖片就有多少item。

<?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/loading_01"        android:duration="100" />    <item        android:drawable="@drawable/loading_02"        android:duration="100" />    <item        android:drawable="@drawable/loading_03"        android:duration="100" />    <item        android:drawable="@drawable/loading_04"        android:duration="100" /></animation-list>

用法:

LoadingProgressDialog dialog =new LoadingProgressDialog(MainActivity.this, "正在載入中...",R.anim.frame);//開啟dialog.show();//隱藏dialog.dismiss();

Demo地址:https://github.com/yangjie10930/LoadingAnim

android 實現載入動畫對話方塊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.