公司--下载svg图片

简介:

加载本地svg图片:

复制代码
SVGParserRenderer norDrawable = 
OtherPageConfigsManager.getInstance().getSVGParserRenderer(this,map.get("iconUrlNor"));


public SVGParserRenderer getSVGParserRenderer(Context context, String svgPath){
    String config = "";
    if(iconLoadPathType == SAVE_TYPE_ASSETS)//本地存储位置
        config = FileSystem.getFromAssets(context, "panelConfigFolder/"+svgPath);
    else if(iconLoadPathType == SAVE_TYPE_SDCARD)//SDK存储位置
        config = "";//"file://"+FileSystem.getCacheRootDir(context, "").getPath()+ url;
    else if(iconLoadPathType == SAVE_TYPE_SYSTEM_DATA_FOLDER)//内部文件系统存储位置
        //modify by chris.lei  添加中台未配发右侧在线客服图标导致从内部文件中读取不到信息客户端首页显示空白的问题
        config = FileSystem.readFromFile(context, 
                getConfigFile(context, "panelConfigFolder", svgPath).getPath());
        if(config == ""){
            config = FileSystem.getFromAssets(context, "panelConfigFolder/"+svgPath);
        }
    return SvgRes1.getSVGParserRenderer(context, config);//从配置文件获取
}
    
    
public static SVGParserRenderer getSVGParserRenderer(Context context, String svgContent) {
    return svgContent != null && !svgContent.equals("")?new SVGParserRenderer(context, parseCssStyleToSvg(svgContent)):null;
}

public static String parseCssStyleToSvg(String svgContent) {
    int startIndex = svgContent.indexOf("<style");
    int endIndex = svgContent.indexOf("</style>") + "</style>".length();
    String styleStr = null;
    if(startIndex >= 0 && endIndex >= 0) {
        styleStr = svgContent.substring(startIndex, endIndex);  // styleStr = <style type="text/css"><![CDATA[.strokeColor{fill:#B0B0B0;}]]></style>
        
        if(styleStr != null && !styleStr.equals("")) {
            int startIndex1 = styleStr.indexOf(".");
            int endIndex1 = styleStr.lastIndexOf(".");
            int endendIndex = styleStr.lastIndexOf(";}") + ";}".length();
            String colorStr;
            if(startIndex1 == endIndex1) {
                colorStr = styleStr.substring(startIndex1 + ".".length(), endendIndex);
            } else {
                colorStr = styleStr.substring(startIndex1, endendIndex);
            }
                //colorStr = strokeColor{fill:#B0B0B0;}
            String[] fillColorArr = colorStr.split(";");    
            if(fillColorArr != null) {
                for(int i = 0; i < fillColorArr.length; ++i) {
                    if(fillColorArr[i].contains("fill:")) {
                        String[] nameColorArr = fillColorArr[i].split("fill:");
                        if(nameColorArr != null) {
                            int nameStartIndex = nameColorArr[0].indexOf(".") + ".".length();
                            String name = nameColorArr[0].substring(nameStartIndex, nameColorArr[0].length() - 1);
                            String color = nameColorArr[1];
                            if(svgContent.contains("class=\"" + name + "\"")) {
                                svgContent = svgContent.replace("class=\"" + name + "\"", "fill=\"" + color + "\"");
                            }
                        }
                    }
                }
            }
        }

        svgContent = svgContent.replace(styleStr, "");
    }

    return svgContent;
}
    
