diff --git a/OCH/mogo-och-common-module/src/main/java/com/mogo/och/common/module/utils/AnimatorDrawableUtil.java b/OCH/mogo-och-common-module/src/main/java/com/mogo/och/common/module/utils/AnimatorDrawableUtil.java new file mode 100644 index 0000000000..bbf9d779eb --- /dev/null +++ b/OCH/mogo-och-common-module/src/main/java/com/mogo/och/common/module/utils/AnimatorDrawableUtil.java @@ -0,0 +1,261 @@ +package com.mogo.och.common.module.utils; + +import android.content.Context; +import android.content.res.XmlResourceParser; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.Handler; +import android.os.Message; +import android.widget.ImageView; + +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlPullParserException; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import java.util.Timer; +import java.util.TimerTask; + +/** + * @author: wangmingjun + * @date: 2022/7/20 + */ +public class AnimatorDrawableUtil { + private static final int MSG_START = 0xf1; + private static final int MSG_STOP = 0xf2; + private static final int STATE_STOP = 0xf3; + private static final int STATE_RUNNING = 0xf4; + + //运行状态 + private int mState = STATE_RUNNING; + //显示图片的View + private ImageView mImageView = null; + //图片资源的ID列表 + private List mResourceIdList = null; + //定时任务器 + private Timer mTimer = null; + //定时任务 + private AnimTimerTask mTimeTask = null; + //记录播放位置 + private int mFrameIndex = 0; + //播放形式 + private boolean isLooping = false; + + public AnimatorDrawableUtil() { + mTimer = new Timer(); + } + + /** + * 设置动画播放资源 + */ + public void setAnimation(ImageView imageview, List resourceIdList) { + mImageView = imageview; + if(mResourceIdList==null){ + mResourceIdList = new ArrayList(); + }else{ + mResourceIdList.clear(); + } + mResourceIdList.addAll(resourceIdList); + } + + /** + * 设置动画播放资源 + */ + public void setAnimation(Context context, int resourceId, ImageView imageview) { + this.mImageView = imageview; + if(mResourceIdList==null){ + mResourceIdList = new ArrayList(); + }else{ + mResourceIdList.clear(); + } + + loadFromXml(context, resourceId, new OnParseListener() { + @Override + public void onParse(List res) { + mResourceIdList.addAll(res); + } + }); + } + + /** + * 解析xml + * + * @param context + * @param resourceId 资源id + */ + private void loadFromXml(final Context context, final int resourceId, + final OnParseListener onParseListener) { + if (context == null) { + return; + } + + final List res = new ArrayList(); + XmlResourceParser parser = context.getResources().getXml(resourceId); + + try { + int eventType = parser.getEventType(); + while (eventType != XmlPullParser.END_DOCUMENT) { + if (eventType == XmlPullParser.START_DOCUMENT) { + } else if (eventType == XmlPullParser.START_TAG) { + if (parser.getName().equals("item")) { + for (int i = 0; i < parser.getAttributeCount(); i++) { + if (parser.getAttributeName(i).equals("drawable")) { + int resId = Integer.parseInt(parser.getAttributeValue(i).substring(1)); + res.add(resId); + } + } + } + } else if (eventType == XmlPullParser.END_TAG) { + } else if (eventType == XmlPullParser.TEXT) { + } + + eventType = parser.next(); + } + } catch (IOException e) { + // TODO: handle exception + e.printStackTrace(); + } catch (XmlPullParserException e2) { + // TODO: handle exception + e2.printStackTrace(); + } finally { + parser.close(); + } + + if (onParseListener != null) { + onParseListener.onParse(res); + } + } + + private AnimationLisenter lisenter; + + + /** + * 开始播放动画 + * + * @param loop 是否循环播放 + * @param duration 动画播放时间间隔 + */ + public void start(boolean loop, int duration, AnimationLisenter lisenter) { + this.lisenter = lisenter; + stop(); + if (mResourceIdList == null || mResourceIdList.size() == 0) { + return; + } + if (mTimer == null) { + mTimer = new Timer(); + } + isLooping = loop; + mFrameIndex = 0; + mState = STATE_RUNNING; + mTimeTask = new AnimTimerTask(); + mTimer.schedule(mTimeTask, 0, duration); + if (lisenter != null){ + lisenter.startAnimation(); + } + } + + /** + * 停止动画播放 + */ + public void stop() { + if (mTimer != null) { + mTimer.purge(); + mTimer.cancel(); + mTimer = null; + } + if (mTimeTask != null) { + mFrameIndex = 0; + mState = STATE_STOP; + mTimeTask.cancel(); + mTimeTask = null; + } + //移除Handler消息 + if (AnimHandler != null) { + AnimHandler.removeMessages(MSG_START); + AnimHandler.removeMessages(MSG_STOP); + AnimHandler.removeCallbacksAndMessages(null); + } + } + + /** + * 定时器任务 + */ + class AnimTimerTask extends TimerTask { + + @Override + public void run() { + if (mFrameIndex < 0 || mState == STATE_STOP) { + return; + } + + if (mFrameIndex < mResourceIdList.size()) { + Message msg = AnimHandler.obtainMessage(MSG_START, 0, 0, null); + msg.sendToTarget(); + } else { + mFrameIndex = 0; + if (!isLooping) { + Message msg = AnimHandler.obtainMessage(MSG_STOP, 0, 0, null); + msg.sendToTarget(); + } + } + } + } + + /** + * Handler + */ + private Handler AnimHandler = new Handler() { + public void handleMessage(Message msg) { + switch (msg.what) { + case MSG_START: { + if (mFrameIndex >= 0 && mFrameIndex < mResourceIdList.size() && mState == STATE_RUNNING) { + //这里不能使用image.setImageResource 因为源码中也是创建了bitmap 所以这里我们自己创建 + Bitmap bitmap=readBitMap(mImageView.getContext(),mResourceIdList.get(mFrameIndex)); + mImageView.setImageBitmap(bitmap); + mFrameIndex++; + } + } + break; + case MSG_STOP: { + if (mTimeTask != null) { + mFrameIndex = 0; + mTimer.purge(); + mTimeTask.cancel(); + mState = STATE_STOP; + mTimeTask = null; + if (lisenter !=null){ + lisenter.endAnimation(); + } + if (isLooping) { + mImageView.setImageResource(0); + } + } + } + break; + default: + break; + } + } + }; + + public static Bitmap readBitMap(Context context, int resId) { + BitmapFactory.Options opt = new BitmapFactory.Options(); + opt.inPreferredConfig = Bitmap.Config.RGB_565; + opt.inPurgeable = true; + opt.inInputShareable = true; + InputStream is = context.getResources().openRawResource(resId); + return BitmapFactory.decodeStream(is, null, opt); + } + + public interface OnParseListener { + void onParse(List res); + } + + public interface AnimationLisenter { + void startAnimation(); + + void endAnimation(); + } +} diff --git a/OCH/mogo-och-taxi-passenger/src/main/java/com/mogo/och/taxi/passenger/ui/TaxiPassengerStartAutopilotView.java b/OCH/mogo-och-taxi-passenger/src/main/java/com/mogo/och/taxi/passenger/ui/TaxiPassengerStartAutopilotView.java index 008854287c..41146e5982 100644 --- a/OCH/mogo-och-taxi-passenger/src/main/java/com/mogo/och/taxi/passenger/ui/TaxiPassengerStartAutopilotView.java +++ b/OCH/mogo-och-taxi-passenger/src/main/java/com/mogo/och/taxi/passenger/ui/TaxiPassengerStartAutopilotView.java @@ -4,7 +4,6 @@ import static com.mogo.eagle.core.utilcode.mogo.logger.scene.SceneConstant.M_TAX import android.annotation.SuppressLint; import android.content.Context; -import android.graphics.drawable.AnimationDrawable; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; @@ -15,10 +14,10 @@ import com.elegant.utils.UiThreadHandler; import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger; import com.mogo.eagle.core.utilcode.util.OverlayViewUtils; import com.mogo.eagle.core.utilcode.util.ToastUtils; +import com.mogo.och.common.module.utils.AnimatorDrawableUtil; import com.mogo.och.common.module.wigets.sfv.FrameSurfaceView; import com.mogo.och.taxi.passenger.R; import com.mogo.och.taxi.passenger.callback.ITPClickStartAutopilotCallback; -import com.mogo.och.taxi.passenger.model.TaxiPassengerModel; import java.util.Arrays; @@ -35,11 +34,11 @@ public class TaxiPassengerStartAutopilotView extends RelativeLayout implements V private ImageView mCloseIV; private ITPClickStartAutopilotCallback mClickCallback; public boolean isStarting = false; - private AnimationDrawable mAnimationBtnDrawable; private static final long TIMER_START_AUTOPILOT_INTERVAL = 20 * 1000L; private Context mContext; private View view; private FrameSurfaceView svCarStartingFrame; + private AnimatorDrawableUtil mAnimationDrawable ; private Integer[] startingAnimIds = new Integer[]{ R.drawable.light_00000, R.drawable.light_00001, @@ -56,6 +55,33 @@ public class TaxiPassengerStartAutopilotView extends RelativeLayout implements V R.drawable.light_00012, R.drawable.light_00013 }; + private Integer[] startBtnBgAnimIds = new Integer[]{ + R.drawable.image_00000, R.drawable.image_00001, R.drawable.image_00002, R.drawable.image_00003, + R.drawable.image_00004, R.drawable.image_00005, R.drawable.image_00006, R.drawable.image_00007, + R.drawable.image_00008, R.drawable.image_00009, R.drawable.image_00010, R.drawable.image_00011, + R.drawable.image_00012, R.drawable.image_00013,R.drawable.image_00014,R.drawable.image_00015, + R.drawable.image_00016, R.drawable.image_00017,R.drawable.image_00018,R.drawable.image_00019, + R.drawable.image_00020, R.drawable.image_00021,R.drawable.image_00022,R.drawable.image_00023, + R.drawable.image_00024, R.drawable.image_00025,R.drawable.image_00026,R.drawable.image_00027, + R.drawable.image_00028, R.drawable.image_00029,R.drawable.image_00030,R.drawable.image_00031, + R.drawable.image_00032, R.drawable.image_00033,R.drawable.image_00034,R.drawable.image_00035, + R.drawable.image_00036, R.drawable.image_00037,R.drawable.image_00038,R.drawable.image_00039, + R.drawable.image_00040, R.drawable.image_00041,R.drawable.image_00042,R.drawable.image_00043, + R.drawable.image_00044, R.drawable.image_00045,R.drawable.image_00046,R.drawable.image_00047, + R.drawable.image_00048, R.drawable.image_00049,R.drawable.image_00050,R.drawable.image_00051, + R.drawable.image_00052, R.drawable.image_00053,R.drawable.image_00054,R.drawable.image_00055, + R.drawable.image_00056, R.drawable.image_00057,R.drawable.image_00058,R.drawable.image_00059, + R.drawable.image_00060, R.drawable.image_00061,R.drawable.image_00062,R.drawable.image_00063, + R.drawable.image_00064, R.drawable.image_00065,R.drawable.image_00066,R.drawable.image_00067, + R.drawable.image_00068,R.drawable.image_00069,R.drawable.image_00070, R.drawable.image_00071, + R.drawable.image_00072,R.drawable.image_00073, R.drawable.image_00074, R.drawable.image_00075, + R.drawable.image_00076,R.drawable.image_00077,R.drawable.image_00078,R.drawable.image_00079, + R.drawable.image_00080, R.drawable.image_00081, R.drawable.image_00082,R.drawable.image_00083, + R.drawable.image_00084, R.drawable.image_00085, R.drawable.image_00086,R.drawable.image_00087, + R.drawable.image_00088, R.drawable.image_00089, R.drawable.image_00090,R.drawable.image_00091, + R.drawable.image_00092, R.drawable.image_00093, R.drawable.image_00094,R.drawable.image_00095, + R.drawable.image_00096, R.drawable.image_00097, R.drawable.image_00098,R.drawable.image_00099, + }; public TaxiPassengerStartAutopilotView(Context context) { super(context); @@ -73,16 +99,20 @@ public class TaxiPassengerStartAutopilotView extends RelativeLayout implements V mAutopilotBtnBg = view.findViewById(R.id.taxi_p_autopilot_btn_bg); + initBtnAnimatonDrawable(); + + initCarStartingFrame(); + } + + private void initCarStartingFrame() { svCarStartingFrame = view.findViewById(R.id.taxi_p_autopilot_starting); svCarStartingFrame.setBitmapIds(Arrays.asList(startingAnimIds)); svCarStartingFrame.setDuration(1300); -// svCarStartingFrame.setOnLongClickListener(new OnLongClickListener() { -// @Override -// public boolean onLongClick(View v) { -// TaxiPassengerModel.getInstance().startServicePilotDone(); -// return false; -// } -// }); + } + + private void initBtnAnimatonDrawable() { + mAnimationDrawable = new AnimatorDrawableUtil(); + mAnimationDrawable.setAnimation(mAutopilotBtnBg,Arrays.asList(startBtnBgAnimIds)); } public void setOnClickStartAutopilotBtnCallback(ITPClickStartAutopilotCallback clickCallback){ @@ -132,7 +162,6 @@ public class TaxiPassengerStartAutopilotView extends RelativeLayout implements V mStartAutopilotBtn.setTextColor( mContext.getResources().getColor(R.color.taxi_p_start_autopilot_txt_color)); mStartAutopilotBtn.setBackground(null); - mAutopilotBtnBg.setBackground(mContext.getResources().getDrawable(R.drawable.anmi_flow)); }else { mStartAutopilotBtn.setBackground( mContext.getResources().getDrawable(R.drawable.taxi_p_start_autopilot_txt_btn_bg)); @@ -148,15 +177,12 @@ public class TaxiPassengerStartAutopilotView extends RelativeLayout implements V public void startAutopilotBgAnimatorDrawable(boolean isStart){ if (isStart){ - if (mAnimationBtnDrawable == null) { - mAnimationBtnDrawable = (AnimationDrawable) mAutopilotBtnBg.getBackground(); + if (mAnimationDrawable == null){ + } - if (mAnimationBtnDrawable.isRunning()) { - CallerLogger.INSTANCE.d(M_TAXI_P+TAG,"mAnimationBtnDrawable.isRunning() = true"); - return; + if (mAnimationDrawable != null){ + mAnimationDrawable.start(true, 30, null); } - mAnimationBtnDrawable.selectDrawable(-1); - mAnimationBtnDrawable.start(); }else { clearBgAnimDrawable(); } @@ -194,10 +220,9 @@ public class TaxiPassengerStartAutopilotView extends RelativeLayout implements V } public void clearBgAnimDrawable() { - if (mAnimationBtnDrawable != null) { - mAnimationBtnDrawable.stop(); + if (mAnimationDrawable != null){ + mAnimationDrawable.stop(); } - mAnimationBtnDrawable = null; } public void closeAllAnimsAndView(){