博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android生成和引用第三方库(jar)的…
阅读量:4053 次
发布时间:2019-05-25

本文共 2374 字,大约阅读时间需要 7 分钟。

本帖最后由 walxlgf 于 2011-5-26 14:20 编辑
临时有一个安排,说实现一个带UI播放器的Jar给第三方调用,研究了一下,找到一份资料,但也不是很明白,最后打电话问人了,参考了一下百度的代码BaiduMapApi_Lib_Android_1.0.1.zip,终于有点眉目了,备查并分享一下。先贴一下步骤:
[p=23, null, left]1, Export Library
1.1 Prepare source code
1.1.1 Create an Android project
1.1.2 Create source code, and fix all bug
1.1.3 remove AndroidManifest.xml
1.1.4 remove res/drawable/icon.png
1.2 Export library
1.2.1 On Package Explorer of Eclipse, right-click created project, and select Export
1.2.2 Select Jave -> JAR file, then press \”Next\”
1.2.3 Select resources to export, then press \”Next\”
1.2.4 Press \”Next\”
1.2.5 Press \”Finish\”
Then the library is created.
(Reference: Eclipse export jar files … 8b2fb2d0a2d3ad.html)
[p=23, null, left]
[p=23, null, left]2, Import Library
You can use a third party JAR in your application by adding it to your Eclipse project as follows:
In the Package Explorer panel, right-click on your project and select Properties.
Select Java Build Path, then the tab Libraries.
Press the Add External JARs… button and select the JAR file.
Alternatively, if you want to include third party JARs with your package, create a new directory for them within your project and select Add Library… instead.
[p=23, null, left]It is not necessary to put external JARs in the assets folder.

就不译了,也挺简单的,现在主要说一下我碰到的问题。我的想法很简单,自定义一个组件(关于自定组件,可以参考),这个自定义组件就是一个LinearLayout,背景设置为一个图片,图片路径为:res/drawable/bg.png,通过出this.setBackgroundResource(R.drawable.bg)实现,按照上述的步骤输出为jar后,给另一个app调用,发现图片根本无法输出,老是显示调用者的默认Icon.

  反编了一下百度地图jar,发现它所有图片都是放在assets中,并不是放在res/drawable中,然后再查看了一下代码,其对图片的调用是通过AssetManager.open()方法来实现的,并非通过R.drawble来实现,修改后的代码如下:

  1.         private void init() {
  2.                 try {
  3.                         //获取图片转换为Bitmap
  4.                         AssetManager localAssetManager = this.getResources().getAssets();
  5.                         String str = "home_pressed.png";
  6.                         InputStream localInputStream = localAssetManager.open(str);
  7.                         Bitmap localBitmap = BitmapFactory.decodeStream(localInputStream);
  8.                         localInputStream.close();
  9.                         //把Bitmap图像转换为Drawable
  10.                         Drawable drawable = new BitmapDrawable(localBitmap);  
  11.                         //设置背景
  12.                         if(localBitmap != null){
  13.                                 setBackgroundDrawable(drawable);
  14.                         }
  15.                 } catch (Exception e) {
  16.                         e.printStackTrace();
  17.                 }
  18.         }
复制代码

图片不能通过R.drawable.xxxxx来调用,举一反三,是不是res/...下面的所有目录下的内容,比如layout文件、values下的strings、colors、dimens等都不能能过R.xx.xxxxx来调用。如果是这样的话,那真的很悲摧,因为播放器界面的按钮不少,而且还要不同分辨率的适配和国际化。代码量是可想而知了。贴源码吧。

这是jar包源代码,尚未删除AndroidManifest.xml和icon.png,在输出Jar包时把这两者删除后再输出.

调用jar包app源代码

转载地址:http://xfpci.baihongyu.com/

你可能感兴趣的文章
慢慢欣赏linux CPU占用率学习
查看>>
2020年终总结
查看>>
Homebrew指令集
查看>>
React Native(一):搭建开发环境、出Hello World
查看>>
React Native(二):属性、状态
查看>>
JSX使用总结
查看>>
React Native(四):布局(使用Flexbox)
查看>>
React Native(七):Android双击Back键退出应用
查看>>
Android自定义apk名称、版本号自增
查看>>
adb command not found
查看>>
Xcode 启动页面禁用和显示
查看>>
【剑指offer】q50:树中结点的最近祖先
查看>>
二叉树的非递归遍历
查看>>
【leetcode】Reorder List (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Candy(python)
查看>>
【leetcode】Clone Graph(python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
【leetcode】Pascal's Triangle II (python)
查看>>