Commit 4e210681 authored by mengcuiguang's avatar mengcuiguang

添加gif图只展示一次

parent 480e3b02
...@@ -14,6 +14,7 @@ import com.mints.flowbox.ui.activitys.base.BaseActivity ...@@ -14,6 +14,7 @@ import com.mints.flowbox.ui.activitys.base.BaseActivity
import com.mints.flowbox.utils.ToastUtil import com.mints.flowbox.utils.ToastUtil
import com.mints.library.utils.CommonUtils import com.mints.library.utils.CommonUtils
import com.mints.library.utils.ConstantUtil import com.mints.library.utils.ConstantUtil
import com.mints.library.utils.GlideUtils
import kotlinx.android.synthetic.main.activity_aboutus.* import kotlinx.android.synthetic.main.activity_aboutus.*
import kotlinx.android.synthetic.main.header_layout.* import kotlinx.android.synthetic.main.header_layout.*
import java.util.* import java.util.*
...@@ -37,6 +38,12 @@ class AboutusActivity : BaseActivity(), View.OnClickListener { ...@@ -37,6 +38,12 @@ class AboutusActivity : BaseActivity(), View.OnClickListener {
iv_left_icon.visibility = View.VISIBLE iv_left_icon.visibility = View.VISIBLE
iv_left_icon.setImageResource(R.mipmap.ic_arrow_back) iv_left_icon.setImageResource(R.mipmap.ic_arrow_back)
// GlideUtils.loadOneTimeGif(this,R.drawable.wifi_speed,iv_left_icon,object:GlideUtils.GifListener{
// override fun gifPlayComplete() {
//
// }
// })
ivAboutasIcon.setOnLongClickListener { ivAboutasIcon.setOnLongClickListener {
ToastUtil.showLong( ToastUtil.showLong(
MintsApplication.getContext(), "自有渠道:" + CommonUtils.getAppMetaData( MintsApplication.getContext(), "自有渠道:" + CommonUtils.getAppMetaData(
......
...@@ -3,12 +3,23 @@ package com.mints.library.utils; ...@@ -3,12 +3,23 @@ package com.mints.library.utils;
import android.content.Context; import android.content.Context;
import android.widget.ImageView; import android.widget.ImageView;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide; import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority; import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.signature.ObjectKey; import com.bumptech.glide.signature.ObjectKey;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/** /**
* 描述:GlideUtils * 描述:GlideUtils
* 作者:孟崔广 * 作者:孟崔广
...@@ -71,4 +82,68 @@ public class GlideUtils { ...@@ -71,4 +82,68 @@ public class GlideUtils {
.priority(Priority.HIGH); .priority(Priority.HIGH);
Glide.with(mContext).load(url).apply(options).into(view); Glide.with(mContext).load(url).apply(options).into(view);
} }
//只加载一次Gif
public static void loadOneTimeGif(Context context, Object model, final ImageView imageView, final GifListener gifListener) {
Glide.with(context).asGif().load(model).listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
try {
Field gifStateField = GifDrawable.class.getDeclaredField("state");
gifStateField.setAccessible(true);
Class gifStateClass = Class.forName("com.bumptech.glide.load.resource.gif.GifDrawable$GifState");
Field gifFrameLoaderField = gifStateClass.getDeclaredField("frameLoader");
gifFrameLoaderField.setAccessible(true);
Class gifFrameLoaderClass = Class.forName("com.bumptech.glide.load.resource.gif.GifFrameLoader");
Field gifDecoderField = gifFrameLoaderClass.getDeclaredField("gifDecoder");
gifDecoderField.setAccessible(true);
Class gifDecoderClass = Class.forName("com.bumptech.glide.gifdecoder.GifDecoder");
Object gifDecoder = gifDecoderField.get(gifFrameLoaderField.get(gifStateField.get(resource)));
Method getDelayMethod = gifDecoderClass.getDeclaredMethod("getDelay", int.class);
getDelayMethod.setAccessible(true);
//设置只播放一次
resource.setLoopCount(1);
//获得总帧数
int count = resource.getFrameCount();
int delay = 0;
for (int i = 0; i < count; i++) {
//计算每一帧所需要的时间进行累加
delay += (int) getDelayMethod.invoke(gifDecoder, i);
}
imageView.postDelayed(new Runnable() {
@Override
public void run() {
if (gifListener != null) {
gifListener.gifPlayComplete();
}
}
}, delay);
} catch (NoSuchFieldException e) {
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return false;
}
}).into(imageView);
}
/**
* Gif播放完毕回调
*/
public interface GifListener {
void gifPlayComplete();
}
} }
...@@ -26,11 +26,11 @@ RELEASE_STORE_PASSWORD=mints.flowbox ...@@ -26,11 +26,11 @@ RELEASE_STORE_PASSWORD=mints.flowbox
RELEASE_STORE_FILE=mints_flowbox.jks RELEASE_STORE_FILE=mints_flowbox.jks
#ShareSDK #ShareSDK
RELEASE_SHARESDK_KEY=328d2edfe42fa RELEASE_SHARESDK_KEY=3387a05ec068c
RELEASE_SHARESDK_SECRET=3586272d2ddd7d9de497cdede529ced3 RELEASE_SHARESDK_SECRET=7e0819d930224b62c492fa32b84d42a2
#友盟 #友盟
RELEASE_UMENG_KEY=604869326ee47d382b7ae112 RELEASE_UMENG_KEY=60d2ef4d8a102159db781cd3
#JPush #JPush
RELEASE_JPUSH_KEY=d9e939e7ec22a9e11dcfc345 RELEASE_JPUSH_KEY=d9e939e7ec22a9e11dcfc345
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment