-----
自定义类
-----
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();
}
}
}
}
------
动画文件
------