開篇介紹
現(xiàn)在項(xiàng)目比較大 資源比較多卿吐,但是若希望動(dòng)態(tài)來加載資源文件屯阀,可以有以下幾種方式:
- 通過下載資源文件zip包然后解壓來加載
- 通過插件開發(fā)
本文通過插件開發(fā)來實(shí)現(xiàn)加載插件中的資源文件.
程序演示
可以打開鏈接 [效果演示](http://weibo.com/tv/v/EzNwkq0oP?fid=1034:8c610fcd0d501a61a67ddf56da6e4225"optional title")
打開后顯示2個(gè)動(dòng)畫鲫趁,上面的動(dòng)畫是加載的本地動(dòng)畫踱葛,下面的動(dòng)畫是從插件里面加載的土陪。
代碼介紹
如圖所示:
工程app作為宿主程序煤蹭,plugin作為插件程序笔喉,資源文件也在plugin里面,需要實(shí)現(xiàn)的是啟動(dòng)app來加載插件plugin里面的資源文件
這里實(shí)現(xiàn)思路大致畫一下硝皂,需要在主程序里面加載插件程序的資源文件常挚,我們需要拿到插件程序里面的Context, 獲取資源文件也就是AssetManager,這里需要用到j(luò)ava的反射機(jī)制
這里給出核心代碼部分
PluginResources 類如下
package com.cayden.plugin;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import java.io.File;
import java.lang.reflect.Method;
/**
* Created by cuiran
* Time 17/3/14 17:37
* Email cuiran2001@163.com
* Description
*/
public class PluginResources extends Resources {
public PluginResources(AssetManager assets, DisplayMetrics metrics, Configuration config) {
super(assets, metrics, config);
}
public static PluginResources getPluginResources(Resources resources,AssetManager assets){
PluginResources pluginResources=new PluginResources(assets,resources.getDisplayMetrics(),resources.getConfiguration());
return pluginResources;
}
public static AssetManager getPluginAssetManager(File apk) throws ClassNotFoundException{
//需要通過反射獲取AssetManager
Class<?> forName= Class.forName("android.content.res.AssetManager");
Method[] methods= forName.getDeclaredMethods();
for(Method method:methods){
if(method.getName().equals("addAssetPath")){
try{
AssetManager assetManager=AssetManager.class.newInstance();
method.invoke(assetManager,apk.getAbsolutePath());
return assetManager;
}catch (InstantiationException e){
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
}
return null;
}
}
啟動(dòng)MainActivity類如下
package com.cayden.plugin;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import dalvik.system.DexClassLoader;
public class MainActivity extends Activity implements View.OnClickListener{
private static final String TAG=MainActivity.class.getSimpleName();
private ImageView imageSource,imageCloud;
private final static String SOURCE_TAG="source";
private final static String CLOUD_TAG="cloud";
private final static String CLOUD_ANIM="animation1";
private final static String PLUGIN_NAME="plugin.apk";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
imageSource=(ImageView)findViewById(R.id.imageSource);
imageCloud=(ImageView)findViewById(R.id.imageCloud);
imageSource.setTag(SOURCE_TAG);
imageCloud.setTag(CLOUD_TAG);
imageSource.setOnClickListener(this);
imageCloud.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view.getTag().equals(SOURCE_TAG)){
handleAnim(view);
}else{
//插件動(dòng)畫
//是否加載
String fileName=PLUGIN_NAME;
String filePath=this.getCacheDir()+ File.separator+fileName;
String packageName="com.cayden.pluginb";
File apkFile=new File(filePath);
if(apkFile.exists()){
Drawable background= view.getBackground();
if(background instanceof AnimationDrawable){
//執(zhí)行動(dòng)畫
handleAnim(view);
}else{
//執(zhí)行插件
try{
AssetManager assetManager=PluginResources.getPluginAssetManager(apkFile);
PluginResources resources= PluginResources.getPluginResources(getResources(),assetManager);
//反射文件
DexClassLoader classLoader=new DexClassLoader(apkFile.getAbsolutePath(),this.getDir(fileName, Context.MODE_PRIVATE).getAbsolutePath(),null,this.getClassLoader());
Class<?> loadClass= classLoader.loadClass(packageName+".R$drawable");
Field[] fields= loadClass.getDeclaredFields();
for(Field field :fields){
if(field.getName().equals(CLOUD_ANIM)){
int animId=field.getInt(R.drawable.class);
Drawable drawable=resources.getDrawable(animId);
((ImageView) view).setBackgroundDrawable(drawable);
handleAnim(view);
}
}
}catch (Exception e){
e.printStackTrace();
}
}
}else{
//需要從服務(wù)端下載稽物,測(cè)試就放入assets目錄下
try{
InputStream is=this.getAssets().open(fileName);
FileOutputStream os=new FileOutputStream(filePath);
int len=0;
byte [] buffer=new byte[1024];
while((len=is.read(buffer))!=-1){
os.write(buffer,0,len);
}
os.close();
is.close();
Log.d(TAG,"file ok");
Toast.makeText(this,"file ok",Toast.LENGTH_SHORT).show();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
private void handleAnim(View v){
AnimationDrawable background=(AnimationDrawable)v.getBackground();
if(background!=null){
if(background.isRunning() ){
background.stop();
}else{
background.stop();
background.start();
}
}
}
}
為了演示方便 我們把plugin.apk放在主程序app工程的assets目錄下奄毡。
最后給出項(xiàng)目的源碼地址:https://github.com/cayden/PluginProject