可以加载Gif动画和png等图片的方法和控件

我司用的是钉钉管理软件,钉钉的引导页面是一个动画,所以上面要求我们的产品也要用GIF动画来引导,而且还要同时可以使用普通的png图片


在晚上搜索半天,在github上面找到一个叫GifImageView的自定义控件


那么剩下的我们就只需要

1、判断是不是gif动画

1.1、如果是就下载并且填装进GifImageView里面

2、如果是png

2.1、下载并且填充


二营长,把老子的意大利代码来上来

先判断是不是Gif动画(甄别是不是老子的意大利炮)

 final Guide_Result result = new Gson().fromJson(str, Guide_Result.class);
//                result.setMessage("http://img4.duitang.com/uploads/blog/201405/19/20140519115336_mQNyR.thumb.224_0.gif");
//                result.setMessage("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png");
//                获取图片bitmap后
//                如果网络和本地是同一张图片,什么都不做
//                如果不是同一张,下载网络上的图片替换到本地
//                如果是同一张,判断本地文件是否存在,不存在下载
                if (result !=null ){
                if (!RequestTool.ToMD5(result.getMessage()).equals((String) SPUtils.get(GuideActivity.this, "GuidePic", ""))) {
                    File file = new File((String) SPUtils.get(GuideActivity.this, "GuidePic", ""));
                    if (file.exists()) {
                        Log.e("321", "file.exists()");
                        file.delete();
                    }
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            SPUtils.put(GuideActivity.this, "GuidePic", RequestTool.ToMD5(result.getMessage()));
                            File file = new File((String) SPUtils.get(GuideActivity.this, "GuidePic", ""));
                            try {
                                file.createNewFile();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            if (checkURL(result.getMessage())) ;
                            {
                                String suffix = result.getMessage().substring(result.getMessage().length() - 4, result.getMessage().length());
                                Log.e("3321", suffix);
                                if (suffix.equals(".gif")) {
                                    saveGif(result.getMessage());
                                    SPUtils.put(GuideActivity.this, "Picformat", ".gif");
                                } else {
                                    saveBitmap(result.getMessage());
                                    SPUtils.put(GuideActivity.this, "Picformat", ".png");
                                }
                            }
                        }
                    }).start();
                } else {
                    File file = new File((String) SPUtils.get(GuideActivity.this, "GuidePic", ""));
                    if (!file.exists()) {
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                SPUtils.put(GuideActivity.this, "GuidePic", RequestTool.ToMD5(result.getMessage()));
                                if (checkURL(result.getMessage())) ;
                                {
                                    String suffix = result.getMessage().substring(result.getMessage().length() - 4, result.getMessage().length());
                                    Log.e("3321", suffix);
                                    if (suffix.equals(".gif")) {
                                        saveGif(result.getMessage());
                                        SPUtils.put(GuideActivity.this, "Picformat", ".gif");
                                    } else {
                                        saveBitmap(result.getMessage());
                                        SPUtils.put(GuideActivity.this, "Picformat", ".png");
                                    }
                                }
                            }
                        }).start();
                    }
                }
如果是的话进行下载(装填炮弹)

 try {
            URL iconUrl = new URL(url);
            URLConnection conn = iconUrl.openConnection();
            conn.connect();
            // 获得图像的字符流
            InputStream is = conn.getInputStream();
            byte[] b = getByte(is);
            File file = new File("/sdcard" + "/" + RequestTool.ToMD5(url));
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(b);
            fileOutputStream.close();
            is.close();// 关闭流
        } catch (Exception e) {
            e.printStackTrace();
        }
  private byte[] getByte(InputStream is) throws IOException {
        byte[] b = new byte[1024];
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        int len = -1;
        while ((len = is.read(b)) != -1) {
            byteArrayOutputStream.write(b, 0, len);
        }
        byteArrayOutputStream.close();
        is.close();
        return byteArrayOutputStream.toByteArray();
    }
这里Gif动画最后是存储到本地文件里面的
如果不是的话进行普通图片的下载(迫击炮准备)
 public void saveBitmap(String url) {
        Bitmap bm = null;
        Log.e("123", url.toString());
        try {
            URL iconUrl = new URL(url);
            URLConnection conn = iconUrl.openConnection();
            HttpURLConnection http = (HttpURLConnection) conn;

            int length = http.getContentLength();

            conn.connect();
            // 获得图像的字符流
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, length);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();// 关闭流
        } catch (Exception e) {
            e.printStackTrace();
        }
        ZZUtils.saveImageToGallery(bm, "/sdcard", RequestTool.ToMD5(url), false, false);
    }
