加载插件资源的Demo

news/2024/7/2 1:44:36

首先了解一下Google加载资源源码

效果图
这里写图片描述
ImageView中加载src源码

final Drawable d = a.getDrawable(R.styleable.ImageView_src);

getDrawable源码

 if (getValueAt(index*AssetManager.STYLE_NUM_ENTRIES, value)) {
            if (value.type == TypedValue.TYPE_ATTRIBUTE) {
                throw new UnsupportedOperationException(
                        "Failed to resolve attribute at index " + index + ": " + value);
            }
            return mResources.loadDrawable(value, value.resourceId, mTheme);
        }

可以看到最终调用Resource中的loadDrawable方法

获取getResources源码,目的了解获取resource怎么去实例化的,一步一步走下去

 @Override
    public Resources getResources() {
        return mBase.getResources();
    }

发现调用的Content中的抽象方法getResource方法

查看ContextImpl中的getResource的方法

  @Override
    public Resources getResources() {
        return mResources;
    }

查看mResource怎么被赋值的
mResources = resources;
Resources resources = packageInfo.getResources(mainThread);

getResource源码

 public Resources getResources(ActivityThread mainThread) {
        if (mResources == null) {
            mResources = mainThread.getTopLevelResources(mResDir, mSplitResDirs, mOverlayDirs,
                    mApplicationInfo.sharedLibraryFiles, Display.DEFAULT_DISPLAY, this);
        }
        return mResources;
    }

查看ActivityThread中getTopLevelResources源码

 Resources getTopLevelResources(String resDir, String[] splitResDirs, String[] overlayDirs,
            String[] libDirs, int displayId, Configuration overrideConfiguration,
            LoadedApk pkgInfo) {
        return mResourcesManager.getTopLevelResources(resDir, splitResDirs, overlayDirs, libDirs,
                displayId, overrideConfiguration, pkgInfo.getCompatibilityInfo(), null);
    }

查看ResourceManager中getTopLevelResources源码

 r = new Resources(assets, dm, config, compatInfo, token);
 AssetManager assets = new AssetManager();
  if (resDir != null) {
            if (assets.addAssetPath(resDir) == 0) {
                return null;
            }
        }

资源加载的总结:所有的资源加载通过Resource -> 构建对象是直接new的对象 -> AssetManager 其实是Resource的核心实例 -> 最终是通过AssetManager获取

案例:首先我们准备资源的apk,修改apk名字为meinv.skin:保存到本地手机中,我这里是直接拖到虚拟机/Download目录下的
代码很简单,直接粘贴了,注意源码中有{@hide}代表不能直接new必须通过反射获取实例

public void onClick(View view){
     Resources superResource = getResources();

     try {
         AssetManager assets = AssetManager.class.newInstance();//{@hide}

         Method method = AssetManager.class.getDeclaredMethod("addAssetPath", String.class);
         method.invoke(assets, Environment.getExternalStorageDirectory().getAbsolutePath()
                 + File.separator + "Download"+File.separator+"meinv.skin");

         Resources resources = new Resources
                 (assets, superResource.getDisplayMetrics(), superResource.getConfiguration());
         //获得资源的id
         int resourceId = resources.getIdentifier("image_src", "drawable", "com.hbwj.kanyuanma");
         Drawable drawable = resources.getDrawable(resourceId);
         skin_imageView.setImageDrawable(drawable);
     } catch (Exception e) {
         e.printStackTrace();
     }
 }

http://www.niftyadmin.cn/n/3648846.html

相关文章

Git使用教程详解之三 Git分支

Git 分支 几乎每一种版本控制系统都以某种形式支持分支。使用分支意味着你可以从开发主线上分离开来,然后在不影响主线的同时继续工作。在很多版本控制系统中,这是个昂贵的过程,常常需要创建一个源代码目录的完整副本,对大型项目来…

DigitalOcean电子书:面向全栈开发人员的Kubernetes

Download the Complete eBook!下载完整的电子书! Kubernetes for Full-Stack Developers eBook in EPUB format 适用于EPUB格式的 Full-Stack Developers电子书的Kubernetes Kubernetes for Full-Stack Developers eBook in PDF format Kubernetes适用于PDF格式的 …

换肤框架的搭建

首先所有皮肤的view——skinView:如ImageView public class SkinView {private View mSkView;//ImageViewprivate List<SkinAttr> mSkinAttrs;//src,backgroudpublic SkinView(View mSkView, List<SkinAttr> mSkinAttrs) {this.mSkView mSkView;this.mSkinAttrs …

javascript 符号_通过JavaScript了解Big O符号

javascript 符号If you’ve ever looked into getting a job as a developer you’ve probably come across this Google interview at some point and wondered ‘what the heck are they talking about?’. In this article, we’re going to explore what they mean throwi…

Windows系统一些计数器

Windows系统Windows -Processor指标名称指标描述指标范围指标单位CPU利用率&#xff08;% Processor Time&#xff09;% Processor Time指处理器执行非闲置线程时间的百分比。这个计数器设计成用来作为处理器活动的主要指示器。它通过在每个时间间隔中衡量处理器用于执行闲置处…

Git使用教程详解之四 服务器上的Git

服务器上的 Git 到目前为止&#xff0c;你应该已经学会了使用 Git 来完成日常工作。然而&#xff0c;如果想与他人合作&#xff0c;还需要一个远程的 Git 仓库。尽管技术上可以从个人的仓库里推送和拉取修改内容&#xff0c;但我们不鼓励这样做&#xff0c;因为一不留心就很容易…

使用JavaScript FileReader API读取和处理文件

Reading, writing and analyzing files is an essential component of software development. For security reasons, in JavaScript, we can’t directly access users’ files. If we had something like fs in Node.js, we could just steal documents from users! 读取&…

耍流氓式的保活service

QQ为什么一直常驻后台? &#xff08;白名单&#xff0c;双进程守护) 应用正在运行&#xff0c;这个时候内存不足回收杀进程 1.提高进程的优先级&#xff0c;其实就是减小进程的p->oomkilladj&#xff08;越小越重要&#xff09;&#xff0c;如启动Service调用startForeg…