ui opt, 性能优化:动效实现问题

This commit is contained in:
wangcongtao
2020-09-16 16:40:05 +08:00
parent 4da2a88fb0
commit 44b20f7c13
10 changed files with 61 additions and 52 deletions

View File

@@ -1,63 +1,64 @@
package com.mogo.module.v2x.utils.animation;
import android.graphics.drawable.AnimationDrawable;
import android.util.Log;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import com.mogo.utils.ThreadPoolService;
import com.mogo.utils.UiThreadHandler;
public class V2XAnimationManager implements Animation {
private static final String TAG = "V2XAnimationManager";
private ImageView targetImageView;
private Animation delegate;
private boolean isStarted = false;
private int mStartIndex = 0;
private final static int MSG_LOOP = 3004;
private long INTERVAL = 100L;
private Handler mHandler = new Handler( Looper.getMainLooper() ) {
@Override
public void handleMessage( Message msg ) {
super.handleMessage( msg );
switch ( msg.what ) {
case MSG_LOOP:
if ( isStarted ) {
targetImageView.setImageResource( AnimationResources.loadingRes[mStartIndex++ % AnimationResources.loadingRes.length] );
mHandler.sendEmptyMessageDelayed( MSG_LOOP, INTERVAL );
}
break;
}
}
};
public void animationWithTarget(ImageView imageView, int[] resources, int duration) {
targetImageView = imageView;
ThreadPoolService.execute(() -> {
final AnimationDrawable drawable = new AnimationDrawable();
for (int i = 0; i < resources.length; i++) {
drawable.setOneShot(false);
drawable.addFrame(targetImageView.getResources().getDrawable(resources[i]), duration);
}
UiThreadHandler.post(() -> {
targetImageView.setBackground(drawable);
delegate = new DelegateDrawable(drawable);
start();
});
});
INTERVAL = duration;
start();
}
@Override
synchronized public void start() {
if (delegate != null && !isStarted) {
targetImageView.setVisibility(View.VISIBLE);
isStarted = true;
delegate.start();
}
isStarted = true;
mHandler.sendEmptyMessage( MSG_LOOP );
targetImageView.setVisibility(View.VISIBLE);
}
@Override
synchronized public void stop() {
if (delegate != null && isStarted) {
isStarted = false;
delegate.stop();
targetImageView.setVisibility(View.INVISIBLE);
}
isStarted = false;
mHandler.removeMessages( MSG_LOOP );
targetImageView.setVisibility(View.INVISIBLE);
}
public void soptWithError(){
if (delegate != null && isStarted) {
isStarted = false;
delegate.stop();
}
stop();
targetImageView.setVisibility(View.VISIBLE);
}
public void release() {
delegate = null;
stop();
}
}