android:實現應用插件內換膚操作

故名思議:插件式換膚就是從插件包中獲取替換資源

  第一步:將資源文件以apk格式存在,它不必存在任何代碼,只需要在drawable下面存在我們想要的資源文件即可,

apk的生成就十分簡單。

 


第二步:新建一個skin_plugin庫

ResourcesManager做爲插件資源更換的類

public class ResourcesManager {

    private Resources mResources;
    private String mPkgName;
    public ResourcesManager(Resources resources, String pkgName)
    {
        mResources = resources;
        mPkgName = pkgName;
    }

    public Drawable getDrawableByResName(String name)
    {
        try{

            return mResources.getDrawable(mResources.getIdentifier(name,"drawable",mPkgName));

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        }

    public ColorStateList getColorByResName(String name)
    {
        try{
            return mResources.getColorStateList(mResources.getIdentifier(name,"color",mPkgName));

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
第三步:準備一個空的庫,無任何代碼,準備做替換的庫來使用

第四步:DEMO中使用方法

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private String mSkinPluginPath =
            Environment.getExternalStorageDirectory() + File.separator + "more_skin.apk";
    private String mSkinPluginPkg ="com.skin.plugin";  

    public DrawerLayout drawer =null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        //DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            updatePlugin(mSkinPluginPath,mSkinPluginPkg);
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    private void updatePlugin(String mSkinPluginPath, String mSkinPluginPkg) {

        try {
            AssetManager assetManager = AssetManager.class.newInstance();
            Method addAssetPathMethod =assetManager.getClass().getMethod("addAssetPath",String.class);
            addAssetPathMethod.invoke(assetManager,mSkinPluginPath);

            Resources superResources = getResources();

            Resources resources= new Resources(
                    assetManager,
                    superResources.getDisplayMetrics(),
                    superResources.getConfiguration()
                    );
            ResourcesManager resourcesManager = new ResourcesManager(resources,mSkinPluginPkg);
            Drawable drawable = resourcesManager.getDrawableByResName("skin_main_bg");

            if(drawable != null){

                drawer.setBackgroundDrawable(drawable);

            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
} 

第五步:權限是必須要有的

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

實現效果:

原效果



插件式換膚後效果


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章