Android 开发进阶

Android 读取网络链接的 9.png 图片,功能实现

2021-03-18  本文已影响0人  JeffreyWorld

关键点在于,在服务器上传的图片,要经过 aapt 的命令行编译。网上也有说一些命令行,但是不是很好使。最终找的解决方案是,打一个 debug 的 apk 包,然后解压缩后在资源文件中取得要用的 9.png 的图片,这个图片实际上是经过 aapt 编译转义后的图片。用转义后的图片上传到服务器,配置上图片链接,就可以正常使用了。

使用场景:APP 的商城里支持购买聊天气泡等。

核心代码流程:

   private void loadBubbleRes(Context context, String download, String bubbleId) {
        String filePath = SJFileUtils.getBubbleCachePath(context) + File.separator;
        String localPath = filePath + bubbleId + ".9.png";
        try {
            Observable.create((ObservableOnSubscribe<Boolean>) emitter -> {
                boolean sizeCompare = SJFileUtils.getFileOrFilesSize(localPath, SJFileUtils.SIZETYPE_B) != SJFileUtils.getNetFileSize(download, SJFileUtils.SIZETYPE_B);
                boolean result = !SJFileUtils.isFileExists(localPath) || sizeCompare;
                emitter.onNext(result);
                emitter.onComplete();
            }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(aBoolean -> {
                if (aBoolean) {
                    DownloadFileUtils.with().url(download).downloadPath(localPath).execute(new AbsFileProgressCallback() {
                        @Override
                        public void onSuccess(String result) {
                            ((Activity)context).runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    bubbleLayout.setBackground(BitmapUtils.loadNinePatch(localPath, context));
                                }
                            });
                        }

                        @Override
                        public void onProgress(long bytesRead, long contentLength, boolean done) {

                        }

                        @Override
                        public void onFailed(String errorMsg) {

                        }

                        @Override
                        public void onStart() {

                        }

                        @Override
                        public void onCancle() {
                        }
                    });
                } else {
                    ((Activity)context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            bubbleLayout.setBackground(BitmapUtils.loadNinePatch(localPath, context));
                        }
                    });
                }
            }, throwable -> {

            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Drawable loadNinePatch(String path, Context context) {
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        byte[] chunk = bitmap.getNinePatchChunk();
        if(NinePatch.isNinePatchChunk(chunk)) {
            return new NinePatchDrawable(context.getResources(), bitmap, chunk, new Rect(), null);
        } else return new BitmapDrawable(bitmap);
    }
上一篇 下一篇

猜你喜欢

热点阅读