標籤: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 實現載入動畫對話方塊