Android 集成

注:以下文档都是假设您已经具备一定的Android开发经验。

这篇教程,讲述了如何将 Hippy 集成到 Android 工程。

前期准备

  • 已经安装了JDK version>=1.7 并配置了环境变量
  • 已经安装Android SDK 并配置环境变量。
  • Android SDK version 23 (compileSdkVersion in build.gradle)
  • SDK build tools version 23.0.1 (buildToolsVersion in build.gradle)

快速接入

步骤如下:

  1. 创建一个Android工程
  2. DEMO工程编译后会在android\sdk\build\outputs\aar目录下生成sdk-release.aar,android\support_ui\build\outputs\aar目录下生成support-ui.jar将sdk-release.aar和support-ui.jar拷贝到你的libs目录下
  3. 配置一下build.gradle
  1. compile(name:'sdk-release', ext:'aar')
  2. compile files('libs/support-ui.jar')
  3. compile 'com.github.bumptech.glide:glide:3.6.1'
  4. compile 'com.android.support:support-v4:25.3.1'

后面的 glide 与support-v4 主要是用来demo里面拉图,如果你们有自己的拉图库,可以使用自己的拉图代码。

  1. 如果要进一步精简包大小,可以添加如下filter,排除掉armeabi-v7a下的so库,这样可以减小hippy 50%的大小:
  1. android {
  2. ....
  3. defaultConfig {
  4. ....
  5. ndk {
  6. abiFilters "armeabi"
  7. }
  8. }
  9. }

代码实现

注:附录中有完整代码地址

实现图片下载

实现图片下载接口,初始化时设置。

需要实现ImageLoaderAdapter.java和DrawableTarget.java

  1. public class ImageLoaderAdapter implements HippyImageLoaderAdapter {
  2. @Override
  3. public void fetchImage(String url, final HippyImageRequestListener requestListener, Object param)
  4. {
  5. Glide.with(ContextHolder.getAppContext()).load(url).asBitmap().into(new DrawableTarget(requestListener));
  6. }
  7. @Override
  8. public HippyDrawableTarget getImage(String url, Object param) {
  9. LocalHippyDrawable localHippyDrawable = new LocalHippyDrawable();
  10. localHippyDrawable.setUrl(url);
  11. return localHippyDrawable;
  12. }
  13. }
  14. public class DrawableTarget extends SimpleTarget<Bitmap> implements HippyDrawableTarget{
  15. HippyImageRequestListener mListener;
  16. Bitmap mBitmap;
  17. public DrawableTarget(HippyImageRequestListener listener)
  18. {
  19. this.mListener = listener;
  20. }
  21. @Override
  22. public Bitmap getBitmap()
  23. {
  24. return mBitmap;
  25. }
  26. @Override
  27. public String getSource()
  28. {
  29. return null;
  30. }
  31. @Override
  32. public Object getExtraData()
  33. {
  34. return null;
  35. }
  36. @Override
  37. public void onDrawableAttached() {}
  38. @Override
  39. public void onDrawableDetached() {}
  40. @Override
  41. public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation)
  42. {
  43. this.mBitmap = bitmap;
  44. mListener.onRequestSuccess(this);
  45. }
  46. @Override
  47. public void onLoadFailed(Exception e, Drawable errorDrawable)
  48. {
  49. mListener.onRequestFail(e, null);
  50. }
  51. }

初始化

  1. public class ExampleHippyEngineHost extends HippyEngineHost {
  2. private Application mApplication;
  3. public ExampleHippyEngineHost(Application application) {
  4. super(application);
  5. this.mApplication = application;
  6. }
  7. @Override
  8. protected List<HippyPackage> getPackages() {
  9. // return null;
  10. List<HippyPackage> hippyPackages = new ArrayList<HippyPackage>();
  11. hippyPackages.add(new ExtendPackages());
  12. return hippyPackages;
  13. }
  14. @Override
  15. protected HippyBundleLoader getCoreBundleLoader() {
  16. return new HippyAssetBundleLoader(mApplication, "base.android.jsbundle", true, "common");
  17. }
  18. @Override
  19. public HippyGlobalConfigs getHippyGlobalConfigs() {
  20. return new HippyGlobalConfigs.Builder().setApplication(mApplication).setImageLoaderAdapter(new ImageLoaderAdapter()).build();
  21. }
  22. }

实现渲染

  1. public class TestActivity extends Activity implements HippyEngineEventListener
  2. {
  3. private ExampleHippyEngineHost mHost;
  4. private HippyEngineManager mEngineManager;
  5. private HippyRootView mInstance;
  6. @Override
  7. public void onCreate(Bundle savedInstanceState)
  8. {
  9. super.onCreate(savedInstanceState);
  10. getWindow().requestFeature(Window.FEATURE_NO_TITLE);
  11. mHost = new ExampleHippyEngineHost(TestActivity.this.getApplication());
  12. // mEngineManager = mHost.createHippyEngineManager();
  13. mEngineManager = mHost.createDebugHippyEngineManager("index");
  14. mEngineManager.addEngineEventListener(this);
  15. mEngineManager.initEngineInBackground();
  16. }
  17. @Override
  18. protected void onResume()
  19. {
  20. super.onResume();
  21. mEngineManager.onEngineResume();
  22. }
  23. @Override
  24. protected void onStop() {
  25. super.onStop();
  26. mEngineManager.onEnginePause();
  27. }
  28. @Override
  29. protected void onDestroy()
  30. {
  31. mEngineManager.destroyInstance(mInstance);
  32. mEngineManager.removeEngineEventListener(this);
  33. mEngineManager.destroyEngine();
  34. super.onDestroy();
  35. }
  36. @Override
  37. public void onEngineInitialized(boolean success)
  38. {
  39. if (success) {
  40. HippyRootViewParams.Builder builder = new HippyRootViewParams.Builder();
  41. HippyMap params = new HippyMap();
  42. HippyBundleLoader hippyBundleLoader = null;
  43. if (!mEngineManager.getSupportDev()) {
  44. hippyBundleLoader = new HippyAssetBundleLoader(getApplicationContext(), "index.android.js", true, "demo");
  45. }
  46. builder.setBundleLoader(hippyBundleLoader).setActivity(TestActivity.this).setName("Demo")
  47. .setLaunchParams(params);
  48. mInstance = mEngineManager.loadInstance(builder.build());
  49. setContentView(mInstance);
  50. }
  51. }
  52. }

附录

Demo 下载地址