安卓开发--使用GIF动图

1 使用ImageView布局

布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/gif_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>

</FrameLayout>

2 添加 GIF 到项目文件

\app\src\main\res\raw\文件中放置我们的动图资源video.gif

3 在活动文件中使用 GIF 资源

代码如下:

package com.example.ecgphone.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.example.ecgphone.R;

public class OtherFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_other, container, false);

        ImageView gifImageView = view.findViewById(R.id.gif_image_view);

        // 使用Glide加载GIF图片
        Glide.with(this)
                .asGif()
                .load(R.raw.video) // 这里放置你的GIF图片URL
                .into(gifImageView);

        return view;
    }
}

4 补充

如果你遇到了"Glide"无法解析的问题Cannot resolve symbol 'Glide',可能是因为你的项目中没有正确添加Glide库的依赖项。
在你的项目的build.gradle文件中添加了Glide的依赖项,如下所示:

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.12.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
}

而在Kotlin中,使用.kts文件扩展名表示Kotlin脚本文件。应该确保build.gradle.kts文件正确地使用了Kotlin语法。

dependencies {
    implementation("com.github.bumptech.glide:glide:4.12.0")
    implementation("com.github.bumptech.glide:compiler:4.12.0")
}

最终效果

  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值