<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>循环次数归档 - 日志</title>
	<atom:link href="https://www.log.show/tag/%E5%BE%AA%E7%8E%AF%E6%AC%A1%E6%95%B0/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.log.show/tag/循环次数/</link>
	<description>LOG.SHOW</description>
	<lastBuildDate>Fri, 05 Dec 2025 01:37:44 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://www.log.show/wp-content/uploads/2025/11/cropped-logo-32x32.png</url>
	<title>循环次数归档 - 日志</title>
	<link>https://www.log.show/tag/循环次数/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>解决Glide加载Loop count（循环次数）为1的gif依然多次播放的问题</title>
		<link>https://www.log.show/log/glide-load-image-loop-count-one-time/</link>
					<comments>https://www.log.show/log/glide-load-image-loop-count-one-time/#comments</comments>
		
		<dc:creator><![CDATA[LOGGER]]></dc:creator>
		<pubDate>Wed, 19 Nov 2025 05:36:43 +0000</pubDate>
				<category><![CDATA[LOG]]></category>
		<category><![CDATA[Android开发]]></category>
		<category><![CDATA[GifDrawable]]></category>
		<category><![CDATA[GIF播放]]></category>
		<category><![CDATA[Glide]]></category>
		<category><![CDATA[LOOP_INTRINSIC]]></category>
		<category><![CDATA[元数据]]></category>
		<category><![CDATA[动画控制]]></category>
		<category><![CDATA[图片加载]]></category>
		<category><![CDATA[循环次数]]></category>
		<category><![CDATA[聊天界面]]></category>
		<guid isPermaLink="false">https://print.surf/?p=258</guid>

					<description><![CDATA[<p>问题出现 在使用Glide加载一个聊天页面的表情包。之前的表情包都是循环播放，没有任何问题。现在加入了猜拳和骰 [&#8230;]</p>
<p><a href="https://www.log.show/log/glide-load-image-loop-count-one-time/">解决Glide加载Loop count（循环次数）为1的gif依然多次播放的问题</a>最先出现在<a href="https://www.log.show">日志</a>。</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">问题出现</h2>



<p class="wp-block-paragraph">在使用Glide加载一个聊天页面的表情包。之前的表情包都是循环播放，没有任何问题。现在加入了猜拳和骰子，这类表情包需要播放到1次末尾的时候停止播放，进而展示当前随机到的内容是什么。</p>



<p class="wp-block-paragraph">现在使用的方案是使用只会播放1次的gif进行加载。下面是我的加载代码：</p>



<pre class="wp-block-code"><code>            GlideApp.with(tv.context)
                .asGif()
                .load(msgDto.msg)
                .override(dp2px(tv.context, 25), dp2px(tv.context, 25))
                .into(imageView)</code></pre>



<p class="wp-block-paragraph">那么现在的问题是什么呢？</p>



<ul class="wp-block-list">
<li>加载Loop count = 0的gif，循环播放</li>



<li>加载loop count = 1的gif，会出现有的播放一次，有的循环播放</li>
</ul>



<h2 class="wp-block-heading">解决问题</h2>



<h3 class="wp-block-heading">尝试方案</h3>



<p class="wp-block-paragraph">我第一时间猜测是GIF图片的播放次数问题。因此我使用了ezgif.com ，Photoshop，在线PS等各种工具进行文件导出，尝试重新设置图片循环次数。</p>



<p class="wp-block-paragraph">结果图片循环次数是加上了，但是Android不可以单次播放。（ios正常加载）</p>



<p class="wp-block-paragraph">通常的排除法此时失效：</p>



<ol class="wp-block-list">
<li>iOS可用，Android不可以，可以排除图片的问题❓</li>



<li>其他播放一次的gif可以适配，这个只播放一次的gif适配不了，可以排除加载方式的问题❓</li>
</ol>



<p class="wp-block-paragraph">问题不难，真的折磨人。下面是解决思路：</p>



<h3 class="wp-block-heading">问题原因</h3>



<p class="wp-block-paragraph">根本原因在于不同的GIF文件在制作时，对于“Loop Count（循环次数）”的元数据（Metadata）写入标准不一致，或者部分文件缺少该头部信息。</p>



<p class="wp-block-paragraph">当Glide加载这些文件时，如果没有显式指定行为，<code>GifDrawable</code> 可能会根据缺省设置或者被复用的View状态，导致某些本该只播放一次的图变成了无限循环。</p>



<p class="wp-block-paragraph">为了通过<strong>同一个语句</strong>完美支持“无限循环”和“只播放一次”的GIF，你需要显式地告诉 Glide：<strong>“请严格按照GIF文件原本定义的次数播放（Intrinsic）”。</strong></p>



<p class="wp-block-paragraph">下面可用的加载代码：</p>



<pre class="wp-block-code"><code>import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException

Glide.with(tv.context)
    .asGif()
    .load(msgDto.msg)
    .override(dp2px(tv.context, 25), dp2px(tv.context, 25))
    .listener(object : RequestListener&lt;GifDrawable> {
        override fun onLoadFailed(
            e: GlideException?, 
            model: Any?, 
            target: Target&lt;GifDrawable>?, 
            isFirstResource: Boolean
        ): Boolean {
            return false
        }

        override fun onResourceReady(
            resource: GifDrawable?, 
            model: Any?, 
            target: Target&lt;GifDrawable>?, 
            dataSource: DataSource?, 
            isFirstResource: Boolean
        ): Boolean {
            resource?.setLoopCount(GifDrawable.LOOP_INTRINSIC)
            return false
        }
    })
    .into(imageView)</code></pre>



<p class="wp-block-paragraph">基本可以解决很多问题。</p>
<p><a href="https://www.log.show/log/glide-load-image-loop-count-one-time/">解决Glide加载Loop count（循环次数）为1的gif依然多次播放的问题</a>最先出现在<a href="https://www.log.show">日志</a>。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.log.show/log/glide-load-image-loop-count-one-time/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