最后不管是Gif还是png,都将其填充进去(开炮!!!)
  iv = (GifImageView) findViewById(R.id.guide_iv);
        // 如果加载的是gif动图,第一步需要先将gif动图资源转化为GifDrawable
        // 将gif图资源转化为GifDrawable
        GifDrawable gifDrawable = null;

//      如果是动态图进try方法,不是动态图抛出异常进入catch方法设置图片
        File f = new File("/sdcard" + "/" + SPUtils.get(this, "GuidePic", ""));
        if (!f.exists()) {
            iv.setImageResource(R.mipmap.defaut_loading);
        }
        try {
            File file = new File("/sdcard" + "/" + SPUtils.get(this, "GuidePic", ""));
            Log.e("123", file.toString());
            gifDrawable = new GifDrawable(file);
//            gifDrawable = new GifDrawable(getResources(), R.mipmap.bg_splash);
            // gif1加载一个动态图gif
            iv.setImageDrawable(gifDrawable);
        } catch (IOException e) {
            iv.setImageURI(Uri.parse("/sdcard" + "/" + SPUtils.get(this, "GuidePic", "")));
//            iv.setImageBitmap(ZZUtils.getImagefromSD("filepath", (String) SPUtils.get(GuideActivity.this, "GuidePic", "")));
        }


        // 如果是普通的图片资源,就像Android的ImageView set图片资源一样简单设置进去即可。
        // gif2加载一个普通的图片(如png,bmp,jpeg等等)
//            iv.setImageResource(R.mipmap.b_xinwen_press);
        if (iv.getDrawable() == null) {
            iv.setImageResource(R.mipmap.defaut_loading);
        }

和尚!你他娘的打歪了!!!




至此完毕~欢迎转载收藏以及cv操作。请注明出处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity 默认不支持 GIF 格式的动画,但是可以通过导入第三方插件来实现 GIF 动画加载和播放。 以下是两个比较常用的插件: 1. GifDecoder:可以在 Unity 中直接加载 GIF 文件,并返回一个 Texture2D 对象,可以将其赋值给 RawImage 或者 Sprite 等组件来实现播放。GifDecoder 插件的 GitHub 地址为:https://github.com/DataB4/GifDecoder。 2. SimpleGif:是一个功能强大的 GIF 解码库,支持解码、播放和编码 GIF 文件,并且提供了丰富的 API 接口供用户使用。SimpleGif 插件的 GitHub 地址为:https://github.com/marijnz/SimpleGif。 这里以 GifDecoder 插件为例,演示如何加载 GIF 动画: 1. 从 GitHub 上下载 GifDecoder 插件,并将其导入到 Unity 项目中。 2. 在需要加载 GIF 动画的场景中,创建一个 RawImage 对象,并将其命名为 gifImage。 3. 编写以下代码: ```csharp using UnityEngine; using System.Collections; using GifDecoderNamespace; public class GifDecoderTest : MonoBehaviour { public Texture2D[] gifFrames; public float fps = 10f; void Start () { // 从 Resources 文件夹中加载 GIF 文件 TextAsset gifFile = Resources.Load("yourGifFile") as TextAsset; // 调用 GifDecoder 插件解码 GIF 文件 GifDecoder gifDecoder = new GifDecoder(); gifDecoder.Decode(gifFile.bytes); // 获取 GIF 动画的帧数 int frameCount = gifDecoder.GetFrameCount(); gifFrames = new Texture2D[frameCount]; for (int i = 0; i < frameCount; i++) { // 获取指定帧的 Texture2D 对象 gifFrames[i] = gifDecoder.GetFrameTexture(i); } // 播放 GIF 动画 StartCoroutine(PlayGifAnimation()); } IEnumerator PlayGifAnimation() { while (true) { // 遍历 GIF 动画的所有帧 for (int i = 0; i < gifFrames.Length; i++) { // 将当前帧显示在 RawImage 组件中 RawImage gifImage = GetComponent<RawImage>(); gifImage.texture = gifFrames[i]; // 等待一定时间后切换到下一帧 yield return new WaitForSeconds(1f / fps); } } } } ``` 根据需要,你可以调整代码中的参数来控制 GIF 动画的播放速度和循环方式等。请将 `yourGifFile` 替换为实际的 GIF 文件名。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值