loading開源控制元件
阿新 • • 發佈:2019-01-23
----- 自定義類 ----- import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import media.itsme.common.R; public class LoadingImageView extends ImageView { private boolean mRequestAnim = true; public LoadingImageView(Context context, AttributeSet attrs) { super(context, attrs); } public LoadingImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public LoadingImageView(Context context) { super(context); } @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (mRequestAnim) { startLoadingAnimation(); } } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); clearAnimation(); } @Override protected void onVisibilityChanged(View changedView, int visibility) { super.onVisibilityChanged(changedView, visibility); if (visibility == View.INVISIBLE || visibility == View.GONE) { clearAnimation(); return; } if (mRequestAnim) { startLoadingAnimation(); } } public void setmRequestAnim(boolean requestAnim) { if (mRequestAnim != requestAnim) { mRequestAnim = requestAnim; if (!mRequestAnim) { post(new Runnable() { @Override public void run() { clearAnimation(); } }); } } } /** * 當前LoadingImageView 屬性為VISIBLE 時才啟動動畫 */ private void startLoadingAnimation(){ if (getVisibility() == View.VISIBLE) { try{ Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(getContext(), R.anim.comm_loading); startAnimation(hyperspaceJumpAnimation); }catch (Exception e){ e.printStackTrace(); } } } } ------ 動畫檔案 ------ <?xml version="1.0" encoding="utf-8"?>