博客
关于我
强烈建议你试试无所不能的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://afpci.baihongyu.com/

你可能感兴趣的文章
nodejs内存控制
查看>>
nodejs Stream使用中的陷阱
查看>>
MongoDB 数据文件备份与恢复
查看>>
数据库索引介绍及使用
查看>>
MongoDB数据库插入、更新和删除操作详解
查看>>
MongoDB文档(Document)全局唯一ID的设计思路
查看>>
mongoDB简介
查看>>
Redis持久化存储(AOF与RDB两种模式)
查看>>
memcached工作原理与优化建议
查看>>
Redis与Memcached的区别
查看>>
redis sharding方案
查看>>
程序员最核心的竞争力是什么?
查看>>
Node.js机制及原理理解初步
查看>>
linux CPU个数查看
查看>>
分布式应用开发相关的面试题收集
查看>>
简单理解Socket及TCP/IP、Http、Socket的区别
查看>>
利用HTTP Cache来优化网站
查看>>
利用负载均衡优化和加速HTTP应用
查看>>
消息队列设计精要
查看>>
分布式缓存负载均衡负载均衡的缓存处理:虚拟节点对一致性hash的改进
查看>>