parseCssStyleToSvg的行参 svgContent =    
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="图层_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="48px" height="48px" viewBox="0 0 48 48" enable-background="new 0 0 48 48" xml:space="preserve">
<style type="text/css">
<![CDATA[
    .strokeColor{fill:#B0B0B0;}
]]>
</style>
<path class="strokeColor" d="M3.494,24c0-0.587,0.476-1.063,1.063-1.063h38.886c0.588,0,1.063,0.476,1.063,1.063l0,0
    c0,0.587-0.475,1.062-1.063,1.062H4.557C3.97,25.063,3.494,24.587,3.494,24L3.494,24z"/>
<g>
    <g>
        <g>
            <path class="strokeColor" d="M3.806,23.249c0.415-0.414,1.088-0.414,1.502,0l17.054,17.053c0.415,0.415,0.415,1.087,0,1.503l0,0
    c-0.415,0.414-1.088,0.414-1.503,0L3.806,24.751C3.391,24.336,3.391,23.665,3.806,23.249L3.806,23.249z"/>
        </g>
        <g>
            <path class="strokeColor" d="M22.361,6.196c0.415,0.415,0.415,1.087,0,1.502L5.308,24.751c-0.414,0.415-1.087,0.415-1.502,0l0,0
    c-0.415-0.415-0.415-1.087,0-1.503L20.858,6.196C21.273,5.781,21.946,5.781,22.361,6.196L22.361,6.196z"/>
        </g>
    </g>
</g>
</svg>


返回 svgContent = <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="图层_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
     width="48px" height="48px" viewBox="0 0 48 48" enable-background="new 0 0 48 48" xml:space="preserve">
<path fill="#B0B0B0" d="M3.494,24c0-0.587,0.476-1.063,1.063-1.063h38.886c0.588,0,1.063,0.476,1.063,1.063l0,0
    c0,0.587-0.475,1.062-1.063,1.062H4.557C3.97,25.063,3.494,24.587,3.494,24L3.494,24z"/>
<g>
    <g>
        <g>
            <path fill="#B0B0B0" d="M3.806,23.249c0.415-0.414,1.088-0.414,1.502,0l17.054,17.053c0.415,0.415,0.415,1.087,0,1.503l0,0
                c-0.415,0.414-1.088,0.414-1.503,0L3.806,24.751C3.391,24.336,3.391,23.665,3.806,23.249L3.806,23.249z"/>
        </g>
        <g>
            <path fill="#B0B0B0" d="M22.361,6.196c0.415,0.415,0.415,1.087,0,1.502L5.308,24.751c-0.414,0.415-1.087,0.415-1.502,0l0,0
                c-0.415-0.415-0.415-1.087,0-1.503L20.858,6.196C21.273,5.781,21.946,5.781,22.361,6.196L22.361,6.196z"/>
        </g>
    </g>
</g>
</svg>
复制代码

下载网络svg图片:

复制代码
if (norDrawable == null) {
    panelConfigsDownloader.startDownloadForSvgIcon(this,
            OtherPageConfigsManager.getInstance(),
            map.get("downloadUrl") + norFilePath, saveNorFilePath,
            new SvgIconOnDownloadCompleteListener(
                    mKdsShortcutView[index], map, "iconUrlNor"));
}


public void startDownloadForSvgIcon(Context context,ConfigsManager configsManager,String svgDownloadUrl, 
                                    String svgFilePath,
                                    OnDownloadCompleteListener onDownloadCompleteListener){
//下载配置文件
String parentFolder = configsManager.mConfigInfo.saveFolderName+"/";    //parentFolder = panelConfigFolder/
String fileName = "";
if(svgFilePath != null && !svgFilePath.equals("")){    //svgFilePath = ueditor/1431988818264.svg
    String[] saveFileDir = svgFilePath.split("/");
    for(int i = 0; i < saveFileDir.length; i++){
        if(saveFileDir[i].contains(".")){
            fileName = saveFileDir[i];   //fileName = 1431988818264.svg
            continue;
        }
        parentFolder += saveFileDir[i];  //parentFolder = panelConfigFolder/ueditor
    }
}else{
    return;//说明不是需要下载的文件
}

File file = configsManager.getConfigFile(context, parentFolder, fileName);   //file = panelConfigFolder/ueditor/1431988818264.svg
DownloadConfigFileThread downloadConfigFileThread = 
        new DownloadConfigFileThread(context, svgDownloadUrl, file, 
        onDownloadCompleteListener);
 if(!file.isDirectory() && !file.exists()) {//考虑已经存在同样名字的文件或者目录,
     //Logger.d("downloadConfigFile", "startDownloadForSvgIcon exists:"+file.exists());
     downloadConfigFileThread.start();//开始下载
 }else{
     Logger.i("快捷按钮配置文件", "警告:该文件存在相同文件名,不进行下载更新,请知晓!");
     downloadConfigFileThread.onDownloadComplete();//已经存在也视为完成
 }
}


public class DownloadConfigFileThread extends Thread
{
    public synchronized void run()
    {
        try
        {
            URL url = new URL(downurl);// http://113.78.134.110:21800/api/config/app/ui/otherpage/online/66099/100000
            // 创建连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.connect();
            // 获取文件大小
            int length = conn.getContentLength();//40674
            // 创建输入流
            InputStream is = conn.getInputStream();       
            //处理文件路径不存在的问题
            String dirFilePath = configFile.getPath();//  /data/data/dongzheng.szkingdom.android.phone/files/panelConfigFolder/otherpage_temp.json
            String childDirPath = "";
            String[] path = dirFilePath.split("/");
            for(int i = 0; i < path.length-1;i++){
                if(path[i].contains("."))
                    continue;   
                childDirPath += "/" + path[i];    
                File file = new File(childDirPath);
                if(!file.exists()) {//考虑已经存在同样名字的文件或者目录,
                    Logger.d("tag", "DownloadConfigFileThread mkdir newPath:"+file.getPath());
                    file.mkdir();
                    file.setExecutable(true, false);
                    file.setReadable(true, false);
                    file.setWritable(true, false);
                }
            }
            FileOutputStream fos = new FileOutputStream(configFile); // 输出到文件  /data/data/dongzheng.szkingdom.android.phone/files/panelConfigFolder/otherpage_temp.json
            // 缓存
            byte buf[] = new byte[1024];
            int count = 0;
            // 写入到文件中
            do
            {
                int numread = is.read(buf);
                count += numread;
                // 计算进度条位置 
                int progress = (int) (((float) count / length) * 100);
                float tempSpace = count / 1024.0f / 1024.0f;
                String downloadSpace = "";
                try{
                    downloadSpace = String.format("%.3f",tempSpace)+"M";// > 1.0f ? (tempSpace+"M") : (count / 1024.0f+"KB");
                }catch(Exception e){
                }             
                if (numread <= 0)
                {
                  //下载完成      
                    mHandler.removeMessages(0);
                    Message msg = Message.obtain();
                    msg.what = 0;
                    mHandler.sendMessage(msg);
                    break;
                }     
                // 写入文件
                fos.write(buf, 0, numread);
            } while (true);// 点击取消就停止下载.
            fos.close();
            is.close();
        }catch(Exception e){
            mHandler.removeMessages(1);
            mHandler.sendEmptyMessage(1);
            //e.printStackTrace();
        }
    }
}
复制代码

 


本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/5660310.html,如需转载请自行联系原作者

相关文章
|
8月前
|
前端开发 小程序
小程序使用canvas制作beas64图片
小程序使用canvas制作beas64图片
58 0
|
测试技术 Android开发
autojs动态修改图片src
牙叔教程 简单易懂
772 0
|
5天前
|
前端开发
前端实现视频或者图片直链下载
前端实现视频或者图片直链下载
77 0
|
11月前
【PhotoShop】导入webp格式图片
【PhotoShop】导入webp格式图片
330 0
|
Web App开发 XML 编解码
Logo小变动,心境大不同,SVG矢量动画格式网站Logo图片制作与实践教程(Python3)
曾几何时,SVG(Scalable Vector Graphics)矢量动画图在坊间称之为一种被浏览器诅咒的技术,只因为糟糕的硬件支持(IE),和没完没了的兼容性调优(Safari)。但是在2022年的今天,一切都不一样了,正所谓三十年河东,三十年河西,微软所研发的采用Chromium内核作为IE替代者的Edge浏览器已经有望超越Safari成为第二大桌面浏览器,而曾经因为不支持关键帧动画被人诟病的Safari也统一了标准,市面上除了老帮菜IE,几乎所有平台(包括移动端)都已经对SVG足够友好,这让我们可以放心大胆的在网站上应用SVG矢量动画图。
Logo小变动,心境大不同,SVG矢量动画格式网站Logo图片制作与实践教程(Python3)
|
编解码 前端开发 搜索推荐
Python3.7将普通图片(png)转换为SVG图片格式并且让你的网站Logo(图标)从此”动”起来
在之前的几篇文章中,介绍了业界中比较火爆的图片技术SVG(Scalable Vector Graphics),比如[Iconfont(矢量图标)+iconmoon(图标svg互转)配合javascript来打造属于自己的个性化社交分享系统](https://v3u.cn/a_id_143),我们可以使用svg来打造精美炫酷的分享小图标(icon),这一次我们使用python来将普通的静态的网站logo图片转换为带路径(path)的svg图片,这样就可以让网站logo能够变成动态的,作为一名不折腾不舒服斯基,一枚炫酷自带动画的网站logo自然能够满足我们的折腾欲,同时亦能击中我们的虚荣心。
Python3.7将普通图片(png)转换为SVG图片格式并且让你的网站Logo(图标)从此”动”起来
|
Web App开发 XML 人工智能
如何利用Chrome浏览器将 SVG 转成 PNG
怎么把 SVG 转成 PNG ?用 Chrome 吧,非常好用,还能设定自己想要的尺寸。
361 0
如何利用Chrome浏览器将 SVG 转成 PNG
|
Web App开发 移动开发 前端开发
把canvas标签里的图像下载成本地图片文件
把canvas标签里的图像下载成本地图片文件
498 0
把canvas标签里的图像下载成本地图片文件