加载插件资源的Demo

首先了解一下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();
     }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前言项目里都会遇到几种页面,分别为中、无网络、无数据、出错四种情况,经常要使用,所以封成库引用了,方便使用,顺便分享出来。先看一下效果: 原理比较简单,继承FrameLayout,在xml渲染完成后,中、无网络、无数据、出错四个页面,根据需要控制显示哪一层,花了些时间,开了很多方法出来,支持很多属性的设置,算是比较实用,源码里已对各个方法的作用都了注释,就不做过多解释了,项目GitHub地址:https://github.com/weavey/LoadingLayoutDemo,感兴趣的可以看看,欢迎指出问题。使用方式gradle引用:compile 'com.lai.weavey:loadinglayout:1.2'使用说明LoadingLayout支持全局配置,对所有使用到的地方都起效,需要在Application中配置,如下: public class App extends Application {     @Override     public void onCreate() {         super.onCreate();         LoadingLayout.getConfig()                 .setErrorText("出错啦~请稍后重试!")                 .setEmptyText("抱歉,暂无数据")                 .setNoNetworkText("无网络连接,请检查您的网络···")                 .setErrorImage(R.mipmap.define_error)                 .setEmptyImage(R.mipmap.define_empty)                 .setNoNetworkImage(R.mipmap.define_nonetwork)                 .setAllTipTextColor(R.color.gray)                 .setAllTipTextSize(14)                 .setReloadButtonText("点我重试哦")                 .setReloadButtonTextSize(14)                 .setReloadButtonTextColor(R.color.gray)                 .setReloadButtonWidthAndHeight(150,40);     } }由于“中”的页面,可能每个App都不一样,因此,LoadingLayout支持自定义LoadingPage,如下: LoadingLayout.getConfig()      .setLoadingPageLayout(R.layout.define_loading_page);同时,为了适应个别界面的“特殊需求”,LoadingLayout也支持局部设置各种属性,仅对当前对象生效,不影响全局。如下:        LoadingLayout  loading = (LoadingLayout) findViewById(R.id.loading_layout);         loading.setLoadingPage(R.layout.define_loading_page)                 .setEmptyText("暂无报告数据")                 .setErrorText("")                 .setNoNetworkText("")                 .setErrorImage(R.mipmap.ic_launcher)                 .setErrorTextSize(16)                 .setReloadButtonText("点我重新哦"); //等等为ReloadButton设置监听:loadingLayout.setOnReloadListener(new LoadingLayout.OnReloadListener() {             @Override             public void onReload(View v) {             }         });设置显示的页面: loadingLayout.setStatus(LoadingLayout.Loading);//中  loadingLayout.setStatus(LoadingLayout.Empty);//无数据  loadingLayout.setStatus(LoadingLayout.Error);//错误  loadingLayout.setStatus(LoadingLayout.No_Network);//无网络  loadingLayout.setStatus(LoadingLayout.Success);//成功最后,在xml里面使用:<com.weavey.loading.lib.LoadingLayout     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingBottom="@dimen/activity_vertical_margin"     android:paddingLeft="@dimen/activity_horizontal_margin"     android:paddingRight="@dimen/activity_horizontal_margin"     android:paddingTop="@dimen/activity_vertical_margin"     app:isFirstVisible="true">     <TextView         android:background="@color/colorPrimary"         android:visibility="visible"         android:gravity="center"         android:textColor="@android:color/white"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:text="ContentView"/> </com.weavey.loading.lib.LoadingLayout>注意: (1)isFirstVisible属性用来控制contentView一开始是否隐藏,由于LoadingLayout原理是在xml渲染完成后在contentView上铺上三层View,因此,一开始如果不隐藏,等contentView渲染完成后调用: loadingLayout.setStatus(LoadingLayout.Loading); 会造成界面闪烁的效果,影响体验,因此默认将contentView隐藏,所以数据完成后一定要调用loadingLayout.setStatus(LoadingLayout.Success);,将contentView显示出来。这样也能解决未获取到数据的情况下,被用户看到杂乱无章的布局,个人还是比较喜欢默认隐藏contentView; (2)为了方便管理,LoadingLayout只能有一个直属子View,类似ScrollView,添两个直属子View会抛出异常throw new IllegalStateException("LoadingLayout can host only one direct child");; (3)由于AS会直接将自定义View的特性反应在预览界面,所以在使用LoadingLayout的时候,会无法看到被LoadingLayout包裹住的布局(默认为gone),因此也可以将isFirstVisible属性暂时设为true,预览布局。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值