Android 如何设置GIF在代码执行时平稳运行?
我有一个活动,需要显示一个GIF,而一些沉重的东西正在后面发生。我指的不是spash屏幕。Gif与其他视图的布局相同 用例:1。活动开始,我将GIF所在的视图可见性设置为TRUE 2。Forground服务启动,并向服务器发出一些请求(异步)以获取内容(此处为GIF块,我不知道为什么)。服务完成网络作业后,将调用回调,并将带有gif的视图设置为“消失” 如果我用进度条替换gif,冻结就不会发生。它会旋转直到我把它设定为“消失” 我使用Glide来显示gif。 下面是activity_base.xmlAndroid 如何设置GIF在代码执行时平稳运行?,android,performance,gif,Android,Performance,Gif,我有一个活动,需要显示一个GIF,而一些沉重的东西正在后面发生。我指的不是spash屏幕。Gif与其他视图的布局相同 用例:1。活动开始,我将GIF所在的视图可见性设置为TRUE 2。Forground服务启动,并向服务器发出一些请求(异步)以获取内容(此处为GIF块,我不知道为什么)。服务完成网络作业后,将调用回调,并将带有gif的视图设置为“消失” 如果我用进度条替换gif,冻结就不会发生。它会旋转直到我把它设定为“消失” 我使用Glide来显示gif。 下面是activity_base.x
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/inject_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- <pl.droidsonroids.gif.GifImageView-->
<!-- android:visibility="gone"-->
<!-- android:id="@+id/view_download_progress"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:background="@color/white"-->
<!-- android:src="@drawable/service_download_content"/>-->
<ProgressBar
android:visibility="gone"
android:id="@+id/view_download_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@color/white"/>
<!-- <ImageView-->
<!-- android:visibility="gone"-->
<!-- android:id="@+id/view_download_progress"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent"-->
<!-- android:background="@color/white" />-->
</RelativeLayout>
更新2:
在服务内执行的作业内调用改装回调后,已注释的方法会执行一些繁重的SQLite工作。如果我这样做,gif不会冻结
override fun onResponse(call: Call<ObjectsSchema>, response: Response<ObjectsSchema>) {
val cmsContent = response.body() as ObjectsSchema
// handleContent(cmsContent)
notifyOnDownloadSucceded()
}
override-fun-onResponse(调用:调用,响应:响应){
val cmsContent=response.body()作为ObjectsSchema
//手册内容(CMS内容)
NotifyOnDownloadSucceed()
}
我建议您使用滑动加载GIF。我在GitHub上构建了一个项目,该项目使用它在AlertDialog中加载GIF
我用协同程序解决了这个问题
fun executeAsync() {
mStlClubApi.downloadCmsBulkData(mUrl, mQuery)
.enqueue(object: Callback<ObjectsSchema> {
override fun onFailure(call: Call<ObjectsSchema>, t: Throwable) {
notifyOnDownloadFailed()
}
override fun onResponse(call: Call<ObjectsSchema>, response: Response<ObjectsSchema>) {
val cmsContent = response.body() as ObjectsSchema
CoroutineScope(IO).launch {
asyncHandleAndNotify(cmsContent)
}
}
})
}
private suspend fun asyncHandleAndNotify(cmsContent: ObjectsSchema) {
handleContent(cmsContent)
notifyOnDownloadSucceded()
}
abstract suspend fun handleContent(cmsContent: ObjectsSchema)
fun executeAsync(){
mStlClubApi.downloadCmsBulkData(mUrl,mQuery)
.enqueue(对象:回调{
覆盖失效时的乐趣(调用:调用,t:可丢弃){
notifyOnDownloadFailed()
}
覆盖fun onResponse(调用:调用,响应:响应){
val cmsContent=response.body()作为ObjectsSchema
协同观测(IO).发射{
AsynchHandleandNotify(cmsContent)
}
}
})
}
私有挂起功能AsynchHandleandNotify(cmsContent:ObjectsSchema){
手册内容(CMS内容)
NotifyOnDownloadSucceed()
}
摘要suspend fun handleContent(CMS内容:ObjectsSchema)
但是,如果作业是在前台服务的上下文中执行的,我仍然不知道为什么会阻止GIF,但我认为这是另一个问题和深入调试的主题。很难看不到代码,但听起来像glide和您的服务使用同一个线程。也许是okhttp中的设置?@IgnacioTomasCrespo我已经编辑了这个问题。这个项目相当大。如果你需要看一些具体的东西,请告诉我。
Glide.with(activity)
.load(imageUrl)
.into(customView.imageDialog)
fun executeAsync() {
mStlClubApi.downloadCmsBulkData(mUrl, mQuery)
.enqueue(object: Callback<ObjectsSchema> {
override fun onFailure(call: Call<ObjectsSchema>, t: Throwable) {
notifyOnDownloadFailed()
}
override fun onResponse(call: Call<ObjectsSchema>, response: Response<ObjectsSchema>) {
val cmsContent = response.body() as ObjectsSchema
CoroutineScope(IO).launch {
asyncHandleAndNotify(cmsContent)
}
}
})
}
private suspend fun asyncHandleAndNotify(cmsContent: ObjectsSchema) {
handleContent(cmsContent)
notifyOnDownloadSucceded()
}
abstract suspend fun handleContent(cmsContent: ObjectsSchema)